hi there im testing out a simple strategy with options
i have option data from ibkr (is it possible to use ibkr data for backtest? is it possible to use ibkr paper trade mode to connect QC in live mode?)
i want to backtest it but i get
Backtest Handled Error: No data loaded for SPY because there were no tradeable dates for this security.
from clr import AddReference
from datetime import timedelta
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
class FibonacciStraddleAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 1, 1)
self.SetEndDate(2023, 7, 13)
self.SetCash(100000)
self.equity = self.AddEquity("SPY", Resolution.Daily)
self.symbol = self.equity.Symbol
self.option = self.AddOption("SPY", Resolution.Minute)
self.option.SetFilter(-2, 2, timedelta(0), timedelta(days=30))
self.sd = SymbolData(self, self.symbol)
self.option_chain = None
self.Schedule.On(self.DateRules.EveryDay(self.symbol),
self.TimeRules.AfterMarketOpen(self.symbol, 1),
Action(self.PlaceStraddle))
def OnData(self, slice):
for kvp in slice.OptionChains:
if kvp.Key != self.option.Symbol: continue
self.option_chain = kvp.Value
def PlaceStraddle(self):
if self.Time.date() > self.EndDate.date() or self.option_chain is None:
return
contracts = [i for i in self.option_chain if i.Right != OptionRight.Call]
curr_price = self.Securities[self.symbol].Price
# Find the closest fib level above and below the current price
fib_levels = [self.sd.fib_38_2, self.sd.fib_50_0, self.sd.fib_61_8]
# Find the closest fib level to the current price
closest_fib = min(fib_levels, key=lambda x: abs(x - curr_price))
# Find the closest strike to the fib level
contracts = sorted(contracts, key=lambda x: abs(x.Strike - closest_fib))
# Split contracts into puts and calls
call_contracts = [contract for contract in contracts if contract.Right == OptionRight.Call]
put_contracts = [contract for contract in contracts if contract.Right == OptionRight.Put]
if call_contracts and put_contracts and len(call_contracts) > 0 and len(put_contracts) > 0:
self.MarketOrder(call_contracts[0].Symbol, 1)
self.MarketOrder(put_contracts[0].Symbol, 1)
def OnOrderEvent(self, orderEvent):
self.Debug(str(orderEvent))
class SymbolData:
def __init__(self, algorithm, symbol):
self.algorithm = algorithm
self.symbol = symbol
self.max = algorithm.MAX(symbol, 180, Resolution.Daily)
self.min = algorithm.MIN(symbol, 180, Resolution.Daily)
self.fib_50_0 = 0
self.fib_61_8 = 0
self.fib_38_2 = 0
self.min.Updated += self.OnMin
def OnMin(self, sender, updated):
height = self.max.Current.Value - self.min.Current.Value
self.fib_50_0 = self.min.Current.Value + (height * 0.500)
self.fib_61_8 = self.min.Current.Value + (height * 0.618)
self.fib_38_2 = self.min.Current.Value + (height * 0.382)
Mia Alissi
Hi John,
From the information provided, it seems like you're trying to backtest an options trading strategy on QuantConnect using data from Interactive Brokers (IBKR).
Regarding your first question, QuantConnect does not support using external data (like IBKR data) for backtesting. QuantConnect provides its own data for backtesting. You can check out the data library provided by QuantConnect which includes equity, futures, options, crypto, CFD, and forex data.
As for your second question, yes, you can use IBKR paper trading account for live trading on QuantConnect. You can connect your IBKR paper trading account to QuantConnect for live trading. You can find more information on how to do this in the QuantConnect documentation.
Regarding the error message you're seeing, it seems like there's no data available for the SPY symbol for the dates you've specified in your backtest (from January 1, 2023, to July 13, 2023). This could be due to a number of reasons, such as the data not being available in QuantConnect's data library for those dates, or there might be an issue with the data subscription in your algorithm.
You might want to check the dates and make sure that data for SPY is available for those dates in QuantConnect's data library. If the data is available and you're still seeing the error, there might be an issue with how you're subscribing to the data in your algorithm.
In your algorithm, you're subscribing to SPY data with this line of code:
This line of code should correctly subscribe your algorithm to daily SPY data. If the data is available for your specified dates, this line of code should allow your algorithm to access the data. If you're still seeing the error, you might want to check other parts of your algorithm to make sure they're not causing issues.
I hope this helps! Let me know if you have any other questions.
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.
John
Thank you. So which data plans do i need to add on to backtest options on US equities ?
I think that should solve this issue?
John
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!