Autonomous, Multi-Threaded, Regime-Aware Trading System for the Indian Options Market
Engineered for Robustness, Adaptability, and Principled Risk Management.
Sentinel PRIME is an autonomous, multi-threaded, event-driven trading agent built for the Indian derivatives market. Designed for mission-critical financial operations, it dynamically adapts its strategies, confidence, and risk allocation based on a real-time, multi-layered comprehension of the market — prioritizing capital preservation, regime awareness, and robust execution.
The culmination of deep research in concurrency, adaptive state systems, and real-time quantitative risk management.
While the trading logic is complex, the true achievement of this system is its underlying distributed architecture:
- Bypassing the Python GIL: Engineered the Actor Model using daemon threads to decouple I/O-bound operations (broker APIs, SQLite writes) from the latency-sensitive execution loop, preventing the engine from freezing during network lag.
- Lock-Free Concurrency: Built a memory-safe, zero-latency data pipeline utilizing auto-trimming
collections.dequeand thread-safe queues for O(1) concurrent ingestion of real-time WebSocket tick data. - Custom Rate Limiting: Developed an asynchronous Token Bucket algorithm within the execution thread to govern API requests, preventing ban-triggers during massive market volatility bursts.
- Atomic State Persistence: Isolated SQLite database operations to a single worker thread, ensuring crash-safe ACID compliance without risking thread deadlocks in the main engine.
Even within the constraints of Python, the decoupled Planner-Executor architecture is profiled using cProfile and py-spy to minimize execution lag under heavy market conditions:
- WebSocket Ingestion Latency:
< 2.5ms(time from raw socket frame arrival to thread-safePriceBusqueue commit). - Signal Generation Latency (Planner Loop):
~10 - 25ms(time to evaluate multi-regime confluence logic and compute sizing cascades). - Execution Reaction Latency (Executor Loop):
< 1.5ms(time from queue retrieval to dispatching order payload to broker API). - Memory Footprint: Stabilized at
~200MB RAMunder continuous load, due to aggressive ring-buffer trimming (collections.deque(maxlen=1000)).
Sentinel PRIME's decision engine is not monolithic. It employs a sophisticated three-stage "Alpha Funnel" to distill market noise into high-probability, optimally-sized trading opportunities.
-
Stage 1: REGIME CLASSIFICATION (The "Playbook")
- Objective: Understand the market's current personality.
- Mechanism: A dedicated
RegimeClassifieranalyzes index futures (NIFTY, BANKNIFTY) and VIX, leveraging indicators like ADX, Bollinger Band Width Percentile Rank, and EMA crosses. It classifies the market into distinct regimes (TRENDING_UP,COMPRESSION,CHOP,CHAOS) using hysteresis to ensure stability. - Outcome: The active regime dictates which strategic "playbook" (e.g.,
TrendPullback,MomentumBreakout) is eligible for signal generation, ensuring the right tool is used for the current market context.
-
Stage 2: CONFLUENCE SCORING (The "Confidence")
- Objective: Validate raw signals and filter noise.
- Mechanism: Signals generated by the active playbook are not trusted blindly. They are rigorously scored by a Confluence Engine (
_score_and_size_trade) against multiple, uncorrelated factors to quantify the probability of success. - Confluence Factors:
- 📈 Regime Appropriateness: Is the strategy suited for the current regime? (
+1.0/-1.0) - 📊 Market Breadth: Does the Nifty 50 Advance/Decline ratio confirm the signal's direction? (
+1.0/-1.0) - 📉 Microstructure: Do Order Book Imbalance (OBI) and Trade Flow Intensity (TFI) provide micro-confirmation? (
+0.5/-1.0each) - 🌋 Volatility Structure: Is Implied Volatility (IV) "coiled" below Historical Volatility (HV), suggesting potential expansion? (
+1.0) Or is premium excessively expensive? (-0.5)
- 📈 Regime Appropriateness: Is the strategy suited for the current regime? (
- Outcome: Only signals achieving a configurable
min_trade_scoreproceed. Low-probability setups are discarded.
-
Stage 3: DYNAMIC SIZING (The "Conviction")
- Objective: Allocate capital proportional to conviction and risk.
- Mechanism: Position size is never fixed. A "Sizing Cascade" (
RiskManager.calculate_position_size) determines the optimal lot size based on the system's conviction in the specific trade and the prevailing risk environment. - Sizing Inputs:
- ✨ Signal Confidence: The
final_score(Stage 2). Higher scores = larger size. - 💡 Strategy Performance: A dynamic
strategy_weightbased on the recent P&L of the specific strategy firing the signal. - 🧭 Regime Confidence: The numerical confidence score from the
RegimeClassifier. Lower confidence = smaller size. - 🌪️ Market Risk: VIX levels, time-of-day multipliers, and expiry-day risk reduction factors.
- 🏦 Account Risk: A core
risk_factoradjusted based on recent performance (consecutive losses).
- ✨ Signal Confidence: The
- Outcome: Capital is dynamically concentrated on high-probability opportunities generated by currently effective strategies within clear market regimes, while risk is aggressively curtailed during periods of uncertainty or poor performance.
| Feature | Description | Advantage |
|---|---|---|
| 🧭 Multi-Regime Logic | Dynamically loads/unloads strategies based on market personality (TRENDING, CHOP, COMPRESSION, CHAOS). |
Ensures the system always uses the appropriate tactical approach. |
| ✨ Confluence Engine | Scores signals against Breadth, Microstructure (TFI/OBI), and Volatility Structure before execution. | Filters market noise, dramatically increasing the probability of executed trades. |
| ⚖️ Dynamic "Sizing Cascade" | No fixed lots. Size based on Signal Score, Strategy P&L, Regime Confidence, VIX, Time-of-Day, & Drawdown. | Bets bigger on conviction, scales back automatically when uncertain or losing. |
| ⚡ Advanced Reflexes | Includes Velocity_Trigger (pre-emptive exit on sharp drops) & dual-mode Trailing Stop (Chandelier ATR + R:R stages). |
Protects capital from "knife catches" and maximizes profit capture on winning trades. |
| 🧵 Planner-Executor Arch. | Decouples heavy analysis (Planner, 5s loop) from execution (Executor, real-time) via a queue. | Guarantees low-latency execution and prevents analysis lag from blocking critical actions. |
| 🧪 High-Fidelity Paper Sim | Realistic 3-stage adaptive entry (BID→MID→ASK) with probabilistic fills based on config (paper_fill_bid_chance). |
Provides far more trustworthy backtesting and forward-testing results than naive fills. |
| ⚙️ Systemic Robustness | Includes self-healing (reconcile), thread monitoring (health_check), atomic DB persistence, stale feed detection, and graceful shutdowns. |
Engineered for high uptime and resilience against common infrastructure/data failures. |
| 🛡️ Multi-Layered Risk | Enforces Portfolio Greek limits (Delta/Vega), Hard Daily/Weekly Drawdown circuit breakers, and a specialized Theta Filter (halts buying in low-IV chop). | Provides comprehensive capital protection at portfolio, account, and environmental levels. |
| 📊 Full Observability | Integrated Prometheus metrics server exports dozens of real-time KPIs (PnL, Drawdown, Regime, Greeks, Latency) for Grafana monitoring. Includes Telegram alerts. | Enables professional, quantitative monitoring of system health and performance. |
Sentinel PRIME employs a Planner-Executor model, isolating complex decision-making from time-sensitive execution via a thread-safe queue. This prevents analysis bottlenecks from impacting reaction speed and enhances stability. While the diagram below simplifies the orchestration for clarity, note that core components like the StrategicPlanner (_run_strategic_planner task) and PositionManager are managed and scheduled by the central Engine.
%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1f2023', 'primaryTextColor': '#f0f0f0', 'lineColor': '#a0a0a0', 'secondaryColor': '#2a2a2e'}, 'themeCSS': '.mermaid svg { max-width: 800px; margin: 0 auto; }'}}%%
graph TD
subgraph Input_Perception["Input & Perception"]
A["Market Data Feed (WebSocket)"] --> B["PriceBus & State Stream"]
B --> C{"Current Market State"}
end
subgraph Decision_Engine["Decision Engine (Planner – 5s Loop)"]
D["Regime Classifier"] --> E["Strategy Selector"]
F["Confluence Engine"] --> G["Signal Validation & Scoring"]
E --> G
G --> H["Dynamic Sizing Engine"]
H --> I["Validated & Sized Trade Plan"]
end
subgraph Execution_Management["Execution & Management (Real-Time – 1s Loop)"]
J["Trade Executor"] --> K["Broker API"]
K --> L["Position Manager & Risk Control"]
L --> K
end
subgraph Monitoring["Monitoring & Telemetry"]
M["Observability (Prometheus, Logs, Alerts)"]
end
%% Connections between Stages
C --> D
C --> F
C --> H
C --> L
I --> J
%% Connections to Monitoring
B --> M
G --> M
H --> M
L --> M
%% Styling
style A fill:#0077c8,stroke:#fff
style K fill:#0077c8,stroke:#fff
style B fill:#3f51b5,stroke:#fff
style C fill:#fdd835,stroke:#333,color:#333
style D fill:#673ab7,stroke:#fff
style E fill:#673ab7,stroke:#fff
style F fill:#009688,stroke:#fff
style G fill:#00bcd4,stroke:#fff
style H fill:#fb8c00,stroke:#fff
style I fill:#e65100,stroke:#fff,stroke-width:2px,color:#fff
style J fill:#f44336,stroke:#fff
style L fill:#9c27b0,stroke:#fff
style M fill:#e6522c,stroke:#fff
- 🧠
Engine(The Maestro): Central coordinator, manages state, runs the scheduler. - ⚡
PriceBus(Nerves): Dedicated WebSocket handler, queues incoming data. - 🧭
StrategicPlanner(Brain - 5s Loop): Executes the Alpha Funnel (Regime → Confluence → Sizing), produces validated trade signals. - 🏃
PositionManager(Reflexes - 1s Loop): High-frequency manager for open positions (trailing stops, scaling, velocity checks). - 🏦
RiskManager(Accountant): Central authority for sizing calculations, drawdown limits, and portfolio risk checks. - 🖐️
Trader(Hands): Abstracts broker interactions, ensures safe order modifications ("Cancel-Modify-Replace"). - 💾
Store(Memory): SQLite persistence layer for state recovery and trade logging. - 👁️
MicroMonitor: Analyzes tick data for TFI and OBI confluence signals.
Risk control is paramount, embedded at every stage of the system's operation.
-
Portfolio Level (Pre-Trade):
- Control: Enforces
max_portfolio_net_delta&max_portfolio_net_vegalimits. - Mechanism:
RiskManager.risk_ok()blocks trades violating portfolio constraints.
- Control: Enforces
-
Drawdown Level (Circuit Breaker):
- Control: Hard
max_daily_drawdown_pct& softweekly_drawdown_pct_limit. - Mechanism:
RiskManager.risk_ok()triggersself.halt_trading = True, liquidates positions (daily), or forcesdefensivemode (weekly).
- Control: Hard
-
Environmental Level (Filter Halts):
- Control: Theta Filter (blocks buying in low-IV
CHOP), Stale Feed Check. - Mechanism: Specific conditions within
StrategicPlannerorhealth_checkcan setself.halt_trading = True.
- Control: Theta Filter (blocks buying in low-IV
-
Capital Level (Dynamic Sizing):
- Control: The "Sizing Cascade" adjusts size based on confidence, performance, and market conditions.
- Mechanism:
RiskManager.calculate_position_size()output varies significantly based on internal and external factors.
-
Trade Level (In-Flight Reflexes):
- Control:
Velocity_Trigger, Dual-Mode Trailing Stop (Chandelier + R:R). - Mechanism:
PositionManager's 1-second loop constantly evaluates price action against adaptive risk thresholds for open positions.
- Control:
-
System Level (Integrity & Recovery):
- Control: Atomic DB writes, Startup Checks,
reconciletask,health_checkthread monitoring. - Mechanism: Prevents state corruption, auto-flattens rogue positions, ensures component liveness, halts safely on critical failures.
- Control: Atomic DB writes, Startup Checks,
- Core Language: Python 3.10+
- Broker API: Zerodha Kite Connect v3 (
kiteconnectlibrary) - Data Analysis:
pandas,numpy,scipy - Technical Indicators:
pandas-ta - Persistence:
sqlite3(via Python's built-in module) - Observability:
prometheus_client,flask,waitress - Concurrency: Python
threading,queue - Utilities:
python-dotenv,pytz,pandas_market_calendars,requests
-
Prerequisites:
- Python 3.10 or higher installed.
- A Zerodha Kite developer account with API access enabled.
- Git installed.
-
Clone & Setup:
# Clone the repository (**Replace YOUR_USERNAME/sentinel-prime**) git clone [https://github.com/YOUR_USERNAME/sentinel-prime.git](https://github.com/YOUR_USERNAME/sentinel-prime.git) cd sentinel-prime # Create and activate a virtual environment (recommended) python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate` # Install dependencies pip install -r requirements.txt
-
Environment Configuration:
- Copy
.env.exampleto.env. - Edit
.envand securely add your:KITE_API_KEYKITE_API_SECRETACCOUNT_EQUITY(Initial equity for paper trading/drawdown calcs)- (Optional)
TELEGRAM_BOT_TOKENandTELEGRAM_CHAT_IDfor alerts.
- Copy
-
System Configuration (
config.json):- Crucially, review
config.json. This defines the system's "personality":- Set
trading.paper_trading(truefor paper,falsefor live - USE EXTREME CAUTION). - Adjust
risk_tiers, drawdown limits (max_daily_drawdown_pct). - Tune strategy parameters (
MomentumBreakout,TrendPullback, etc.). - Configure filters (
strike_selection,option_selection_filters). - Set timing parameters (
market_open,final_entry_time).
- Set
- Crucially, review
-
First-Time Authentication: The system requires Kite Connect authentication. The first time you run it, it will guide you through the process:
python main1.py
- Follow the console prompts: Open the login URL -> Log in -> Copy the full redirect URL -> Paste it back into the console.
- A token file (
persist_sentinel_prime/kite_token.json) will be created for subsequent runs.
-
Running Sentinel PRIME:
# Ensure virtual environment is active source venv/bin/activate # Run the main script python main1.py
- The system will initialize, warm up data, connect, and begin operation according to the market timings in
config.json. - Use
Ctrl+Cfor a graceful shutdown (attempts to close open positions).
- The system will initialize, warm up data, connect, and begin operation according to the market timings in
This software is provided "AS IS" for educational and research purposes ONLY. It is NOT financial advice.
Trading financial instruments, particularly derivatives like options, involves SUBSTANTIAL RISK OF LOSS and is not suitable for all investors. You can lose more than your initial investment.
The developers and contributors of Sentinel PRIME assume NO RESPONSIBILITY for any trading losses incurred using this software. You are SOLELY RESPONSIBLE for your trading decisions, risk management, and understanding the code before deployment.
DO NOT DEPLOY THIS SYSTEM WITH REAL CAPITAL UNLESS YOU FULLY UNDERSTAND ITS MECHANICS, HAVE THOROUGHLY TESTED IT, AND ARE AWARE OF THE SIGNIFICANT RISKS INVOLVED. Always start with paper trading.