Quick Start Guide: US Equity Trading with IBKR
This guide will help you get started with the CGS Engine US Equity Trading System using Interactive Brokers.
Prerequisites
- Python 3.9+ installed
- Interactive Brokers account (paper trading or live)
- IBKR Trader Workstation (TWS) or IBKR Gateway installed
- PostgreSQL database
Step 1: Install IBKR Software
Option A: Install Trader Workstation (TWS)
# Download from:
https://www.interactivebrokers.com/en/index.php?f=16042
Option B: Install IBKR Gateway (Recommended for automated trading)
# Download from:
https://www.interactivebrokers.com/en/index.php?f=16457
Step 2: Configure IBKR
For TWS:
- Open TWS
- Go to Configure → API → Settings
- Enable Enable ActiveX and Socket Clients
- Set Socket port to
7497(paper trading) or7496(live trading) - Add your IP address to Trusted IPs (use
127.0.0.1for localhost)
For IBKR Gateway:
- Launch IBKR Gateway
- Login with your credentials
- API settings are automatically configured
Step 3: Install System Dependencies
# Navigate to project directory
cd cgs_tech
# Create virtual environment
python -m venv venv
# Activate virtual environment
# macOS/Linux:
source venv/bin/activate
# Windows:
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
pip install -r requirements-ibkr.txt
Step 4: Configure Environment
# Copy example environment file
cp env_example .env
# Edit .env file with your configuration
nano .env # or use your preferred editor
Update the following in .env:
IBKR_HOST=127.0.0.1
IBKR_PORT=7497 # 7497 for paper, 7496 for live
IBKR_CLIENT_ID=1
TRADING_MODE=paper
TRADING_SYMBOLS=AAPL,MSFT,GOOGL
Step 5: Test Connection
# Start TWS or IBKR Gateway
# Wait for it to connect
# Test the connection
python -c "
from ib_insync import IB
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=1)
print('Connected successfully!')
print('Account values:', ib.accountValues())
ib.disconnect()
"
If successful, you should see account information.
Step 6: Setup Database
# Create PostgreSQL database
createdb cgs_trading
# Or using psql:
psql -U postgres
CREATE DATABASE cgs_trading;
CREATE USER cgs_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE cgs_trading TO cgs_user;
\q
# Update .env with database credentials
DB_HOST=localhost
DB_PORT=5432
DB_NAME=cgs_trading
DB_USER=cgs_user
DB_PASSWORD=your_password
Step 7: Run the System
Paper Trading Test
# Start TWS/Gateway (already running from Step 5)
# Run the example trading system
python examples/ibkr_equity_trading_example.py
Monitor Trading Dashboard (Optional)
In a separate terminal:
# Start the dashboard
python src/app.py
# Access at http://localhost:8081
Common Symbols for Testing
# Large-cap stocks
AAPL # Apple
MSFT # Microsoft
GOOGL # Google
TSLA # Tesla
# ETFs
SPY # S&P 500
QQQ # NASDAQ
DIA # Dow Jones
# Tech stocks
NVDA # NVIDIA
META # Meta/Facebook
AMZN # Amazon
Troubleshooting
Connection Issues
Problem: Cannot connect to IBKR
Error: Connection refused
Solutions:
- Ensure TWS/Gateway is running
- Check port number (7497 for paper, 7496 for live)
- Verify API is enabled in TWS settings
- Check firewall settings
Permission Errors
Problem: Orders rejected due to permissions
Solutions:
- Enable API permissions in TWS settings
- Check account permissions in IBKR
- Ensure account has sufficient buying power
Data Issues
Problem: No historical data available
Solutions:
- Request more historical data (increase duration)
- Check if market data subscription is active
- Verify symbol is tradeable
Next Steps
- Backtest your strategy: Use
notebooks/VBT_cgs_base.ipynbto backtest strategies - Optimize parameters: Use SHAP analysis to optimize strategy parameters
- Monitor performance: Use the dashboard to monitor real-time performance
- Adjust risk settings: Fine-tune risk parameters in
.env
Safety Checklist
Before going live:
- Tested in paper trading for at least 2 weeks
- Verified all risk limits are set appropriately
- Configured stop-losses
- Set reasonable position sizes
- Tested emergency stop functionality
- Verified data quality
- Setup logging and monitoring
- Backtested strategies extensively
- Documented all parameters
Getting Help
- Documentation: See
IBKR_US_EQUITY_SYSTEM_README.md - Examples: Check
examples/directory - Issues: Report on GitHub
- Community: Join discussions on Discord/Slack
Important Notes
⚠️ Never trade with real money before extensive testing
⚠️ Always monitor your system when live
⚠️ Use appropriate risk management at all times
⚠️ Start with small position sizes
⚠️ Keep emergency stop procedures ready
Support
For questions or issues:
- GitHub Issues: https://github.com/yourusername/cgs_tech/issues
- Documentation: README files
- Email: support@cgs-tech.com
Happy Trading! 🚀