Skip to content

ChengruiMa/quantProject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quantitative Trading System for Interactive Brokers (IBKR)

A modular end-to-end Python system for developing, backtesting, and executing quantitative trading strategies using the Interactive Brokers (IBKR) TWS API.

The project is divided into two main components:

  • framework/ – A full-featured trading engine that manages IB Gateway connectivity, handles simulated and IBKR-based backtesting, and supports both paper and live trading modes.
  • quantStrat/ – A strategy library that implements reusable trading strategies (e.g., Moving Average, Mean Reversion, Momentum, and Options) with QuantLib-based analytics and signal aggregation.

Together, these modules combines quantitative research, testing, and real trading execution in one unified environment.

Overview

This project is meant to record and implement some of the common quantitative strategies that I've learned (therefore the quantStrat folder will keep on expanding), to check out these strategies' performances on real datasets across times for different stocks, and to play around with my potential ideas.

It is also helpful for traders, researchers, and developers who want to:

  • Build and test trading strategies quickly
  • Seamlessly move from simulation → IBKR backtest → paper → live trading
  • Integrate multiple trading signals into one consensus decision
  • Automate the IB Gateway lifecycle

Each strategy in this project is independent and can be easily merged with the framework’s backtesting or live trading engine.

Project Structure

.
├── framework/               # IBKR connectivity, trading modes, backtesting engines
│   ├── config.py
│   ├── ib_connector.py
│   ├── ib_gateway_manager.py
│   ├── strategy_base.py
│   ├── live_runner.py
│   ├── backtest_runner.py
│   ├── test_strat.py
│   └── README.md
│
├── quantStrat/              # Strategy implementations and signal management
│   ├── moving_average_strategy.py
│   ├── mean_reversion_strategy.py
│   ├── momentum_strategy.py
│   ├── black_scholes_strategy.py
│   ├── strategy_manager.py
│   ├── base_strategy.py
│   └── README.md
│
└── main.py                  # Optional adapter integrating both modules

Installation

Prerequisites

  • Python ≥ 3.8
  • IBKR TWS or IB Gateway (Standalone version recommended)
  • IBKR Account (paper or live)

Install Dependencies

pip install pandas numpy ibapi QuantLib

To install IBKR’s official API library:

# Download TWS API from https://interactivebrokers.github.io/
cd /path/to/TWS_API/source/pythonclient
python setup.py install

Environment Setup

Create a .env file inside the framework/ directory:

IB_USERNAME=your_username
IB_PASSWORD=your_password
IB_HOST=127.0.0.1
IB_PAPER_PORT=4002
IB_LIVE_PORT=4001
IB_CLIENT_ID=1

The framework automatically loads this configuration when connecting to IBKR (Note: manual intervention is sometimes required as IBKR tries to block auto log-ins for security reasons)

Core Components

1. Framework (framework/)

The Quantitative Trading Framework manages:

  • IBKR Connectivity: via ib_connector.py
  • Gateway Management: automated start/stop of IB Gateway
  • Trading Modes: simulated, IBKR backtest, paper, and live
  • Backtest Engine: modular, with pluggable data providers
  • Execution Layer: live and paper trade execution loops

You can run the standalone framework demo:

cd framework
python3 test_strat.py

You’ll be prompted to choose between:

  1. Simulated Backtest
  2. IBKR Backtest
  3. Paper Trading
  4. Live Trading

Each mode automatically handles connection setup and resource management.

2. Strategy Library (quantStrat/)

Implements the common quantitative strategies for different market behaviors. Each strategy can run independently or be combined via the StrategyManager.

Included Strategies

Strategy Core Idea Key Parameters
Moving Average Crossover Short-term MA crosses long-term MA short_window, long_window
Mean Reversion Bollinger Bands overbought/oversold window, num_std
Trend Momentum RSI + MACD confirmation rsi_period, macd_fast, macd_slow
Black-Scholes Options Mispricing vs theoretical value risk_free_rate, dividend_yield

Example:

from strategy_manager import StrategyManager
from moving_average_strategy import MovingAverageCrossover
from momentum_strategy import TrendMomentum

manager = StrategyManager()
manager.add_strategy(MovingAverageCrossover(20, 50))
manager.add_strategy(TrendMomentum(rsi_period=14))

signal = manager.get_consensus_signal(data)
print(signal.signal.name, signal.confidence)

Example Integration

Below is an example main.py integrating both modules:

from quantStrat.strategy_manager import StrategyManager
from quantStrat.moving_average_strategy import MovingAverageCrossover
from quantStrat.momentum_strategy import TrendMomentum
from framework.test_strat import run_simulated_backtest

# Initialize strategies
manager = StrategyManager()
manager.add_strategy(MovingAverageCrossover(short_window=20, long_window=50))
manager.add_strategy(TrendMomentum(rsi_period=14))

# Run a simulated backtest (no IBKR required)
run_simulated_backtest(manager, symbol="AAPL", periods=200, start_date="2024-01-01")

To switch to an IBKR historical backtest or live mode:

from framework.test_strat import run_ibkr_backtest, run_live_trading
# Requires gateway running and manual login
run_ibkr_backtest(manager, symbol="AAPL", duration="6 M", bar_size="1 day")

Supported Trading Modes

Mode Description Data Source IBKR Required
1. Simulated Backtest Fast prototype testing with synthetic data Local simulation
2. IBKR Backtest Real historical data via IB API IBKR historical data
3. Paper Trading Real-time simulation on IBKR paper account Live market
4. Live Trading Actual capital deployment Live market

🔗 Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages