NadirAliOfficial
kaizen-intraday-semis-backtest
Python

Mechanical intraday backtest for SMH and SOXX using progressive entry, VIX-scaled leverage, and strict end-of-day flat rules.

Last updated Jul 4, 2026
14
Stars
1
Forks
1
Issues
0
Stars/day
Attention Score
50
Language breakdown
Python 93.5%
PowerShell 6.5%
โ–ธ Files click to expand
README

IBKR Production Trading System

EMA 25/125 Strategy with Dynamic Leverage

๐Ÿ“‹ Overview

Automated trading system for SMH (semiconductor ETF) using EMA crossover strategy with:

  • Signal: EMA 25 > EMA 125 = BULL (enter long)
  • Leverage: 3.0x - 3.75x based on VIX
  • Stop Loss: -2% daily equity
  • Entry: 5 minutes before market close (3:55 PM ET)
  • Exit: Bear signal or stop loss

โš™๏ธ Setup

1. Install Dependencies

pip install ib_insync pandas pytz

2. Configure IBKR TWS

  • Open TWS Paper Trading
  • Go to: Edit โ†’ Global Configuration โ†’ API โ†’ Settings
  • Enable: "Enable ActiveX and Socket Clients"
  • Set Port: 7496 (paper) or 7497 (live)
  • Add Trusted IP: 127.0.0.1
  • Click OK and restart TWS

3. Login to TWS

  • Start TWS and login with paper trading credentials

๐Ÿš€ Running the System

Test Mode (Immediate Entry)

python ibkr_production.py
  • Enters position immediately
  • Good for testing

Production Mode (3:55 PM Entry)

Edit line 241:
system.run(test_mode=False)  # Change True to False

๐Ÿ“Š How It Works

Daily Cycle

| Time | Action | |------|--------| | 9:30 AM | Records day start equity | | Continuous | Monitors -2% stop loss | | 3:55 PM | Enters if BULL signal + no position | | 4:00 PM | Updates EMAs, checks bear exit |

When You Add Money

  • System recalculates at 4:00 PM (market close)
  • Updates EMAs with today's close price
  • Checks signal: BULL or BEAR
  • If BULL โ†’ enters position at 3:55 PM next day
  • Position sized on new total equity

Stop Loss

  • Monitors equity every 5 seconds
  • If daily loss โ‰ฅ -2% โ†’ exits immediately
  • Can re-enter same day at 3:55 PM if BULL signal remains

Leverage Scaling

| VIX Level | Leverage | |-----------|----------| | VIX < 12 | 3.75x | | VIX < 13 | 3.5x | | VIX < 14 | 3.25x | | VIX โ‰ฅ 14 | 3.0x |

Note: Paper trading uses default 3.0x (VIX not available)


๐Ÿ“ˆ Console Output

โœ… Connected to IBKR Paper Trading (Port 7496)
โœ… EMAs Initialized: 25=399.87 | 125=356.11
   Signal: BULL
๐Ÿ“… Day Start Equity: $1,001,295.25
โœ… ENTERED: 7355 shares @ $408.39 (Leverage: 3.0x)

Status Messages

| Symbol | Meaning | |--------|---------| | โœ… | Success | | ๐Ÿ›‘ | Stop loss triggered | | ๐Ÿšช | Position exited | | ๐Ÿ“Š | Signal changed | | โน๏ธ | System shutdown |


๐Ÿ”ง Configuration

Edit these variables in ibkr_production.py:

# Connection
IBKR_PORT = 7496  # 7496 = paper, 7497 = live

Strategy

EMA_FAST = 25 EMA_SLOW = 125 STOPLOSSPCT = 0.02 # 2%

Leverage

LEV_BASE = 3.0 LEVVIX14 = 3.25 LEVVIX13 = 3.5 LEVVIX12 = 3.75

Timing

ENTRYTIME = dttime(15, 55) # 3:55 PM ET

๐Ÿ›ก๏ธ Safety Features

  • -2% Hard Stop - Cannot lose >2% per day (except gaps)
  • Bear Exit - Exits when EMA turns bearish
  • Auto-reconnect - Handles disconnections
  • Position verification - Checks actual IBKR position
  • Paper trading default - Safe testing environment

๐Ÿ”„ Position Management

Entry Conditions

  • โœ… No current position
  • โœ… EMA 25 > EMA 125 (BULL)
  • โœ… Time = 3:55 PM ET
  • โœ… Valid price available

Exit Conditions

  • โŒ Daily loss โ‰ฅ -2% (stop loss)
  • โŒ EMA 25 < EMA 125 (bear signal)

Re-entry

  • Can re-enter same day at 3:55 PM if:
- Stop triggered earlier - BULL signal still valid

๐Ÿ“ Logs & Monitoring

Real-time Monitoring

Watch the console for:
  • Entry/exit confirmations
  • Stop loss triggers
  • Signal changes
  • Errors

Position Check

# In TWS: Portfolio โ†’ Positions

Should show: SMH shares = script output


๐Ÿšจ Troubleshooting

"No security definition found"

  • Fix: Check symbol and exchange (SMH on ARCA)

"Market data requires subscription"

  • Fix: System uses delayed data (automatic)
  • 15-minute delay is acceptable for end-of-day strategy

"Cannot convert NaN to integer"

  • Fix: Updated script handles this automatically
  • Waits for valid price before entering

Position not entering

  • Check time (must be 3:55 PM ET in production mode)
  • Check signal (EMA 25 must be > EMA 125)
  • Check TWS is connected and logged in

๐Ÿ“Š Expected Performance

Based on backtest (July 2022 - Jan 2026):

  • CAGR: 99.30%
  • Max DD: 20.84%
  • Sharpe: 1.64
  • Trades/Year: 2.8
  • Win Rate: 40%

๐ŸŽฏ Going Live (Real Money)

Checklist

  • โœ… Test in paper trading for 1 week
  • โœ… Verify all entries/exits working
  • โœ… Confirm stop loss triggers correctly
  • โœ… Check position sizing is correct

Switch to Live

  • Change port to 7497
IBKR_PORT = 7497
  • Login to TWS Live (not paper)
  • Start with small capital (test $10k first)
  • Monitor daily for first week
  • Gradually increase to full capital

โš ๏ธ Risk Warnings

  • Gap Risk: Overnight gaps can exceed -2% stop
  • Leverage: 3x amplifies gains AND losses
  • Bear Markets: Strategy exits during downtrends (misses upside)
  • Slippage: Real executions may differ from backtest
  • Technical: System failures, disconnections possible

๐Ÿ›‘ Emergency Shutdown

To stop the system:

  • Press Ctrl+C in terminal
  • System exits gracefully
  • Does NOT close position (manual close in TWS if needed)
Manual position close:
  • Open TWS
  • Go to Portfolio โ†’ Positions
  • Right-click SMH โ†’ Close Position

๐Ÿ“ž Support

Issues?

  • Check TWS connection
  • Verify API settings
  • Check console for error messages
  • Ensure market hours (9:30 AM - 4:00 PM ET)

๐Ÿ“„ Files

  • ibkr_production.py - Main trading script
  • DEPLOYMENT_GUIDE.md - Setup instructions
  • README.md - This file

Strategy validated. System ready. Trade safe. ๐ŸŽฏ

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท NadirAliOfficial/kaizen-intraday-semis-backtest ยท Updated daily from GitHub