Automated trading - Interactive Brokers

Setup guide - how to autotrade STOCKS on Interactive Brokers exchanges (Nyse, Nasdaq, etc)

Updated 09.12.2021! This guide explains how to setup and use Traderelay v.409 and later versions.

This process might seem complicated, but its actually quite simple.
The complication comes from the fact that there are 3 different platforms that has to be setup: Tradingview -> Traderelay -> Trader Workstation (TWS - Interactive Brokers)
But after they are setup, you have Trading Bot trading stocks through Interactive brokers. Then you can start tweaking your strategies and making them profitable.

automated crypto trading

This guide helps you to set up automated trading Stocks in Interactive brokers. We use AMD stocks as example. LONG and SHORT orders are possible with AMD without extra borrow operations.

1. Setup

1. Go to website and login. (Dont have account yet? Open account here. (included is our referrer code)
2. After login. Go to chart screen in tradingview. Choose AMD as symbol.
3. Press 'Pine Editor'-tab below the chart and copy paste our example Study code from below. Press Save. Press Add to Chart. -> now you should see startegy added to you ETHUSDT chart.

Example strategy with alerts

This code written with Pinescript v.4.
Just copy-paste it to -> chart window -> Pine Editor -> Save -> Add to chart.

study("Traderelay 4 Example Study", overlay=true)

sma1 = sma(close, 14)
sma2 = sma(close, 55)
longCondition = crossover(sma1, sma2)

shortCondition = crossunder(sma1, sma2)


//entry long
plotshape(longCondition, style=shape.labelup, location=location.belowbar,, 0), size=size.tiny, title='longCondition', text='longCondition',, 0))
alertcondition(longCondition, title='longCondition', message='action:LONG symbol:{{ticker}} qt:1 id:xxxx')

//entry short
plotshape(shortCondition, style=shape.labeldown, location=location.abovebar,, 0), size=size.tiny, title='shortCondition', text='shortCondition',, 0))
alertcondition(shortCondition, title='shortCondition', message='action:SHORT symbol:{{ticker}} qt:1 id:xxxx')


2. Setup alerts in Tradingview

Now that your study is running succesfully in chart window. Create alerts for it. Alerts will send LONG or SHORT command to Traderelay and onwards to real exchanges.

Create alert

1. Go to "Alerts"-section, its on upper right navigation. Create new alert with following settings.
2. Condition: choose your study from dropdown ("Traderelay 4 Example Study").
3. Choose "Once per bar close".
4. Fill checkbox "webhook". Insert webhook url:
4. Copy paste your personal webhook id to messagebox (Change xxxx to your personal webhook id) (webhook id you can get from Traderelay)
5. Press "Create". Thats it! Now your study will send alert message to traderelay software everytime alert is triggered.

All available commands

You can use these commands in your alert as you develop your strategy further. (Let us know if you need new special commands.)

Market Commands that can be used:
BUY LONG command: action:LONG symbol:AMD qt:1 id:xxxx (if you have short position, this command will close it and buy long.)
SELL SHORT command: action:SHORT symbol:AMD qt:1 id:xxxx (if you have long position, this command will close it and sell short.)
Close half of current position command: action:CLOSEHALF symbol:AMD id:xxxx (we use this command to take profits.)
Close current position command: action:CLOSE symbol:AMD id:xxxx (use this to close position, stoploss, etc.)

3. Setup TradeRelay software

-Install TradeRelay software on your computer

1. Set TWS port (optional, default setting should work fine.)

-Login with your email and password
-Go to Settings tab and fill in TWS port:: 7497. (this is IB TWS default port.)
-Save settings

2. Setup instruments

You have to have at least one instrument on instrument list before you start Traderelay software.
Instrument can be paused, but there needs to be atleast one instrument so software can loop the list.

-On main page of Traderelay software, add instrument symbol: AMD
-Choose AMD from the list and click exchange/sectype IB (Stock market) -> this sets the exchange to IB stock market

3. Start traderelay

-Press START and TradeRelay begins to loop through your instruments -> checking received alerts from Tradingview -> relaying orders to exchanges
(in this case ib Stock markets (nyse, nasdaq, etc) ).


4. Setup Interactive Brokers and its TWS software

1. You will need to open Interactive Brokers account. (Here is link with our referral code: Open Interactive Brokers.)
2. You will need install TWS software on your computer. Download latest TWS software
3. Start TWS and set settings as:
File -> Global configuration -> API -> settings:
-Enable activeX and socket clients
-Uncheck "Read -Only API"
-Set port: 7497 (its default setting so probably set already)
-Press APPLY to save setting (Windows defender might popup)
-Allow 'Trader Workstation' through your firewall (to private network & public networks)

Testing Traderelay - TWS connection
1. Login to TWS to "papertrading" side.
2. Login to Traderelay -> add new instrument 'AMD' -> Select 'AMD' from instruments list -> set its exhange/sectype by pressing 'IB (Stock market)'-button.
3. Start Traderelay looping by pressing 'Start'-button. (Traderelay starts looping intruments and waiting incoming Tradingview alerts.)
4. Send test order to IB -> by pressing 'Send Test Order'-button -> order 'action:LONG symbol:AMD qt:1 id:xxxx' is sent to IB
-> In TWS you should see that you just bought 1 AMD stock.

5. Setup complete

After you have succesfully completed above 4 step you should be able to run trading bot on market open times in IB Nyse or Nasdaq exchanges. Please note, that market orders are only completed when markets are open. So if your strategy sends orders when markets are closed -> the orders will trigger on next market day.
Checklist: is sending alerts (with your webhook id) %
-Traderelay is looping and waiting alerts (incoming alerts have your webhook id + some command) %
-IB account has money and TWS is running on your computer. %

Firewall settings (optional)

Usually there is no need to change firewall setting on your windows.
But if you have trouble and alerts are not forwarded to TWS:
1. Go to your computers firewall settings and allow application "Traderelay software" in and out.
This should allow Traderelay to send and receive API commands to TWS.
2. Go to your computers firewall settings and allow application "TWS software" in and out.
This should allow TWS to receive orders and send them forward to Interactive brokers.

Still having problems getting automated strategy running?

Due lot of help requests we now offer paid 'Developer help' for setting up all parts of automated trading ( strategy and alerts, Traderelay, Binance and IB TWS).
You can check it at our Subscription page.

After you have ordered 'Developer help', we will contact you by email -> the setup process can be done live in Google meets or by email guidance.