Hi,

I'm trying to get QuantConnect Lean CLI to connect to Interactive Brokers (IBKR) and download and backtest some option data from last week.

Here's the setup:

  1. public override void Initialize()
  2. {
  3. var endDate = DateTime.Today.AddDays(-1);
  4. var startDate = endDate.AddDays(-7);
  5. SetStartDate(startDate);
  6. SetEndDate(endDate);
  7. SetCash(100000);
  8. SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin);
  9. _equity = AddEquity("AAPL", Resolution.Minute).Symbol;
  10. Securities[_equity].SetDataNormalizationMode(DataNormalizationMode.Raw);
  11. SetBenchmark(_equity);
  12. var option = AddOption(_equity, Resolution.Minute);
  13. option.SetFilter(-3, 3, TimeSpan.FromDays(15), TimeSpan.FromDays(60));
  14. }

(1) I made sure to have correct data subscriptions with IBKR and that they are active.

- NASDAQ (Network C/UTP) - North America
- NYSE (Network A/CTA) - North America
- NYSE American, BATS, ARCA, IEX, and Regional Exchanges (Network B) - North America
- OPRA (US Options Exchanges) - North America

(2) Market data is shared with the IBKR paper trading account.

(3) Mobile authentication is activated on the account.

(4) After setting up Lean CLI to use IBKR, `lean.json` has the following properties set:

  1. "data-provider": "QuantConnect.Lean.Engine.DataFeeds.DownloaderDataProvider",
  2. "ib-account": "<paper trading account>",
  3. "ib-user-name": "...",
  4. "ib-password": "...",
  5. "ib-trading-mode": "paper",
  6. "id": "InteractiveBrokersBrokerage",
  7. "data-downloader": "QuantConnect.ToolBox.IBDownloader.IBDataDownloader"

 

(5) Have Researcher Seat subscription with QuantConnect.

(6) I run the algo with the following command:

lean backtest MyProject --data-provider-historical "Interactive Brokers" --backtest-name qc-ibkr-backtest-01


and
(7) authenticate with mobile app:
    TRACE:: InteractiveBrokersBrokerage.OnIbAutomaterOutputDataReceived(): 2FA confirmation success

After running the algo, it shows that the stock data for the last 7 days get downloaded but option data fails to download. Also can confirm that from the downloaded files in the data folder.

From the logs:

DATA USAGE:: Total data requests 62 DATA USAGE:: Succeeded data requests 7 DATA USAGE:: Failed data requests 55


 

There is one error during execution:

20240514 21:08:17.198 TRACE:: InteractiveBrokersBrokerage.LookupSymbols(): Requesting symbol list for AAPL ... 20240514 21:08:17.218 ERROR:: <>c__DisplayClass9_0.b__0(): System.NullReferenceException: Object reference not set to an instance of an object. at QuantConnect.Brokerages.InteractiveBrokers.InteractiveBrokersBrokerage.LookupSymbols(Symbol symbol, Boolean includeExpired, String securityCurrency) at QuantConnect.ToolBox.IBDownloader.IBDataDownloader.GetChainSymbols(Symbol symbol, Boolean includeExpired) at QuantConnect.ToolBox.IBDownloader.IBDataDownloader.Get(DataDownloaderGetParameters dataDownloaderGetParameters) at QuantConnect.Lean.Engine.DataFeeds.DownloaderDataProvider.GetDownloadedData(IEnumerable`1 downloaderDataParameters, Symbol symbol, DateTimeZone exchangeTimeZone, DateTimeZone dataTimeZone, Type dataType)+MoveNext() in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Engine/DataFeeds/DownloaderDataProvider.cs:line 220 at QuantConnect.Lean.Engine.DataFeeds.DownloaderDataProvider.<>c__DisplayClass9_0.b__0(String s) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Engine/DataFeeds/DownloaderDataProvider.cs:line 178

 

At this point I am blocked and don't know how to fix this. Can you please help?

Thanks,
Omid

Author

Omid Rad

May 2024