Skip to content

Repository files navigation

IB Trader

A modular trading system for Interactive Brokers that allows you to implement and test trading strategies with ease.

Features

  • Modular Design: Easily add new trading strategies by implementing the BaseStrategy interface.
  • Broker Agnostic: Built with a broker-agnostic design, though currently focused on Interactive Brokers.
  • Example Strategy: Includes a moving average crossover strategy as a starting point.
  • Configuration Management: Uses YAML for easy configuration management.
  • Type Hints: Full Python type hints for better code quality and IDE support.

Prerequisites

  • Python 3.8+
  • Interactive Brokers TWS or IB Gateway installed and running
  • An Interactive Brokers account with API access enabled

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/ib-trader.git
    cd ib-trader
  2. Create and activate a virtual environment (recommended):

    python -m venv venv
    # On Windows:
    .\venv\Scripts\activate
    # On Unix or MacOS:
    source venv/bin/activate
  3. Install the package in development mode:

    pip install -e .
  4. Install development dependencies:

    pip install -r dev-requirements.txt

Configuration

  1. Copy the example configuration file:

    cp config/trading_config.yaml config/local_config.yaml
  2. Edit config/local_config.yaml with your Interactive Brokers connection details and trading parameters.

Running the Example Strategy

  1. Make sure Interactive Brokers TWS or IB Gateway is running and API connections are enabled.

  2. Run the example strategy:

    python -m ib_trader.main

Project Structure

ib-trader/
├── config/                    # Configuration files
│   ├── trading_config.yaml    # Example configuration
│   └── local_config.yaml      # Local configuration (gitignored)
├── ib_trader/                 # Main package
│   ├── brokers/               # Broker implementations
│   │   ├── __init__.py
│   │   ├── base_broker.py     # Base broker interface
│   │   └── interactive_brokers.py  # IB implementation
│   ├── strategies/            # Trading strategies
│   │   ├── __init__.py
│   │   ├── base_strategy.py   # Base strategy interface
│   │   └── example_strategy.py  # Example strategy
│   └── __init__.py
├── tests/                     # Unit tests
├── .gitignore
├── README.md
├── requirements.txt           # Production dependencies
├── dev-requirements.txt       # Development dependencies
└── setup.py                   # Package configuration

Creating a New Strategy

To create a new trading strategy:

  1. Create a new file in the ib_trader/strategies/ directory.
  2. Create a class that inherits from BaseStrategy.
  3. Implement all required methods from the base class.
  4. Add your strategy to ib_trader/strategies/__init__.py.
  5. Configure your strategy in the configuration file.

Testing

Run the test suite with:

pytest tests/

License

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This software is for educational purposes only. Use it at your own risk. The authors are not responsible for any trading losses you might incur. Always test your strategies thoroughly before using real money.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages