Autonomous, multi-threaded options trading engine. Bypasses the Python GIL via the Actor Model for lock-free data ingestion and low-latency execution on the Zerodha Kite API.
0
stars
28
commits
Python
primary language
Sep 7, 2026
updated
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:
collections.deque and thread-safe queues for O(1) concurrent ingestion of real-time WebSocket tick data.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:
< 2.5ms (time from raw socket frame arrival to thread-safe PriceBus queue commit).~10 - 25ms (time to evaluate multi-regime confluence logic and compute sizing cascades).< 1.5ms (time from queue retrieval to dispatching order payload to broker API).~200MB RAM under 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")
RegimeClassifier analyzes 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.TrendPullback, MomentumBreakout) is eligible for signal generation, ensuring the right tool is used for the current market context.Stage 2: CONFLUENCE SCORING (The "Confidence")
_score_and_size_trade) against multiple, uncorrelated factors to quantify the probability of success.+1.0 / -1.0)+1.0 / -1.0)+0.5 / -1.0 each)+1.0) Or is premium excessively expensive? (-0.5)min_trade_score proceed. Low-probability setups are discarded.Stage 3: DYNAMIC SIZING (The "Conviction")
RiskManager.calculate_position_size) determines the optimal lot size based on the system's conviction in the specific trade and the prevailing risk environment.final_score (Stage 2). Higher scores = larger size.strategy_weight based on the recent P&L of the specific strategy firing the signal.RegimeClassifier. Lower confidence = smaller size.risk_factor adjusted based on recent performance (consecutive losses).| 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):
max_portfolio_net_delta & max_portfolio_net_vega limits.RiskManager.risk_ok() blocks trades violating portfolio constraints.Drawdown Level (Circuit Breaker):
max_daily_drawdown_pct & soft weekly_drawdown_pct_limit.RiskManager.risk_ok() triggers self.halt_trading = True, liquidates positions (daily), or forces defensive mode (weekly).Environmental Level (Filter Halts):
CHOP), Stale Feed Check.StrategicPlanner or health_check can set self.halt_trading = True.Capital Level (Dynamic Sizing):
RiskManager.calculate_position_size() output varies significantly based on internal and external factors.Trade Level (In-Flight Reflexes):
Velocity_Trigger, Dual-Mode Trailing Stop (Chandelier + R:R).PositionManager's 1-second loop constantly evaluates price action against adaptive risk thresholds for open positions.System Level (Integrity & Recovery):
reconcile task, health_check thread monitoring.kiteconnect library)pandas, numpy, scipypandas-tasqlite3 (via Python's built-in module)prometheus_client, flask, waitressthreading, queuepython-dotenv, pytz, pandas_market_calendars, requestsPrerequisites:
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:
.env.example to .env..env and securely add your:
KITE_API_KEYKITE_API_SECRETACCOUNT_EQUITY (Initial equity for paper trading/drawdown calcs)TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID for alerts.System Configuration (config.json):
config.json. This defines the system's "personality":
trading.paper_trading (true for paper, false for live - USE EXTREME CAUTION).risk_tiers, drawdown limits (max_daily_drawdown_pct).MomentumBreakout, TrendPullback, etc.).strike_selection, option_selection_filters).market_open, final_entry_time).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
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
config.json.Ctrl+C for a graceful shutdown (attempts to close open positions).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.
26 commits
2 commits
Python
100.0%
Autonomous, multi-threaded options trading engine. Bypasses the Python GIL via the Actor Model for lock-free data ingestion and low-latency execution on the Zerodha Kite API.
0
stars
28
commits
Python
primary language
Sep 7, 2026
updated
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:
collections.deque and thread-safe queues for O(1) concurrent ingestion of real-time WebSocket tick data.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:
< 2.5ms (time from raw socket frame arrival to thread-safe PriceBus queue commit).~10 - 25ms (time to evaluate multi-regime confluence logic and compute sizing cascades).< 1.5ms (time from queue retrieval to dispatching order payload to broker API).~200MB RAM under 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")
RegimeClassifier analyzes 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.TrendPullback, MomentumBreakout) is eligible for signal generation, ensuring the right tool is used for the current market context.Stage 2: CONFLUENCE SCORING (The "Confidence")
_score_and_size_trade) against multiple, uncorrelated factors to quantify the probability of success.+1.0 / -1.0)+1.0 / -1.0)+0.5 / -1.0 each)+1.0) Or is premium excessively expensive? (-0.5)min_trade_score proceed. Low-probability setups are discarded.Stage 3: DYNAMIC SIZING (The "Conviction")
RiskManager.calculate_position_size) determines the optimal lot size based on the system's conviction in the specific trade and the prevailing risk environment.final_score (Stage 2). Higher scores = larger size.strategy_weight based on the recent P&L of the specific strategy firing the signal.RegimeClassifier. Lower confidence = smaller size.risk_factor adjusted based on recent performance (consecutive losses).| 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):
max_portfolio_net_delta & max_portfolio_net_vega limits.RiskManager.risk_ok() blocks trades violating portfolio constraints.Drawdown Level (Circuit Breaker):
max_daily_drawdown_pct & soft weekly_drawdown_pct_limit.RiskManager.risk_ok() triggers self.halt_trading = True, liquidates positions (daily), or forces defensive mode (weekly).Environmental Level (Filter Halts):
CHOP), Stale Feed Check.StrategicPlanner or health_check can set self.halt_trading = True.Capital Level (Dynamic Sizing):
RiskManager.calculate_position_size() output varies significantly based on internal and external factors.Trade Level (In-Flight Reflexes):
Velocity_Trigger, Dual-Mode Trailing Stop (Chandelier + R:R).PositionManager's 1-second loop constantly evaluates price action against adaptive risk thresholds for open positions.System Level (Integrity & Recovery):
reconcile task, health_check thread monitoring.kiteconnect library)pandas, numpy, scipypandas-tasqlite3 (via Python's built-in module)prometheus_client, flask, waitressthreading, queuepython-dotenv, pytz, pandas_market_calendars, requestsPrerequisites:
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:
.env.example to .env..env and securely add your:
KITE_API_KEYKITE_API_SECRETACCOUNT_EQUITY (Initial equity for paper trading/drawdown calcs)TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID for alerts.System Configuration (config.json):
config.json. This defines the system's "personality":
trading.paper_trading (true for paper, false for live - USE EXTREME CAUTION).risk_tiers, drawdown limits (max_daily_drawdown_pct).MomentumBreakout, TrendPullback, etc.).strike_selection, option_selection_filters).market_open, final_entry_time).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
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
config.json.Ctrl+C for a graceful shutdown (attempts to close open positions).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.
26 commits
2 commits
Python
100.0%