Automate placing trailing stop limit sell orders on Interactive Brokers (IBKR) using Python and the ib_insync library. This tool reads stock order data from a CSV file and places GTC trailing stop-limit orders with logic to protect against selling below your average buy price.
- Connects to Interactive Brokers via TWS or IB Gateway using API
- Supports delayed data fallback if real-time quotes are unavailable
- Reads orders from a CSV file including:
- Symbol (e.g., AAPL, TSLA)
- Quantity
- Trailing stop percentage
- Limit price offset
- Average buy price (to prevent selling at a loss)
- Dynamically calculates stop and limit prices based on live market prices
- Places Good-Till-Canceled (GTC) trailing stop-limit sell orders
- Logs all order activity, errors, and skipped trades
- Python 3.7 or higher
- IBKR Trader Workstation (TWS) or IB Gateway running and API enabled
- Python libraries:
ib_insyncpandas
- Clone this repository
- Install required Python packages
- Launch IBKR TWS or IB Gateway with API enabled
- Prepare your CSV file with the required columns:
Symbol,Quantity,TrailingPercent,LimitOffset,AvgBuyPrice
- Start your TWS/IB Gateway and ensure it's connected
- Run the script with your CSV file path as input
- Orders will be submitted, and logs will be generated showing each action
Symbol,Quantity,TrailingPercent,LimitOffset,AvgBuyPrice
AAPL,10,1.5,0.3,180.25
TSLA,5,2.0,0.5,700.00
- Orders will not be placed if the current market price is below your average buy price
- Script uses GTC time-in-force to keep orders active beyond market hours
- Ensure API access and market data subscription are properly set in your IBKR account
Support can be added for Telegram or Email alerts on order execution. Contact for integration.
Built by [Jahanzaib] Feel free to reach out for custom trading automation projects.