Week 04 · Build Your Own Automated Options Trading System

Setting up your toolkit.

Pros are not smarter. Their desks are tidier. This week we tidy yours — once — so the next eight weeks are about ideas, not setup pain.

Week 4 of 12
Editor · repo · venv · .env
First 10-line script prints NIFTY
This week, in one glance

Same five beats. New topic.

1
The idea
Pros are not smarter; their desks are tidier. We tidy yours once.
2
The outcome
Editor, repository, and Claude Code working; a 10-line script prints NIFTY.
3
Vocabulary
code editor · repository · commit / revert · virtual environment · .env file
4
Hands-on
Run a script · make a small mistake on purpose · undo it.
5
War story
Lots vs. units — "3" in one segment means "195" in another. The trap that has cost the most.
1 · The idea

A tidy desk is not discipline. It is leverage.

The messy desk

  • Code lives in a folder on the Desktop called Final_Final_v3.
  • Passwords pasted inside main.py — committed to a public repo by accident.
  • "It worked yesterday." No way to compare to yesterday.
  • One Python on the laptop. Every project breaks the others.
  • Editor is Notepad. Errors are invisible until runtime.

The tidy desk

  • Code lives in a repository. Every change is a labelled snapshot.
  • Secrets live in .env. Never in code. Never in commits.
  • You can revert to any past version in seconds.
  • Each project has a virtual environment — its own Python and packages.
  • Editor (VS Code / Cursor) catches typos and unsaved files before you run anything.
Set up the desk once. Reap it for every week to come.
The four things you install this week

Four tools. One afternoon.

📝

Code editor

Where you read and edit files. Highlights errors, autocompletes, integrates with Claude.

Pick: VS Code or Cursor
🗂

Git + GitHub

Saves every version of your code. Lets you revert mistakes. Backs up everything to the cloud.

Install: git · sign up: github.com
🐍

Python + venv

Python runs your scripts. venv gives each project its own sandbox so installs never collide.

python3 -m venv .venv
🤖

Claude Code

Already installed from Week 2. This week you point it at your new project folder.

cd ~/trading && claude
No new concepts after this week. Just deepening what these four already give you.
2 · By the end of this week

The first script. Ten lines. Prints NIFTY.

VS Code (or Cursor) is open on a folder called ~/trading. Everything else lives inside that one folder.
The folder is a Git repository. You can run git log and see at least one commit.
Your .venv is active. pip install requests goes into the sandbox, not your system Python.
A 10-line nifty.py script prints today's NIFTY price when you run it. Credentials read from .env — not hard-coded.
If anything here is missing on Sunday, ask Claude to walk you through the missing piece — out loud, step by step.
3 · New vocabulary

Five words. Use them this week.

code editor
The text-editor-for-code where you read and modify files. Catches typos before runtime.
VS Code · Cursor
repository
A folder under Git's care. Every save can become a labelled snapshot you can return to.
~/trading/.git/
commit / revert
Commit = take a labelled snapshot. Revert = go back to any past snapshot, exactly as it was.
git commit · git revert
virtual environment
A sandboxed Python for one project. Installing here does not touch other projects.
.venv/bin/activate
.env file
A text file holding secrets (tokens, passwords). Read by code at runtime. Never committed.
UPSTOX_TOKEN=eyJ…
If .env ever ends up in git status, stop everything and add it to .gitignore. Then rotate the secret.
The first real script

Ten lines. Prints NIFTY.

~/trading — nifty.py
# nifty.py — prints today's NIFTY 50 last price
import os, requests
from dotenv import load_dotenv

load_dotenv()                              # read .env
token = os.environ["UPSTOX_TOKEN"]

url = "https://api.upstox.com/v2/market-quote/quotes"
params = {"instrument_key": "NSE_INDEX|Nifty 50"}
headers = {"Authorization": f"Bearer {token}"}

r = requests.get(url, params=params, headers=headers).json()
print("NIFTY:", r["data"]["NSE_INDEX:Nifty 50"]["last_price"])


$ python nifty.py
NIFTY: 24825.10
Notice: zero secrets in the file. .env does that work. The script is the same on your laptop and on the cloud server.
4 · The hands-on bit — your task

Run. Break. Undo.

What to do this week (60–90 min, once)

  1. Make the folder a repo. cd ~/trading && git init. Add a one-line README.md. Commit.
  2. Create the venv. python3 -m venv .venv && source .venv/bin/activate. Then pip install requests python-dotenv.
  3. Create .env with your Upstox token. Add a line to .gitignore: .env. Verify git status does not show .env.
  4. Run nifty.py. See the number. Commit the working version.
  5. Break it on purpose. Change "Nifty 50" to "Nifty Fifty". Run. See the error. Read the error.
  6. Revert. git checkout -- nifty.py. Run again. Working. Notice how cheap "undo" was.
The point isn't the price. The point is feeling how safe the cycle is — break, see, undo — when the desk is tidy.
5 · A real-money war story

"3" in one segment means "195" in another.

Different exchange segments expect quantity in different units. Equity options on NSE/BSE want total units (lots × lot size). MCX commodities want lots, directly. Pass "3" to the wrong segment and you've ordered 65× what you meant.

Segment Instrument What you mean What the API expects Pass "3" here →
NFO (NIFTY) NIFTY 24800 CE 3 lots qty = 3 × 65 = 195 Order goes for 3 units = 1/65th of a lot — rejected
BFO (SENSEX) SENSEX 80000 PE 3 lots qty = 3 × 20 = 60 Same trap — under-orders by 20×
MCX (commodity) CRUDEOIL CE 3 lots qty = 3 (lots directly) Correct — segment is "lots", not "units"
The broker adapter does not convert. The strategy code has to know which segment expects which unit. Get it wrong → mis-sized order → real money at risk.
The discipline that fixes it

One conversion, in one place, per segment.

Naïve (the bug)

Strategy passes "lots" everywhere; trusts the broker to interpret it. NFO/BFO orders silently under-size by lot-size×.

# strategy/oc.py — wrong for NFO/BFO
order = OrderRequest(
    instrument=NIFTY_CE,
    quantity=lots,   # ❌ 3 sent as raw "3"
    side="SELL",
)

Disciplined (the fix)

Equity strategies multiply by lot_size at the boundary. MCX passes lots directly. The rule is written in CLAUDE.md and checked in code review.

# equity strategies
order_qty = lots * cfg["lot_size"]
order = OrderRequest(quantity=order_qty, ...)

# MCX strategies
order = OrderRequest(quantity=lots, ...)
65×
NIFTY lot size — the multiplier you'd miss
20×
SENSEX lot size — same trap, smaller factor
1
place to convert: at the broker boundary
Week 4 — takeaway

A tidy desk costs a few hours.
A messy one costs months of bugs.

Editor, repo, venv, .env — set up once, used forever. And the most expensive bug in this entire system is a unit mix-up. Pin it: NFO/BFO want units. MCX wants lots. Different segments, different rules.

End of Week 4

Next: A cloud server. A static IP.
Your first whitelisted API call.

source: cowork/Course_Outline_12_Weeks · Week 4
← → navigate · F fullscreen · click to advance
1 / 12