Skip to content

mbmabiao/my_test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trading System

A comprehensive algorithmic trading system built with Python, supporting both backtesting and live trading through Interactive Brokers (IBKR) API.

🚀 Features

  • 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

📁 Project Structure

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

🛠️ Installation

Prerequisites

  • Python 3.8+
  • Interactive Brokers TWS or IB Gateway
  • Required Python packages:
pip install backtrader ib-insync yfinance pandas pyyaml

IBKR Setup

  1. Install and configure TWS (Trader Workstation) or IB Gateway
  2. 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
  3. For paper trading, use port 7497 (default in config)
  4. For live trading, use port 7496

🚦 Quick Start

1. Test IBKR Connection

python ib_test.py

This will test your connection to IBKR and fetch AAPL market data.

2. Run Backtest

python engine/backtest/run_backtest.py

This runs a simple SMA crossover strategy on YINN using historical data from Yahoo Finance.

3. Live Trading with IBKR

python engine/live/ib/runner.py

This connects to IBKR and runs the SMA strategy with real market data.

⚙️ Configuration

Broker Configuration (configs/broker.ib.yaml)

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%)

📊 Strategies

SMA Crossover Strategy

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)

🔧 Development

Adding New Strategies

  1. Create a new strategy class in engine/backtest/strategies/
  2. Inherit from bt.Strategy
  3. Implement __init__() and next() methods
  4. Add strategy to the runner scripts

Data Sources

  • Backtesting: Yahoo Finance via yfinance
  • Live Trading: Interactive Brokers real-time data
  • Planned: Support for multiple data providers

📈 Analysis & Reporting

The system provides:

  • Trade analysis (win rate, total trades)
  • Portfolio performance metrics
  • Sharpe ratio calculation
  • Real-time trade notifications

🎯 Roadmap

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

🔒 Risk Management

  • 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

📝 License

This project is for educational and research purposes. Use at your own risk in live trading environments.

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Implement your changes
  4. Add tests if applicable
  5. Submit a pull request

⚠️ Disclaimer

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors