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.
- 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
- 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)
- Create a virtual environment:
python -m venv venv
- Activate the virtual environment:
source venv/bin/activate # On Unix/macOS
venv\Scripts\activate # On Windows
- Install dependencies:
pip install -r requirements.txt
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.
-
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
-
Run the application:
python run.py
-
Open your browser and navigate to the displayed URL (typically http://localhost:8501)
-
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
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
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
- Clone the repository:
git clone https://github.com/yourusername/TWSPairsTradingApp.git
cd TWSPairsTradingApp
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Unix/macOS
- Install in development mode:
pip install -e .
python -m pytest tests/
- 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
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Create a Pull Request
MIT License
- Interactive Brokers TWS API
- Streamlit for the web interface
- Python data science community