07 - Sandbox Architecture (Analyzer Mode)
Overview
OpenAlgo's Sandbox/Analyzer mode provides a production-grade walkforward testing environment with ₹1 Crore sandbox capital, realistic margin calculations, leverage-based trading, auto square-off, and T+1 settlement simulation. It runs completely isolated from live trading with its own database (db/sandbox.db).
Architecture Diagram

Why margin_blocked is critical:
Stores exact margin calculated at order placement
Prevents over/under-release when execution price ≠ order price
Ensures margin consistency across async execution
SandboxTrades Table
Records executed trades linked to orders.
SandboxPositions Table
Tracks open positions with comprehensive P&L tracking.
P&L Field Semantics:
pnl: Display field for unrealized P&L (varies by context)accumulated_realized_pnl: All-time realized, never decrementstoday_realized_pnl: Daily realized, resets at session boundary (03:00 IST)
SandboxHoldings Table
T+1 settled CNC positions (delivery holdings).
SandboxFunds Table
Sandbox capital management per user.
Fund Balance Equation:
SandboxDailyPnL Table
EOD snapshots for historical P&L reporting.
SandboxConfig Table
Global configuration for all sandbox settings.
Default Configuration Values:
starting_capital
10000000
₹1 Crore sandbox capital
reset_day
Never
Weekly reset day (Never/Monday-Sunday)
reset_time
00:00
Reset time in IST
equity_mis_leverage
5
5x leverage for equity intraday
equity_cnc_leverage
1
1x for equity delivery
futures_leverage
10
10x for futures
option_buy_leverage
1
Full premium for option buy
option_sell_leverage
1
Full premium for option sell
nse_bse_square_off_time
15:15
NSE/BSE MIS square-off
cds_bcd_square_off_time
16:45
Currency MIS square-off
mcx_square_off_time
23:30
MCX MIS square-off
ncdex_square_off_time
17:00
NCDEX MIS square-off
order_check_interval
5
Execution engine polling (seconds)
mtm_update_interval
5
Position MTM update (seconds)
2. Fund Manager
Location: sandbox/fund_manager.py
Manages sandbox capital with thread-safe operations and realistic margin calculations.
Margin Calculation
Margin Block/Release Flow
Margin Reconciliation
Detects and fixes margin inconsistencies.
Auto-Reset Feature
3. Execution Engine
Location: sandbox/execution_engine.py
Background worker that monitors pending orders and executes them based on live market data.
Main Execution Loop
Order Execution Logic by Price Type
Trade Execution and Position Update
Execution Flow Diagram
4. Position Manager
Location: sandbox/position_manager.py
Handles position tracking, MTM updates, session filtering, and expired contract handling.
MTM (Mark-to-Market) Updates
Session Filtering
Expired Contract Handling
5. Square-Off Manager
Location: sandbox/squareoff_manager.py
Automatically closes MIS positions at exchange-specific timings.
Main Square-Off Logic
APScheduler Jobs
6. Holdings Manager
Location: sandbox/holdings_manager.py
Handles T+1 settlement and holdings MTM.
T+1 Settlement Process
7. Order Manager
Location: sandbox/order_manager.py
Handles order placement, modification, and cancellation.
Order Placement
Order Modification
Order Cancellation
8. API Integration
Location: restx_api/analyzer.py, services/sandbox_service.py
All major API endpoints check sandbox mode and route accordingly.
Analyzer Toggle Endpoint
Complete Order Flow Diagram
Session and Settlement Flow
Key Files Reference
database/sandbox_db.py
All database models and initialization
sandbox/fund_manager.py
Capital and margin management
sandbox/execution_engine.py
Order execution background worker
sandbox/position_manager.py
Position tracking and MTM
sandbox/squareoff_manager.py
Auto square-off scheduling
sandbox/holdings_manager.py
T+1 settlement logic
sandbox/order_manager.py
Order CRUD operations
sandbox/catch_up_processor.py
Startup catch-up for missed events
sandbox/execution_thread.py
Execution engine thread management
sandbox/websocket_execution_engine.py
WebSocket-based order execution
sandbox/squareoff_thread.py
APScheduler management
services/sandbox_service.py
API integration layer
services/analyzer_service.py
Analyzer mode toggle
restx_api/analyzer.py
REST API endpoints
blueprints/analyzer.py
Web UI routes
blueprints/sandbox.py
Configuration UI routes
Configuration Blueprint
Location: /sandbox web routes
/sandbox/
GET
Configuration page
/sandbox/api/configs
GET
Get all config values
/sandbox/update
POST
Update config value
/sandbox/reset
POST
Reset all sandbox data
/sandbox/reload-squareoff
POST
Reload square-off schedule
/sandbox/squareoff-status
GET
Current square-off status
/sandbox/mypnl
GET
P&L history page
/sandbox/mypnl/api/data
GET
P&L history data (JSON)
Last updated