Think of a variable as a labelled box that holds a value. In trading, every number you work with — a stock price, a trade quantity, a P&L figure — needs to be stored somewhere so your program can use it. Python lets you create a variable with a simple name = value assignment.
name = value — no type declaration needed.ltp updates every second; your variable can too.entry_price.for, if, return, True, class, etc.# ── Creating variables ────────────────────────── symbol = "NIFTY25MAYFUT" # str – stock / contract symbol entry_price = 24350.50 # float – entry price in ₹ quantity = 50 # int – lot size is_long = True # bool – direction of trade print("Symbol :", symbol) print("Entry :", entry_price) print("Qty :", quantity) print("Long? :", is_long)
Symbol : NIFTY25MAYFUT Entry : 24350.5 Qty : 50 Long? : True
p is bad; entry_price is perfect.
When debugging at midnight, you'll thank yourself.
Python automatically detects the type of a variable from the value you assign. As a trader-programmer, you'll use these five constantly:
| Type | Trading Use | Example | Check with |
|---|---|---|---|
| int | Quantity, lot size, candle index | quantity = 50 | type(quantity) → int |
| float | Price, P&L, percentage | price = 24350.50 | type(price) → float |
| str | Symbol, signal text, broker name | sym = "RELIANCE" | type(sym) → str |
| bool | Signal on/off, kill switch, position active | is_long = True | type(is_long) → bool |
| None | Unset stop loss, missing data | stop_loss = None | stop_loss is None |
str instead of float.
If you don't convert them, your P&L calculation will crash. Always check types when working with external data!
When your broker API returns "24350.50" instead of 24350.50,
you need to convert it before doing any maths.
int(x)float(x)str(x)bool(x)# ── Broker API returns everything as strings ── api_ltp = "24380.25" api_qty = "50" api_signal = "1" # ── Convert before using ────────────────────── ltp = float(api_ltp) qty = int(api_qty) signal = bool(int(api_signal)) entry = 24350.50 pnl = (ltp - entry) * qty print(f"LTP : {ltp}") print(f"Qty : {qty}") print(f"Signal : {signal}") print(f"P&L : ₹{pnl}")
LTP : 24380.25 Qty : 50 Signal : True P&L : ₹1487.5
TypeError: unsupported operand type(s) for -: 'str' and 'float' # ltp is still "24380.25" (string) — Python can't do maths on text!
float / int.
+ Add- Subtract* Multiply/ Divide// Floor div% Modulo# ══ NIFTY FUTURES — P&L CALCULATOR ═══════════════════ symbol = "NIFTY25MAYFUT" entry_price = 24350.50 exit_price = 24420.75 quantity = 50 brokerage = 20.0 capital = 500000.0 pnl_per_unit = exit_price - entry_price gross_pnl = pnl_per_unit * quantity net_pnl = gross_pnl - (brokerage * 2) return_pct = (net_pnl / capital) * 100 stop_loss = entry_price - 40 target_price = entry_price + 80 risk_reward = 80 / 40 print(f"Gross P&L : ₹{gross_pnl}") print(f"Net P&L : ₹{net_pnl}") print(f"Return : {return_pct:.2f}%") print(f"Stop Loss : ₹{stop_loss}") print(f"Target : ₹{target_price}") print(f"R:R Ratio : 1 : {risk_reward}")
Gross P&L : ₹3512.5 Net P&L : ₹3472.5 Return : 0.69% Stop Loss : ₹24310.5 Target : ₹24430.5 R:R Ratio : 1 : 2.0
:.2f Format Trick:.2f rounds to 2 decimal places. Use :, for comma-separated thousands like ₹3,472.50.
# ── F-string formatting examples ─────────────────── symbol = "BANKNIFTY25MAYFUT" price = 52145.50 qty = 15 pnl = -3250.75 capital = 1000000 print(f"Order: BUY {qty} lots of {symbol} @ ₹{price}") print(f"P&L: ₹{pnl:.2f}") print(f"Capital: ₹{capital:,}") print(f"Result: {'PROFIT ✓' if pnl > 0 else 'LOSS ✗'}")
Order: BUY 15 lots of BANKNIFTY25MAYFUT @ ₹52145.5 P&L: ₹-3250.75 Capital: ₹1,000,000 Result: LOSS ✗
# ══ TRADE JOURNAL ENTRY — RELIANCE ════════════════════ trade_id = "TXN-20250404-001" symbol = "RELIANCE" side = "BUY" entry_price = 2985.50 exit_price = 3034.75 quantity = 100 brokerage = 40.0 stt = 0.025 gross_pnl = (exit_price - entry_price) * quantity stt_amount = (exit_price * quantity * stt) / 100 net_pnl = gross_pnl - brokerage - stt_amount result_tag = "WIN" if net_pnl > 0 else "LOSS" print(f"Trade ID : {trade_id}") print(f"Symbol : {symbol}") print(f"Side : {side}") print(f"Entry : ₹{entry_price}") print(f"Exit : ₹{exit_price}") print("─" * 36) print(f"Gross P&L : ₹{gross_pnl:,.2f}") print(f"Net P&L : ₹{net_pnl:,.2f}") print(f"Result : [{result_tag}]")
Trade ID : TXN-20250404-001 Symbol : RELIANCE Side : BUY Entry : ₹2985.5 Exit : ₹3034.75 ──────────────────────────────────── Gross P&L : ₹4,925.00 Net P&L : ₹4,884.24 Result : [WIN]
24350.50?qty = "50" as a string. What converts it to an integer?pnl = 3472.5 as ₹3,472.50?symbol, ltp, prev_close. Calculate change_pct = ((ltp - prev_close) / prev_close) * 100. Print a formatted table with all 3 using f-string padding.- Variables — how to store and name trading data in Python
- 5 data types —
int,float,str,bool,Noneand when to use each - Type conversion — critical for broker API data (
int(),float(),str()) - Arithmetic operators —
+-*///%for P&L calculations - F-strings — embed variables, format decimals, add comma separators
- Real application — NIFTY P&L calculator + RELIANCE trade journal entry