Simulation & Backtesting
Signal.Engine utilizes a dual-layered validation framework to ensure strategy robustness before capital deployment. This includes Historical Backtesting (testing the model against past data) and the Real-Time Simulation Engine (a virtual environment that mirrors live market conditions).
📊 Historical Backtesting
Backtesting is used to evaluate the LSTM Predictor’s accuracy and the agent's historical profitability. The system processes historical candles in windows (default: 50) to generate hindsight-based performance reports.
Running a Backtest
You can trigger a backtest via the API or the CLI. The system evaluates the agent's performance on a specific ticker (e.g., AAPL or RELIANCE.NS) and generates a performance chart.
CLI Usage:
python -m src.backtest --ticker AAPL --days 365
API Usage:
GET /backtest
- Response:
{"status": "success", "message": "Backtest complete."} - Side Effect: Generates a
backtest_chart.pngin the static assets directory showing cumulative returns vs. Buy & Hold.
Validation Metrics
The backtester tracks several key performance indicators (KPIs):
- Directional Accuracy: The percentage of correct "UP/DOWN/NEUTRAL" predictions.
- Confidence Thresholding: Filters results to show how the model performs when its confidence score is $>80%$.
- Log Returns: Comparison of predicted log returns vs. actual price movement.
🎮 Real-Time Simulation Engine
The SimulationEngine (located in src/simulation/engine.py) provides a "Paper Trading" environment that does not require an Alpaca API key. It is integrated directly into the HybridBrain loop to track how the RL (PPO) agent manages risk in a live data stream.
Core Component: SimulationEngine
The engine maintains an internal virtual portfolio. Every time the Brain "thinks," the engine processes the resulting decisions and updates the virtual state.
Key Methods (Internal API)
While used primarily by the system backend, these methods define the simulation logic:
process_tick(decisions, regime): Executes virtual trades based on agent output. It considers the current market "regime" (e.g., HIGH_VOLATILITY) to adjust slippage and risk.get_portfolio(): Returns the current state of the virtual account.reset(): Wipes the simulation history to start a fresh tracking session.
🌐 Simulation API Reference
The frontend dashboard communicates with the simulation engine via the following endpoints:
1. Get Simulation State
Fetches the current virtual balance, open positions, and performance logs.
- Endpoint:
GET /api/simulation/state - Output:
{ "balance": 10000.00, "equity": 10250.45, "positions": [], "status": "ACTIVE", "pnl_pct": 2.5 }
2. Reset Simulation
Clears all virtual holdings and restores the starting balance. Useful for re-testing the agent after a model update.
- Endpoint:
POST /api/simulation/reset - Response: Returns the initial state of the simulation.
3. Fetch Results & Logs
Retrieves the latest decisions made by the Brain along with the simulation's reaction to those decisions.
- Endpoint:
GET /api/results - Output Fields:
data: List of ticker decisions (Action, Confidence, Rational).simulation: The updated portfolio state.logs: A list of execution logs (e.g., "Virtual Buy RELIANCE.NS at 2500.00").
🛡️ Risk Modeling in Simulation
The simulation engine isn't just a ledger; it applies Quantitative Risk Modeling to every trade:
- Confidence Weighting: Trades with higher confidence scores ($>0.85$) are assigned larger virtual position sizes.
- Regime Adaptation: If the Brain detects "High Volatility," the simulation engine increases the simulated spread, reflecting the difficulty of executing trades in turbulent markets.
- Drawdown Tracking: Tracks the
MaxDrawdownandVaR95(Value at Risk) to ensure the RL agent's risk management layer is functioning correctly before moving totrader_alpaca.py.