API Analyzer (Sandbox Mode)
Relevant source files
The following files were used as context for generating this wiki page:
The API Analyzer, also known as Sandbox Mode, is a production-grade simulated trading environment within OpenAlgo designed for strategy testing and validation without financial risk. It provides a fully isolated infrastructure that mimics real broker interactions, including realistic margin requirements, leverage calculations, and exchange-aligned square-off schedules.
The system utilizes a dedicated database (sandbox.db) to ensure complete isolation from live trading activities. It is seeded with a configurable starting capital (defaulting to ₹1 Crore) and supports core order types including MARKET, LIMIT, SL, and SL-M across various asset classes.
Sources: sandbox/__init__.py:4-18, README.md:121-131, CLAUDE.md:104-106
Architecture and System Flow
The Sandbox architecture decouples simulated execution from the live broker adapters. When Analyzer Mode is enabled, the request pipeline redirects order placement from live broker plugins to the Sandbox execution engine.
Data Flow Overview
The following diagram illustrates how a user request is processed when the system is in Sandbox Mode:
Sources: README.md:121-131, CLAUDE.md:144-149
Core Components
| Component | Description |
|---|---|
| Sandbox Execution Engine | Simulates order matching, margin blocking, and position management. |
| Sandbox DB | Isolated SQLite store for orders, trades, positions, holdings, and funds. |
| Scheduler | Handles automatic MIS square-offs based on exchange timings (NSE, MCX, etc.). |
| Fund Manager | Manages capital allocation, margin release, and T+1 settlement for CNC. |
| Sources: sandbox/__init__.py:8-17, test/sandbox/README.md:65-71, blueprints/sandbox.py:228-245 |
Configuration and Governance
The Sandbox environment is highly configurable via the web UI and API. Users can adjust leverage settings, capital, and operational intervals to match specific testing requirements.
Configuration Parameters
| Parameter | Default Value | Description |
|---|---|---|
starting_capital | ₹10,000,000 | Initial capital provided to the sandbox account. |
equity_mis_leverage | 5x | Leverage multiplier for Intraday Equity. |
futures_leverage | 10x | Leverage multiplier for Futures contracts. |
order_check_interval | 5 seconds | Frequency of checking pending LIMIT/SL orders. |
reset_day | Sunday | Day for automatic capital and data reset. |
| Sources: blueprints/sandbox.py:75-108, sandbox/__init__.py:8-17 |
Control Endpoints
The system provides REST endpoints to manage the sandbox state:
GET /api/analyzer-services/analyzerstatus: Retrieves the current state (on/off) of the analyzer.POST /api/analyzer-services/analyzertoggle: Toggles the sandbox mode for the instance.
Sources: okf/api/analyzer-services/analyzerstatus.md:16-24, okf/api/analyzer-services/analyzertoggle.md:16-24
Execution Logic and Validation
The execution engine performs strict validation similar to real-world brokers to ensure realistic simulation.
Order Life Cycle
Margin Scenarios
The Sandbox environment specifically tests for:
- Partial Closure: Ensuring margin is released correctly when closing only part of a position (e.g., BUY 100 → SELL 50).
- Position Reversal: Transitioning directly from Long to Short (e.g., BUY 100 → SELL 200).
- Double-Blocking Prevention: Ensuring funds aren't locked twice for the same transaction.
Sources: test/sandbox/README.md:10-17
Risk Management and Settlement
The system enforces operational constraints to simulate professional trading conditions:
- Auto Square-off: MIS (Intraday) positions are automatically closed based on configurable times (e.g., 15:15 for NSE/BSE).
- Settlement: CNC (Delivery) holdings follow T+1 settlement logic.
- Audit Trail: Every rejected order is stored with a clear rejection reason in the sandbox orderbook for debugging.
Sources: sandbox/__init__.py:8-17, test/sandbox/README.md:34-45, blueprints/sandbox.py:110-120
Summary
API Analyzer (Sandbox Mode) is a critical sub-system of OpenAlgo that allows developers and traders to stress-test algorithmic strategies against real market ticks without capital risk. By providing isolated databases, realistic leverage constraints, and automated square-off schedules, it serves as the final validation gate before live deployment.
Sources: sandbox/__init__.py:4-18, CLAUDE.md:104-106