44 - PnL Tracker
Overview
Architecture Diagram
┌──────────────────────────────────────────────────────────────────────────────┐
│ PnL Tracker Architecture │
└──────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Data Sources │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Tradebook │ │ Position Book │ │ History API │ │
│ │ (Broker API) │ │ (Broker API) │ │ (1-minute bars)│ │
│ └────────┬────────┘ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │ │
│ └────────────────────┼────────────────────┘ │
│ │ │
│ ▼ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ PnL Calculation (blueprints/pnltracker.py) │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Position Window Tracking │ │
│ │ │ │
│ │ 1. Parse trades from tradebook │ │
│ │ 2. Group by symbol/exchange │ │
│ │ 3. Create position windows (start_time, end_time, qty, price) │ │
│ │ 4. Apply rate limiting (2 calls/sec for history API) │ │
│ │ 5. Calculate MTM using historical close prices │ │
│ │ 6. Aggregate all symbols into portfolio P&L │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ P&L Calculation Formula │ │
│ │ │ │
│ │ For LONG positions: │ │
│ │ MTM P&L = (Current Price - Entry Price) × Quantity │ │
│ │ │ │
│ │ For SHORT positions: │ │
│ │ MTM P&L = (Entry Price - Current Price) × Quantity │ │
│ │ │ │
│ │ Realized P&L = (Exit Price - Entry Price) × Quantity [Long] │ │
│ │ = (Entry Price - Exit Price) × Quantity [Short] │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────────┐
│ Frontend Display │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Metrics Cards │ │
│ │ │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Current │ │ Max │ │ Min │ │ Max │ │ │
│ │ │ MTM │ │ MTM │ │ MTM │ │ Drawdown │ │ │
│ │ │ +₹3,750 │ │ +₹4,200 │ │ +₹1,000 │ │ -₹800 │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ P&L Chart (LightWeight Charts) │ │
│ │ │ │
│ │ ₹ │ │
│ │ 4000│ ╭──────╮ │ │
│ │ 3000│ ╭───╯ ╰──╮ │ │
│ │ 2000│╭───╯ ╰────── │ │
│ │ 1000│ │ │
│ │ 0├────────────────────────────► Time │ │
│ │ 9:15 10:00 11:00 12:00 1:00 │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘Implementation Details
Position Window Tracking
Trade Timestamp Parsing
Rate Limiting
Carry-Forward Position PnL Tracking
API Endpoint
Get P&L Data
Response Fields
Field
Type
Description
Calculation Flow
Data Dependencies
Service
Purpose
Frontend Components
React Page
Legacy Jinja Template
Edge Cases Handled
Sub-Minute Trades
Pre-Trade Period
Timezone Handling
Drawdown Calculation
Key Files Reference
File
Purpose
Last updated