An automated trading integration that connects Interactive Brokers with AI-powered financial analysis for intelligent trading decisions.
Created by: @aristides lintzeris | Follow for updates and trading insights
- Real-time Data Processing: Connect to Interactive Brokers API for live market data
- AI-Powered Analysis: Leverage multiple AI models for comprehensive market analysis
- Automated Trading: Execute trades based on AI recommendations with built-in risk management
- Multi-Market Support: Configure for any stock exchange or market worldwide
- Risk Management: Position sizing, stop-losses, and daily trade limits
- Paper Trading Support: Test strategies safely before live trading
- Comprehensive Logging: Track all activities and performance metrics
- Quick Start
- Prerequisites
- Installation
- Configuration
- Usage
- Risk Management
- Monitoring
- Documentation
- Troubleshooting
- Contributing
- Contact
- Disclaimer
Get up and running in 5 minutes:
# 1. Clone the repository
git clone https://github.com/aristideslintzeris/ibkr-ai-hedge-fund.git
cd ibkr-ai-hedge-fund
# 2. Install dependencies
pip install -r requirements.txt
# 3. Configure environment
cp env.template .env
# Edit .env with your API keys and settings
# 4. Start paper trading
python ibkr_integration.py- Sign up at Interactive Brokers
- Download and install Trader Workstation (TWS) or IB Gateway
- Recommended: Start with paper trading for testing
- Python 3.8 or higher
- pip package manager
- OpenAI API Key: For GPT models
- Financial Datasets API Key: For market data
- Optional: Additional model providers (Anthropic, Groq, DeepSeek, Google)
git clone https://github.com/aristideslintzeris/ibkr-ai-hedge-fund.git
cd ibkr-ai-hedge-fundpip install -r requirements.txtgit clone https://github.com/virattt/ai-hedge-fund.gitcp env.template .envEdit your .env file with the following configurations:
# IBKR Connection Settings
IBKR_HOST=127.0.0.1
IBKR_PORT=7497 # 7497 for paper trading, 7496 for live
IBKR_CLIENT_ID=123
# Trading Settings
INITIAL_CASH=10000.0 # Starting capital (adjust to your preference)
CURRENCY=USD # USD, EUR, GBP, AUD, CAD, etc.
MAX_POSITION_SIZE=0.2 # Maximum 20% per position
MAX_DAILY_TRADES=10 # Maximum trades per day
STOP_LOSS_PERCENTAGE=0.05 # 5% stop loss
TAKE_PROFIT_PERCENTAGE=0.10 # 10% take profit
# Market Settings
MARKET_TIMEZONE=US/Eastern # Your market timezone
TRADING_SCHEDULE=09:30 # Daily analysis time (24-hour format)
# Watchlist (comma-separated stock symbols)
WATCHLIST=AAPL,GOOGL,MSFT,AMZN,TSLA,NVDA,META,NFLX,BABA,V
# AI Model Configuration
PRIMARY_MODEL=gpt-4o-mini
BACKUP_MODELS=gpt-3.5-turbo,claude-3-haiku
# API Keys (REQUIRED)
OPENAI_API_KEY=your-openai-api-key
FINANCIAL_DATASETS_API_KEY=your-financial-datasets-api-key
# Optional AI Model Providers
ANTHROPIC_API_KEY=your-anthropic-api-key
GROQ_API_KEY=your-groq-api-key
DEEPSEEK_API_KEY=your-deepseek-api-key
GOOGLE_API_KEY=your-google-api-key
# Logging
LOG_LEVEL=INFO # DEBUG, INFO, WARNING, ERROR
LOG_FILE=ibkr_hedge_fund.logπΊπΈ US Market Configuration
CURRENCY=USD
MARKET_TIMEZONE=US/Eastern
TRADING_SCHEDULE=09:30
WATCHLIST=AAPL,GOOGL,MSFT,AMZN,TSLA,NVDA,META,NFLX,JPM,JNJπͺπΊ European Market Configuration
CURRENCY=EUR
MARKET_TIMEZONE=Europe/London
TRADING_SCHEDULE=09:00
WATCHLIST=ASML,SAP,LVMH,NESN,NOVN,ROG,OR,SAN,INGA,PHIAπ¦πΊ Australian Market Configuration
CURRENCY=AUD
MARKET_TIMEZONE=Australia/Sydney
TRADING_SCHEDULE=09:00
WATCHLIST=CBA,BHP,CSL,WBC,ANZ,NAB,WOW,TLS,RIO,MQGπ―π΅ Japanese Market Configuration
CURRENCY=JPY
MARKET_TIMEZONE=Asia/Tokyo
TRADING_SCHEDULE=09:00
WATCHLIST=7203,6758,6861,8411,9984,4661,6098,9432,8001,8031- Open TWS/IB Gateway
- Navigate to API Settings:
- File β Global Configuration β API β Settings
- Enable API:
- β Enable ActiveX and Socket Clients
- β Read-Only API (uncheck for trading)
- Set Port:
- Paper Trading:
7497 - Live Trading:
7496
- Paper Trading:
- Add Trusted IPs:
127.0.0.1
- Validate Configuration:
python config.py- Start Paper Trading:
python ibkr_integration.py- Monitor Logs:
tail -f ibkr_hedge_fund.log# In your .env file
WATCHLIST=AAPL,GOOGL,MSFT,AMZN,TSLA
# Or modify in config.py
WATCHLIST = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"]# Daily at 9:30 AM
TRADING_SCHEDULE=09:30
# Multiple times per day (comma-separated)
TRADING_SCHEDULE=09:30,14:00,16:00
# Weekly on specific days
TRADING_SCHEDULE=Monday:09:30,Wednesday:09:30,Friday:09:30Conservative Profile
MAX_POSITION_SIZE=0.1 # 10% max per position
MAX_DAILY_TRADES=5
STOP_LOSS_PERCENTAGE=0.03 # 3% stop loss
TAKE_PROFIT_PERCENTAGE=0.06 # 6% take profitModerate Profile
MAX_POSITION_SIZE=0.15 # 15% max per position
MAX_DAILY_TRADES=8
STOP_LOSS_PERCENTAGE=0.05 # 5% stop loss
TAKE_PROFIT_PERCENTAGE=0.10 # 10% take profitAggressive Profile
MAX_POSITION_SIZE=0.25 # 25% max per position
MAX_DAILY_TRADES=15
STOP_LOSS_PERCENTAGE=0.08 # 8% stop loss
TAKE_PROFIT_PERCENTAGE=0.15 # 15% take profit- Position Sizing: Automatic position size calculation based on portfolio percentage
- Stop Losses: Configurable stop-loss orders for all positions
- Daily Limits: Maximum number of trades per day
- Portfolio Limits: Maximum allocation per security
- Paper Trading: Safe testing environment
# Check current risk metrics
python -c "from ibkr_integration import get_portfolio_metrics; print(get_portfolio_metrics())"The integration provides comprehensive monitoring through:
- Log Files: Detailed activity logging
- Portfolio Tracking: Real-time position updates
- Performance Metrics: P&L tracking and analytics
- Error Handling: Automatic error recovery and logging
Create a simple monitoring dashboard:
python dashboard.pyComprehensive documentation is available in the /docs folder:
- Installation Guide: Detailed setup instructions
- Configuration Guide: Complete configuration reference
- API Reference: Code documentation and examples
- Trading Guide: Trading strategies and best practices
- Troubleshooting: Common issues and solutions
Connection Errors
Issue: Cannot connect to IBKR
Error: Connection failed to 127.0.0.1:7497
Solution:
- Ensure TWS/IB Gateway is running
- Check API settings are enabled
- Verify correct port (7497 for paper, 7496 for live)
- Confirm 127.0.0.1 is in trusted IPs
API Key Errors
Issue: Invalid API key
Error: OpenAI API key is invalid
Solution:
- Verify API key in
.envfile - Check API key permissions
- Confirm sufficient quota/credits
- Test API key independently
Trading Errors
Issue: Order rejected
Error: Order rejected - insufficient funds
Solution:
- Check account balance
- Verify market hours
- Confirm security permissions
- Review position limits
Enable detailed logging:
LOG_LEVEL=DEBUGRun diagnostic tests:
python -m pytest tests/We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
- Creator: @aristidesai
For project updates, trading insights, and AI developments, follow @aristidesai on Twitter.
This project is licensed under the MIT License - see the LICENSE file for details.
Important: This software is for educational purposes only. Trading involves substantial risk of loss. Always test thoroughly with paper trading before using real money. The authors and contributors are not responsible for any financial losses.
Key Points:
- Start with paper trading
- Test thoroughly before live trading
- Never risk more than you can afford to lose
- Understand the risks involved
- Consult with financial professionals
Contact me on twitter for support - @aristidesai
- IBKR API: Official Documentation
- AI-Hedge-Fund for the original AI analysis system
- Interactive Brokers for the trading platform
- Contributors and the open-source community
Made by @aristides lintzeris


