๐ค IBKR Algorithmic Trading Bot โ Auto trading with EMA, RSI, MACD, Bollinger Bands | FastAPI + React dashboard | 6 trading modes | Real-time WebSocket
๐ค NINABOT v3
IBKR Algorithmic Trading Bot
Fully automated trading bot for Interactive Brokers with a live React dashboard. Supports stocks, crypto, futures, forex, and CFDs โ with built-in risk management and 6 trading modes.
Features โข Setup โข Trading Modes โข Architecture โข Risk Management
โจ Features
| | Feature | |---|---| | ๐ก | Live TWS connection via ibapi 9.81.1 with auto-reconnect every 15s | | ๐ง | Signal engine โ EMA, RSI, MACD, Bollinger Bands, ATR combined into a 0-100 score | | โก | 6 trading modes โ scalp, daytrade, swing, trend, crypto night, safe | | ๐ | Auto position sizing using Kelly criterion based on NAV + conviction score | | ๐ก๏ธ | Risk management โ kill switch, circuit breakers, drawdown limits | | ๐น | Multi-asset โ stocks, crypto, futures, forex, CFDs, options | | ๐ | React dashboard with real-time WebSocket updates (P&L, positions, logs) | | ๐ฆ | Social sentiment โ optional Twitter/X scoring per instrument | | ๐ | Yahoo Finance fallback when IBKR live data is unavailable | | ๐ | Bracket orders โ automatic Stop Loss & Take Profit on every trade |
๐ Setup
Prerequisites
- Python 3.12
- Node.js 20
- TWS (Trader Workstation) โ must be running
Step 1 โ Configure TWS API
In TWS: Edit โ Global Configuration โ API โ Settings
- โ Enable ActiveX and Socket Clients
- โ
Port:
7496(live) or7497(paper trading) - โ Enable Allow connections from localhost only
Step 2 โ Configure .env
Open backend/.env and fill in your account number:
IBKR_ACCOUNT=UXXXXXXXX # Your IBKR account number (starts with U)
TWS_PORT=7496 # 7496 = live | 7497 = paper trading
Step 3 โ Install dependencies (first time only)
# Backend
cd backend
py -3.12 -m venv venv
venv\Scripts\activate # Windows
source venv/bin/activate # Mac/Linux
pip install -r requirements.txt
Frontend
cd ../frontend
npm install
Step 4 โ Run
Windows โ double-click:
LANCER_BOT.bat โ Start ARRETER_BOT.bat โ Stop
Manual:
# Terminal 1 โ Backend cd backend && venv\Scripts\activate && python main.py
Terminal 2 โ Frontend
cd frontend && npm run dev
Live URLs
| Service | URL | |---|---| | ๐ฅ๏ธ Dashboard | http://localhost:5173 | | ๐ API Health | http://localhost:8000/health | | ๐ก Scan Status | http://localhost:8000/scan/status |
๐ Trading Modes
| Mode | EMA | RSI Band | Risk % | Max Trades | Scan | Description | |---|---|---|---|---|---|---| | scalp | 9/21 | 35โ65 | 0.5% | 20 | 5s | Flash profit, max reactivity | | daytrade | 21/50 | 35โ65 | 1.0% | 8 | 30s | Short sessions, regular profits | | swing | 50/200 | 30โ70 | 1.5% | 3 | 120s | Max profit, high TP | | trend | 50/200 | 35โ65 | 1.0% | 5 | 60s | Pure trend following | | crypto_night | 20/100 | 28โ72 | 0.8% | 6 | 20s | BTC/ETH 24/7, high volatility | | safe | 34/150 | 40โ60 | 0.3% | 2 | 120s | Capital preservation, minimal risk |
๐ง Signal Engine
Each instrument is scored 0โ100 by combining:
EMA crossover โ 25 pts (trend direction)
RSI โ 20 pts (overbought / oversold)
MACD โ 15 pts (momentum)
Bollinger Bands โ 15 pts (volatility squeeze)
ATR โ 10 pts (volatility filter)
Sentiment โ 5 pts (Twitter/X โ optional)
Signal fires when score โฅ min_conviction threshold (configurable per mode)
๐ก๏ธ Risk Management
- Kill switch โ auto-stops bot if drawdown exceeds threshold
- Max drawdown: 5% of NAV (configurable)
- Max position size: 2% of NAV per trade
- Daily loss limit: $10,000 (configurable)
- Consecutive losses: position size halved after every 2 losses (anti-martingale)
- Retry queue: failed orders retried up to 3x with backoff (2s โ 5s โ 15s)
๐๏ธ Architecture
ninabot/
โโโ backend/
โ โโโ main.py # FastAPI server + WebSocket hub
โ โโโ ibkr_client.py # TWS connection, orders, positions, prices
โ โโโ auto_trader.py # Trading loop, signal engine, position manager
โ โโโ price_feed.py # IBKR live + Yahoo Finance fallback
โ โโโ risk_manager.py # Circuit breakers, kill switch, drawdown
โ โโโ social_monitor.py # Twitter/X sentiment scoring
โ โโโ config.py # Pydantic settings from .env
โ โโโ requirements.txt
โ โโโ .env # โ configure this
โโโ frontend/
โ โโโ src/App.jsx # React real-time dashboard
โโโ LANCER_BOT.bat # One-click start (Windows)
โโโ ARRETER_BOT.bat # One-click stop (Windows)
โ๏ธ Environment Variables
| Variable | Default | Description | |---|---|---| | IBKR_ACCOUNT | (required) | IBKR account number (starts with U) | | TWS_HOST | 127.0.0.1 | TWS host | | TWS_PORT | 7496 | 7496 = live ยท 7497 = paper | | TWSCLIENTID | 1 | API client ID | | MAXDRAWDOWNPCT | 5.0 | Kill switch threshold (% of NAV) | | MAXPOSITIONPCT | 2.0 | Max trade size (% of NAV) | | DAILYLOSSLIMIT | 10000.0 | Daily loss cap | | TWITTERBEARERTOKEN | (optional) | Twitter/X API for sentiment | | API_HOST | 0.0.0.0 | Backend bind address | | API_PORT | 8000 | Backend port |
๐ก API Endpoints
| Method | Endpoint | Description | |---|---|---| | GET | /health | Bot status + IBKR connection | | GET | /snapshot | Full account snapshot | | POST | /order | Place an order | | DELETE | /order/{id} | Cancel an order | | POST | /position/close | Close a position | | POST | /positions/close_all | Close all positions | | POST | /auto/start | Start auto trader | | POST | /auto/stop | Stop auto trader | | GET | /auto/status | Auto trader status | | GET | /performance | P&L + win rate | | GET | /risk/status | Risk manager state | | POST | /risk/kill | Emergency kill switch | | WS | /ws | Real-time WebSocket stream |
Built with โค๏ธ for algorithmic traders ยท Interactive Brokers TWS API