This project is a modular framework for developing, backtesting, and deploying quantitative trading strategies using Python. It is designed to simulate trading logic as realistically as possible, with a focus on market-generated information and advanced risk management techniques. The platform has evolved from a basic concept to a robust engine capable of running complex, multi-day backtests.
The core backtesting engine is fully functional and stable. The primary focus has been the development and iterative refinement of the OpenRejectionReverseStrategy, a sophisticated day-trading strategy inspired by the principles in James Dalton's "Mind Over Markets".
- Event-Driven Backtesting Engine: The engine operates on a bar-by-bar basis, preventing lookahead bias and closely simulating a live trading environment. It now uses a pre-calculated trading calendar to reliably handle weekends and holidays.
- Realistic Cash Settlement: The
BacktestLedgerfully simulates a T+1 cash settlement environment, where proceeds from sales are not available for trading until the next session. This is critical for accurately testing strategies in a cash account. - Configurable Strategies: All strategy parameters are externalized to
config.yaml, allowing for easy tuning and experimentation without altering the source code. - Advanced Analytics: The platform integrates
VolumeProfilerandMarketProfilerto provide strategies with market-generated context, such as Point of Control (POC) and Value Area (VA). - Sophisticated Risk Management: The active strategy employs a multi-layered position sizing model that considers:
- A percentage of total equity to define the maximum risk per trade.
- A maximum allocation cap to prevent over-concentration in a single position.
- A final check against available settled cash, scaling the position down if necessary.
- Dynamic Trade Management: The strategy includes advanced logic for managing open positions, such as a dynamic trailing stop-loss that uses the migrating Point of Control (POC) of the intraday trend.
- Detailed Performance Reporting: At the end of each run, the
BacktestResultsmodule calculates and displays a full suite of performance metrics (PnL, Win Rate, Profit Factor, Max Drawdown, etc.) and plots the equity curve. - Extensive Logging: The engine and strategies produce detailed logs, which have been instrumental in diagnosing and fixing complex logical issues.
The platform maintains a modular design to separate concerns:
- /analytics: Contains reusable tools for market analysis (
VolumeProfiler,MarketProfiler,VWAP). - /backtest: Houses the core backtesting engine (
engine.py) and results analysis tools (results.py). - /core: Contains essential components like the
BacktestLedgerandBaseFeeModel. - /strategies: Location for all trading strategy logic. Each strategy inherits from
BaseStrategy. /logs: Output directory for the detailed strategy and engine logs.config.yaml: A central configuration file for managing all backtest, strategy, and fee parameters.
The primary strategy developed on this platform is a long-only, day-trading strategy that operates in three phases:
- The Scan (Pre-Market): Analyzes the previous day's data to find stocks that closed with strong volume and momentum, creating a daily watchlist.
- The Setup (Market Open): Confirms that a candidate has opened above the prior day's value area, indicating a continuation of bullish sentiment.
- The Trigger (Intraday): Waits for a specific pattern where the price dips below the open and then rallies back through the opening bar's high. This pattern serves as the entry trigger, with a stop-loss placed at the low of the dip.
- Install Dependencies:
pip install -r requirements.txt
- Add Data: Ensure your 1-minute historical data files are in the directory specified in
config.yaml(default:/data/historical/). - Configure the Test: Edit
config.yamlto select the active strategy (name) and set its corresponding parameters. - Run the Engine: Execute the main script from the project root. You will be prompted for a start and end date for the backtest.
python main.py
- Strategy Refinement: Continue to test and refine the
OpenRejectionReverseStrategybased on backtest results. - New Strategies: Implement other strategies based on "Mind Over Markets" or different trading concepts.
- Live Trading Integration: Build out the components necessary to connect the strategy logic to a live paper or real money account via the Interactive Brokers API.
- Portfolio-Level Analysis: Enhance the results module to aggregate statistics across multiple backtest runs and provide portfolio-level insights.