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:
public override void Initialize()
{
var endDate = DateTime.Today.AddDays(-1);
var startDate = endDate.AddDays(-7);
SetStartDate(startDate);
SetEndDate(endDate);
SetCash(100000);
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin);
_equity = AddEquity("AAPL", Resolution.Minute).Symbol;
Securities[_equity].SetDataNormalizationMode(DataNormalizationMode.Raw);
SetBenchmark(_equity);
var option = AddOption(_equity, Resolution.Minute);
option.SetFilter(-3, 3, TimeSpan.FromDays(15), TimeSpan.FromDays(60));
}
(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:
"data-provider": "QuantConnect.Lean.Engine.DataFeeds.DownloaderDataProvider",
"ib-account": "<paper trading account>",
"ib-user-name": "...",
"ib-password": "...",
"ib-trading-mode": "paper",
"id": "InteractiveBrokersBrokerage",
"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
Omid Rad
Here is complete log:
Jared Broad
Hi @Omid can you please post simplified code to reproduce the example. If its not reproducible with SPY/AAPL we'll be blocked from continuing. You can follow the work on this issue here:
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Omid Rad
Done.
I also posted a related question here:
Using ToolBox for downloading options data from Interactive Brokers
Omid Rad
Hi, I wanted to follow up on this. In the github issue got an update that IB does not currently provide the option symbol chain, but I cannot validate that because IB does in fact provide option chain data through API and also in Lean.Brokerages.InteractiveBrokers implementation there are function calls to request option chain. However, the code doesn't seem to hit those calls during backtesting data request.
Ashutosh
Issue fixed in the latest lean image (reference)
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Omid Rad
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!