Brokerages

Interactive Brokers

Introduction

The Lean CLI supports live trading on your local machine or in QuantConnect Cloud, which makes the transfer from backtesting to live trading as seamless as possible. You must log in using your QuantConnect account to deply your algorithm on your local machine or in QuantConnect Cloud. This page contains instructions on how to start live trading with the Interactive Brokers (IB) brokerage. If the Lean Configuration file in your organization workspace contains values for some of the command options, the CLI skips some of the prompts.

To view the implementation of the IB brokerage integration, see the Lean.Brokerages.InteractiveBrokers repository.

Deploy Local Algorithms

If you have an ARM M1 chip, you can't deploy a local live algorithm with the IB brokerage.

Follow these steps to start local live trading with the Interactive Brokers brokerage:

  1. Log in to the CLI if you haven't done so already.
  2. Open a terminal in the organization workspace that contains the project.
  3. Run lean live deploy "<projectName>" to start a live deployment wizard for the project in . / <projectName> and then enter the brokerage number, 2.
    $ lean live deploy "My Project"
    Select a brokerage:
    1) Paper Trading
    2) Interactive Brokers
    3) Tradier
    4) OANDA
    5) Bitfinex
    6) Coinbase Advanced Trade
    7) Binance
    8) Zerodha
    9) Samco
    10) Terminal Link
    11) Trading Technologies
    12) Kraken
    13) TD Ameritrade
    14) Bybit
    15) TradeStation
    16) Alpaca
    Enter an option: 2
  4. Set up IB Key Security via IBKR Mobile. For instructions, see IB Key Security via IBKR Mobile on the IB website.
  5. Go back to the terminal and enter your Interactive Brokers username, account id, and password.
    $ lean live "My Project"
    Username: trader777
    Account id: DU1234567
    Account password: ****************
  6. Enter a weekly restart time that's convenient for you.
    $ lean live "My Project"
    Weekly restart UTC time (hh:mm:ss) [21:00:00]: 

    You'll receive a notification on your IB Key device every Sunday to re-authenticate the connection between IB and your live algorithm. Enter a time on Sunday to receive the notification. If you don't re-authenticate before the timeout period, your algorithm quits executing. Ensure your IB Key device has sufficient battery for the time you expect to receive the notification. If you don't receive a notification, see I am not receiving IBKR Mobile notifications on the IB website.

  7. Enter the number of the live data provider(s) to use and then follow the steps required for the data connection.
    $ lean live deploy "My Project"
    Select a live data provider:
    1) Interactive Brokers
    2) Tradier
    3) Oanda
    4) Bitfinex
    5) Coinbase Advanced Trade
    6) Binance
    7) Zerodha
    8) Samco
    9) Terminal Link
    10) Trading Technologies
    11) Kraken
    12) TDAmeritrade
    13) IQFeed
    14) Polygon
    15) IEX
    16) CoinApi
    17) ThetaData
    18) Custom data only
    19) Bybit
    20) TradeStation
    21) Alpaca
    To enter multiple options, separate them with comma:
  8. If you select one of the following data providers, see the respective page for more instructions:

  9. Enter whether you want to enable delayed market data.
    $ lean live "My Project"
    Enable delayed market data? [yes/no]: 
    This property configures the behavior when your algorithm attempts to subscribe to market data for which you don't have a market data subscription on Interactive Brokers. When enabled, your algorithm continues running using delayed market data. When disabled, live trading will stop and LEAN will shut down.
  10. View the result in the <projectName> / live / <timestamp> directory. Results are stored in real-time in JSON format. You can save results to a different directory by providing the --output <path> option in step 2.

If you already have a live environment configured in your Lean configuration file, you can skip the interactive wizard by providing the --environment <value> option in step 2. The value of this option must be the name of an environment which has live-mode set to true.

Deploy Cloud Algorithms

Follow these steps to start live trading a project in the cloud with the Interactive Brokers brokerage and the Interactive Brokers data provider:

  1. Log in to the CLI if you haven't done so already.
  2. Open a terminal in the organization workspace that contains the project.
  3. Run lean cloud live deploy "<projectName>" --push --open to push . / <projectName>. to the cloud, start a live deployment wizard, and open the results in the browser once the deployment starts.
  4. $ lean cloud live deploy "My Project" --push --open
    [1/1] Pushing 'My Project'
    Successfully updated cloud file 'My Project/main.py'
    Started compiling project 'My Project'
    Successfully compiled project 'My Project'
  5. Enter 2 to select the Interactive Brokers brokerage.
  6. $ lean cloud live deploy "My Project" --push --open
    Select a brokerage:
    1) Paper Trading
    2) Interactive Brokers
    3) Tradier
    4) Oanda
    5) Bitfinex
    6) Coinbase Advanced Trade
    7) Binance
    8) Zerodha
    9) Samco
    10) Terminal Link
    11) Trading Technologies
    12) Kraken
    13) TD Ameritrade
    14) Bybit
    15) TradeStation
    16) Alpaca
    Enter an option: 2
  7. Set up IB Key Security via IBKR Mobile. For instructions, see IB Key Security via IBKR Mobile on the IB website.
  8. Go back to the terminal and enter your Interactive Brokers username, account id, and password.
  9. $ lean cloud live "My Project" --push --open
    Username: trader777
    Account id: DU1234567
    Account password: ****************
  10. Enter a weekly restart time that's convenient for you.
  11. $ lean cloud live "My Project" --push --open
    Weekly restart UTC time (hh:mm:ss) [21:00:00]: 

    You'll receive a notification on your IB Key device every Sunday to re-authenticate the connection between IB and your live algorithm. Enter a time on Sunday to receive the notification. If you don't re-authenticate before the timeout period, your algorithm quits executing. Ensure your IB Key device has sufficient battery for the time you expect to receive the notification. If you don't receive a notification, see I am not receiving IBKR Mobile notifications on the IB website.

  12. Enter whether you want to use the price data from Interactive Brokers instead of the data from QuantConnect. Enabling this feature requires you to have active Interactive Brokers market data subscriptions for all data required by your algorithm.
  13. $ lean cloud live "My Project" --push --open
    Do you want to use the Interactive Brokers price data feed instead of the QuantConnect price data feed? (yes/no): y
  14. Configure your notification settings.
  15. You can configure any combination of email, webhook, SMS, and Telegram notifications for order events and emitted insights. To view the number of notification you can send for free, see the Live Trading Notification Quotas.

    $ lean cloud live deploy "My Project" --push --open
    Do you want to send notifications on order events? [y/N]: y
    Do you want to send notifications on insights? [y/N]: y
    Email notifications: None
    Webhook notifications: None
    SMS notifications: None
    Select a notification method:
    1) Email
    2) Webhook
    3) SMS
    4) Telegram
    Enter an option: 1
    Email address: john.doe@example.com
    Subject: Algorithm notification
    Email notifications: john.doe@example.com
    Webhook notifications: None
    SMS notifications: None
    Telegram notifications: None
    Do you want to add another notification method? [y/N]: n
  16. Enable or disable automatic algorithm restarting.
  17. This feature attempts to restart your algorithm if it fails due to a runtime error, like a brokerage API disconnection.

    $ lean cloud live deploy "My Project" --push --open
    Do you want to enable automatic algorithm restarting? [Y/n]: y
  18. Set your initial cash balance.
  19. $ lean cloud live deploy "My Project" --push --open
    Previous cash balance: [{'currency': 'USD', 'amount': 100000.0}]
    Do you want to set a different initial cash balance? [y/N]: y
    Setting initial cash balance...
    Currency: USD
    Amount: 95800
    Cash balance: [{'currency': 'USD', 'amount': 95800.0}]
    Do you want to add more currency? [y/N]: n
  20. Set your initial portfolio holdings.
  21. $ lean cloud live deploy "My Project" --push --open
    Do you want to set the initial portfolio holdings? [y/N]: y
    Do you want to use the last portfolio holdings? [] [y/N]: n
    Setting custom initial portfolio holdings...
    Symbol: GOOG
    Symbol ID: GOOCV VP83T1ZUHROL
    Quantity: 10
    Average Price: 50
    Portfolio Holdings: [{'symbol': 'GOOG', 'symbolId': 'GOOCV VP83T1ZUHROL', 'quantity': 10, 'averagePrice': 50.0}]
    Do you want to add more holdings? [y/N]: n
  22. Select the live node that you want to use.
  23. If you only have one idle live trading node, it is selected automatically and this step is skipped.

    $ lean cloud live deploy "My Project" --push --open
    Select a node:
    1) L-MICRO node 89c90172 - 1 CPU @ 2.4GHz, 0.5GB Ram
    2) L-MICRO node 85a52135 - 1 CPU @ 2.4GHz, 0.5GB Ram
    Enter an option: 1
  24. Enter 2 to select the Interactive Brokers data provider.
  25. $ lean live "My Project"
    Select a live data feed:
    1) QuantConnect
    2) Interactive Brokers
    3) Tradier
    4) Oanda
    5) Bitfinex
    6) Coinbase Advanced Trade
    7) Binance
    8) Zerodha
    9) Samco
    10) Terminal Link
    11) Trading Technologies
    12) Kraken
    13) TDAmeritrade
    14) Polygon
    15) IEX
    16) CoinApi
    17) Bybit
    18) TradeStation
    19) Alpaca
    To enter multiple options, separate them with comma: 2

    If you select one of the following data providers, see the respective page for more instructions:

  26. Verify the configured settings and confirm them to start the live deployment in the cloud.
  27. $ lean cloud live deploy "My Project" --push --open
    Brokerage: Interactive Brokers
    Project id: 1234567
    Environment: Live
    Server name: L-MICRO node 89c90172
    Server type: L-MICRO
    Live Data providers: Interactive Brokers
    LEAN version: 11157
    Order event notifications: Yes
    Insight notifications: Yes
    Email notifications: john.doe@example.com
    Webhook notifications: None
    SMS notifications: None
    Telegram notifications: None
    Initial live cash balance: [{'currency': 'USD', 'amount': 95800.0}]
    Initial live portfolio holdings: [{'symbol': 'GOOG', 'symbolId': 'GOOCV VP83T1ZUHROL', 'quantity': 10, 'averagePrice': 50.0}]
    Automatic algorithm restarting: Yes
    Are you sure you want to start live trading for project 'My Project'? [y/N]: y
  28. Inspect the result in the browser, which opens automatically after the deployment starts.

Follow these steps to see the live status of a project:

  1. Log in to the CLI if you haven't done so already.
  2. Open a terminal in the organization workspace that contains the project.
  3. Run lean cloud status "<projectName>" to show the status of the cloud project named "<projectName>".
  4. $ lean cloud status "My Project"
    Project id: 1234567
    Project name: My Project
    Project url: https://www.quantconnect.com/project/1234567
    Live status: Running
    Live id: L-1234567a8901d234e5e678ddd9b0123c
    Live url: https://www.quantconnect.com/project/1234567/live
    Brokerage: Interactive Brokers
    Launched: 2021-06-09 15:10:12 UTC

Data Provider

For more information about the IB data provider, see Interactive Brokers.

Troubleshooting

The following table describes errors you may see when deploying to IB:

Error Message(s)Possible Cause and Fix
Login failed.
The credentials you provided are incorrect. Typically, the password contains leading and/or trailing white spaces. Copy the password to a text editor to ensure the password is correct. If you can't log in to Trader Workstation (TWS) with your credentials, contact IB. If you can log in to TWS but can't log in to the deployment wizard, contact us and provide the algorithm Id and deployment Id.
An existing session was detected and will not be automatically disconnected.

Historical Market Data Service error message: Trading TWS session is connected from a different IP address.
IB still recognizes your previous live deployment as being partially connected. It can take a minute to fully disconnect. For more information, see Security and Stability > Connections.
The two factor authentication request timed out.

A security dialog was detected for Code Card Authentication.
You haven't replied to the two factor authentication requests. The code card authentication is triggered when you don't reply to the IB mobile 2FA requests. Ensure your IB Key device has sufficient battery for the time you expect to receive the notification. If you don't receive a notification, see I am not receiving IBKR Mobile notifications on the IB website.
API support is not available for accounts that support free trading.
Upgrade your plan from IBKR Lite to IBKR Pro.
No security definition has been found for the request.
Your algorithm added an invalid security. For example, a deslisted stock, an expired contract, or inexistent contract (invalid expiration date or strike price). If the security should be valid, contact us and provide the algorithm Id and deployment Id.
Requested market data is not subscribed.

Historical Market Data Service error message: No market data permissions for ...
Your algorithm uses the Interactive Brokers Data Provider, but you don't have a subscription to it. Subscribe to the data bundle you need, contact IB, or re-deploy the algorithm with a different data provider. Try the QuantConnect or the hybrid QuantConnect + Interactive Brokers data providers on QuantConnect Cloud or try a third-party provider.
Timeout waiting for brokerage response for brokerage order id 37 lean id 31
IB didn't repond to an order request. Stop and re-deploy the algorithm. On the next deployment, LEAN retrieves this order or the positions it opened or closed.

To view the description of less common errors, see Error Codes in the TWS API Documentation. If you need further support, open a new support ticker and add the live deployment with the error.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: