Phase 1: Modular rule-based trading decision engine with IBKR integration
Rule-Based Trading System โ Phase 1
Modular rule-based trading decision engine integrated with IBKR. Phase 1 covers data ingestion, signal generation, and decision output via CLI with full logging. No live trades are executed in this phase.
Architecture
kingston-trading-engine/
โโโ config.py # All parameters (tickers, MA periods, RSI thresholds, etc.)
โโโ main.py # Entry point โ runs EOD evaluation and prints decisions
โโโ data/
โ โโโ market_data.py # Data layer โ fetches OHLCV via yfinance
โโโ signals/
โ โโโ signal_engine.py # Signal layer โ computes MAs, RSI, volume, breakout
โโโ decisions/
โ โโโ decision_engine.py # Decision layer โ applies market filter and entry rules
โโโ logger/
โ โโโ trade_logger.py # Logging layer โ writes signals and trades to dated log files
โโโ logs/ # Runtime log output (created automatically)
Modules
Data Layer (data/market_data.py)
Fetches daily OHLCV data for each ticker using yfinance. Configurable period and interval via config.py.
Signal Engine (signals/signal_engine.py)
Computes all technical indicators from raw OHLCV:
- 50-day, 200-day, 20-day simple moving averages
- RSI(14) using exponential smoothing
- 5-day high breakout level
- 20-day average volume
Decision Engine (decisions/decision_engine.py)
Applies entry rules per ticker after checking the market filter.
Market Filter โ evaluated on SPY:
- SPY price > 200-day MA
- SPY RSI > 50
- Price > 50-day MA
- 50-day MA > 200-day MA
- RSI between 55 and 65
- Price above 5-day high (breakout)
- Volume > 1.2x 20-day average
BUYโ all entry conditions passHOLDโ market filter passes but one or more entry conditions failBLOCKโ market filter fails (no trades for any ticker)
Logging Layer (logger/trade_logger.py)
Writes two dated log files to logs/: signals_YYYYMMDD.logโ full signal detail for every ticker evaluatedtrades_YYYYMMDD.logโ BUY signals and BLOCK events only
Configuration (config.py)
| Parameter | Default | Description | |---|---|---| | TICKERS | SPY, QQQ, AAPL, MSFT | Instruments to evaluate | | MARKETFILTERTICKER | SPY | Market regime filter ticker | | MA_50 | 50 | Fast MA period | | MA_200 | 200 | Trend MA period | | MA_20 | 20 | Exit MA period | | HIGH_LOOKBACK | 5 | Days for breakout level | | RSI_PERIOD | 14 | RSI lookback | | RSIENTRYLOW | 55 | RSI entry lower bound | | RSIENTRYHIGH | 65 | RSI entry upper bound | | RSIMARKETMIN | 50 | SPY RSI minimum for market filter | | VOLUME_MULTIPLIER | 1.2 | Volume surge threshold |
Setup
pip install -r requirements.txt
python main.py
Phase 2 (Upcoming)
- Risk engine: 1% risk per trade, max 3 open positions, daily loss limit (2 losses), 1-day cooldown
- IBKR paper execution via
ib_insync - Exit rule monitoring: MA20 cross, RSI < 50, 2% trailing stop, 2% hard stop
- Full trade lifecycle logging (entry, exit, blocked)