Error code 162: TWS session is connected from a different IP address

When collecting historical data from IB, you might see this error message in the logs:

quantrocket.history: WARNING [demo-stk-1d] ibg1 got IB error code 162 for historical data request for 1 M of 15 mins TRADES for EDC STK (conid 225312853) ending 20181122 21:00:00 GMT: Historical Market Data Service error message:Trading TWS session is connected from a different IP address
quantrocket.history: WARNING [demo-stk-1d] Waiting 60 seconds before collecting demo-stk-1d history to see if other TWS session terminates

Cause: simultaneous logins

This error message occurs when you are logged in to IB's platform in two places simultaneously with the same user login. IB only permits each set of user credentials to be logged into one session at a time. This restriction applies to IB Gateway, Trader Workstation (TWS), Client Portal, and any other IB platform where market data can be accessed. It does not apply to Account Management.

Simultaneous paper and live login not allowed

If you're using a paper account, this error can occur when you are logged in to one IB platform with your live login and using the corresponding paper account with QuantRocket (or vice versa). Because paper accounts share the same market data subscription with the live account, IB does not permit you to be logged into both the live and paper account at the same time.

Resolution: terminate other session

When QuantRocket detects this error, it sleeps for 60 seconds then tries to request the data again. The error will continue until you disconnect your other session, at which point QuantRocket will be able to resume data collection:

quantrocket.history: INFO [demo-stk-1d] Resuming collecting demo-stk-1d history after delay

How to have multiple sessions

If you need to access TWS or another platform while collecting data with QuantRocket, you should create a second user login, reserving one login for QuantRocket and the other for your own use.

If you only need to access TWS at certain times of day and want to use that login to speed up data collection, one option is to schedule the IB Gateway associated with that login to start and stop at certain times of day. For example, if you need to use TWS while the US stock market is open, you might schedule IB Gateway to start daily after the market closes and stop each morning before the open:

# Start ibg2 weekdays at 4:30 PM
30 16 * * mon-fri quantrocket ibg start 'ibg2'
# Stop ibg2 weekdays at 9 AM
0 9 * * mon-fri quantrocket ibg stop 'ibg2'

You don't need to cancel and restart a long-running historical data collection in order to start or stop IB Gateway. QuantRocket's historical data collection automatically detects the appearance and disappearance of IB Gateways and seamlessly uses whatever gateways are available to maximize data collection speed.