A comprehensive algorithmic trading system built with Python, supporting both backtesting and live trading through Interactive Brokers (IBKR) API.
- Backtesting Engine: Built on Backtrader framework for historical strategy testing
- Live Trading: Real-time trading through Interactive Brokers API using ib-insync
- Strategy Framework: Modular strategy implementation with SMA crossover example
- Configuration Management: YAML-based configuration for brokers and strategies
- Paper Trading: Safe testing environment before live deployment
my_test/
├── configs/ # Configuration files
│ └── broker.ib.yaml # IBKR connection and trading parameters
├── core/ # Core trading logic (planned)
├── data/ # Market data storage (planned)
├── engine/ # Trading engines
│ ├── backtest/ # Backtesting framework
│ │ ├── strategies/ # Trading strategies
│ │ │ └── sma.py # Simple Moving Average crossover strategy
│ │ └── run_backtest.py # Backtest execution script
│ └── live/ # Live trading
│ └── ib/ # Interactive Brokers integration
│ └── runner.py # Live trading runner with IBKR
├── research/ # Research and analysis (planned)
├── utils/ # Utility functions (planned)
├── ib_test.py # IBKR connection test script
└── idea.md # Project architecture documentation
- Python 3.8+
- Interactive Brokers TWS or IB Gateway
- Required Python packages:
pip install backtrader ib-insync yfinance pandas pyyaml- Install and configure TWS (Trader Workstation) or IB Gateway
- Enable API connections in TWS:
- Go to File → Global Configuration → API → Settings
- Enable "Enable ActiveX and Socket Clients"
- Add your IP address to trusted IPs
- For paper trading, use port 7497 (default in config)
- For live trading, use port 7496
python ib_test.pyThis will test your connection to IBKR and fetch AAPL market data.
python engine/backtest/run_backtest.pyThis runs a simple SMA crossover strategy on YINN using historical data from Yahoo Finance.
python engine/live/ib/runner.pyThis connects to IBKR and runs the SMA strategy with real market data.
mode: paper # paper | live
ib:
host: 127.0.0.1 # IBKR host
port: 7497 # TWS-Paper: 7497, Live: 7496
client_id: 1 # Unique client ID
symbols:
- YINN-STK-SMART-USD # Symbol format: TICKER-SECTYPE-EXCHANGE-CURRENCY
timeframe: minutes # minutes | daily
compression: 5 # Bar size (5 = 5-minute bars)
cash: 10000 # Starting capital
stake: 10 # Position size
commission: 0.001 # Commission rate (0.1%)Located in engine/backtest/strategies/sma.py, this strategy implements:
- Buy Signal: Fast SMA crosses above Slow SMA
- Sell Signal: Fast SMA crosses below Slow SMA
- Parameters:
fast: Fast SMA period (default: 10)slow: Slow SMA period (default: 20)
- Create a new strategy class in
engine/backtest/strategies/ - Inherit from
bt.Strategy - Implement
__init__()andnext()methods - Add strategy to the runner scripts
- Backtesting: Yahoo Finance via
yfinance - Live Trading: Interactive Brokers real-time data
- Planned: Support for multiple data providers
The system provides:
- Trade analysis (win rate, total trades)
- Portfolio performance metrics
- Sharpe ratio calculation
- Real-time trade notifications
Based on idea.md, the planned architecture includes:
- Enhanced Data Pipeline: Raw → Bronze → Silver data layers
- Research Framework: Jupyter notebooks with vectorbt integration
- Advanced Strategies: Pairs trading, mean reversion
- Risk Management: Position limits, stop-loss, circuit breakers
- Multi-Broker Support: Tiger Brokers integration
- CLI Interface: Unified command-line interface
- Comprehensive Testing: Unit tests for core logic
- Paper Trading: Always test strategies in paper mode first
- Position Sizing: Configurable stake sizes
- Commission Modeling: Realistic cost simulation
- Error Handling: Robust connection and data error handling
This project is for educational and research purposes. Use at your own risk in live trading environments.
- Fork the repository
- Create a feature branch
- Implement your changes
- Add tests if applicable
- Submit a pull request
This software is for educational purposes only. Trading involves substantial risk of loss. Past performance is not indicative of future results. Always test thoroughly in paper trading before deploying with real money.