Skip to content

quantitative pairs trading app based on Streamlit for connection to TWS from Interactive Brokers

License

Notifications You must be signed in to change notification settings

a3igner/TWSPairsTradingApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TWS Pairs Trading Application

A sophisticated Python application for automated pairs trading using Interactive Brokers TWS (Trader Workstation) API. This application provides a user-friendly interface for analyzing and executing pairs trading strategies, with real-time market data monitoring and statistical analysis tools.

Screenshots

Main Application Interface

App Screenshot

Help Section

Help Section

Features

  • Real-time market data streaming from TWS
  • Statistical analysis of trading pairs (correlation, cointegration)
  • Trading signals based on spread z-score
  • Interactive charts and metrics dashboard
  • Support for multiple trading pairs
  • Real-time performance monitoring
  • Customizable trading parameters
  • Comprehensive error handling

Requirements

  • Python 3.9+
  • Interactive Brokers TWS/Gateway running locally
  • TWS API configured for READ-ONLY mode on port 7497
  • Required Python packages (see requirements.txt)

Installation

Method 1: Using pip

  1. Create a virtual environment:
python -m venv venv
  1. Activate the virtual environment:
source venv/bin/activate  # On Unix/macOS
venv\Scripts\activate     # On Windows
  1. Install dependencies:
pip install -r requirements.txt

Method 2: Development Installation

For development purposes, you can install the package in editable mode:

pip install -e .

This will install the package in development mode using setup.py, allowing you to modify the code and see changes immediately.

Usage

  1. Ensure TWS is running and configured:

    • TWS must be running in READ-ONLY mode
    • API port should be set to 7497
    • Enable API connections in TWS settings
  2. Run the application:

python run.py
  1. Open your browser and navigate to the displayed URL (typically http://localhost:8501)

  2. Using the application:

    • Connect to TWS using the sidebar button
    • Select your trading pair (default: NVDA-TSM)
    • Monitor real-time price movements and spread
    • View key metrics like correlation and z-score
    • Watch for trading signals based on spread divergence

Configuration

The application settings can be configured in src/config/settings.py:

  • TWS connection parameters
  • Trading pairs configuration
  • Strategy parameters (z-score threshold, lookback period)
  • UI settings and theme colors

Project Structure

TWSPairsTradingApp/
├── src/
│   ├── config/
│   │   ├── __init__.py
│   │   └── settings.py
│   ├── core/
│   │   ├── __init__.py
│   │   ├── tws_connection.py
│   │   └── pair_analyzer.py
│   ├── ui/
│   │   ├── __init__.py
│   │   └── app.py
│   └── __init__.py
├── requirements.txt
├── setup.py
├── README.md
└── run.py

Development

Setting Up Development Environment

  1. Clone the repository:
git clone https://github.com/yourusername/TWSPairsTradingApp.git
cd TWSPairsTradingApp
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Unix/macOS
  1. Install in development mode:
pip install -e .

Running Tests

python -m pytest tests/

Error Handling

  • The application includes comprehensive error handling for TWS connection issues
  • Failed data fetches are logged and reported in the UI
  • Connection status is always visible in the sidebar
  • Automatic reconnection attempts on connection loss
  • Detailed error logging for troubleshooting

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Create a Pull Request

License

MIT License

Acknowledgments

  • Interactive Brokers TWS API
  • Streamlit for the web interface
  • Python data science community

About

quantitative pairs trading app based on Streamlit for connection to TWS from Interactive Brokers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages