Production-ready trade replication engine for Interactive Brokers. Copies trades from a master account to multiple follower accounts with position sizing, risk controls, and execution monitoring.
โฌก Pax Americana
Mirror trades from a master IBKR account to unlimited client accounts in real-time.
How It Works
Master Machine (Windows RDP / VPS with TWS)
โ
โโโ master.py
โ โโโ Connects to TWS (clientId=0 โ sees ALL manual orders)
โ โโโ Captures orders in real-time
โ โโโ Serves orders + master balance via Flask API (:5000)
โ โโโ Auto-updates master balance every 30 seconds
โ
โ HTTP GET (poll every 2 seconds)
โผ
Client Machine (Any Windows Machine)
โ
โโโ Pax_Americana.exe
โโโ Connects to client's own TWS
โโโ Polls master API for new orders
โโโ Applies proportional sizing + multiplier
โโโ Places orders in client IBKR account
Project Structure
ibkr-copytrade-engine/
โโโ master.py # Runs on master Windows machine โ TWS + Flask in one
โโโ NeroAI_CopyTrade.py # Client GUI app โ polls master & places orders
โโโ build_neroai.spec # PyInstaller spec to build Windows .exe
โโโ neroai.ico # App icon (embedded in .exe)
โโโ requirements.txt # Python dependencies
โโโ README.md
โโโ .github/
โโโ workflows/
โโโ build.yml # Auto-builds Windows .exe on push to main
Setup
1. Master Machine (Windows RDP / VPS)
The master machine runs TWS 24/7 and serves orders to all clients.
Requirements:
- TWS (Trader Workstation) running and logged in
- API enabled in TWS:
Edit โ Global Config โ API โ Enable ActiveX and Socket Clients - Port 5000 open in Windows Firewall
netsh advfirewall firewall add rule name="PaxAmericana" dir=in action=allow protocol=TCP localport=5000
Install:
pip install ib_insync flask
Run:
python master.py
master.py will:
- Connect to TWS on
127.0.0.1:7496 - Capture all open and new orders in real-time
- Start Flask API on port
5000 - Serve orders + master balance to all clients
/orders?since=N | Returns new orders from index N | | GET | /balance | Returns master account balance | | GET | /status | Server status + total orders | | POST | /clear | Clear order list |
2. Client Machine (Windows)
Each client runs Pax_Americana.exe on their own machine with their own TWS.
Requirements:
- TWS installed, running, and logged in to their IBKR account
- API enabled in TWS settings
NeroAI_CopyTrade.py before building .exe): MASTERURL = "http://YOURMASTER_IP:5000" Replace YOURMASTERIP with the public IP of the master Windows machine. Find it at: https://whatismyip.com (run on master machine)
Client Features
TWS Connection
- Live โ auto-connects to port
7496 - Paper โ auto-connects to port
7497 - No manual port or host entry needed
Copy Mode
| Mode | Description | |------|-------------| | New Trades Only | Only mirrors orders placed after pressing START | | Existing + New Trades | Mirrors all current open orders + new orders |Trading Mode
| Mode | Description | |------|-------------| | Long & Short | Mirrors both BUY and SELL orders | | Long Only | Only mirrors BUY orders โ SELL orders are skipped |Risk Management
| Setting | Range | Description | |---------|-------|-------------| | Size Multiplier | 0.1x to 5.0x | Scales order quantity proportionally | | Max Drawdown | 1% to 50% | Stops copying if client balance drops by this % |Proportional Sizing Formula:
clientqty = masterqty x (clientbalance / masterbalance) x multiplier
Example: Master balance = $25,000 Client balance = $100,000 Master places = BUY 100 TSLA Multiplier = 1.0
Ratio = 100,000 / 25,000 = 4.0 Client qty = 100 x 4.0 x 1.0 = 400 shares
Building the Windows .exe
Via GitHub Actions (recommended)
Push to main โ GitHub Actions automatically builds the .exe. Go to Actions tab โ download PaxAmericanaEXE artifact.
The app icon is automatically extracted and embedded into the .exe during build.
Manually on Windows
pip install pyinstaller ib_insync requests pillow tzdata
pyinstaller build_neroai.spec
The .exe will be in the dist/ folder.
Requirements
ib_insync
requests
flask
pillow
tzdata
Supported Order Types
| Type | Supported | |------|-----------| | Market (MKT) | Yes | | Limit (LMT) | Yes | | Stocks | Yes | | BUY | Yes | | SELL | Yes (skipped in Long Only mode) |
Deployment Checklist
Master Machine:
- [ ] TWS running and logged in
- [ ] API enabled in TWS settings
- [ ] Port 5000 open in Windows Firewall
- [ ]
master.pyrunning - [ ] Test:
curl http://YOUR_IP:5000/status
- [ ] TWS running and logged in
- [ ] API enabled in TWS settings
- [ ]
MASTER_URLset to master machine IP - [ ]
.exebuilt and distributed to client - [ ] Select Live or Paper mode
- [ ] Set multiplier and max drawdown
- [ ] Press START
Notes
master.pyusesclientId=0to capture ALL manually placed orders in TWS- Orders are stored in memory โ reset if
master.pyrestarts - Each client connects to their own TWS with their own account
- Max drawdown is measured from the balance at the time START is pressed
- When max drawdown is hit, copying stops โ client presses START again to resume
Credits
Built by Nadir Ali Khan โ theteamnak.com
ยฉ Pax Americana | github.com/NadirAliOfficial