I have been trying to access 0DTE options data for SPX/SPY and I have not been able to find a way to access it in a research notebook. I have copy pasted the code that I am using to get options data for SPY and SPX.
import numpy as np
import pandas as pd
qb = QuantBook()
index_symbol = qb.AddIndex("SPX", Resolution.Minute).Symbol
option = qb.AddIndexOption(index_symbol)
eq_symbol = qb.AddEquity("SPY", Resolution.Minute).Symbol
eq_option = qb.AddOption(eq_symbol)
start_time = datetime.fromtimestamp((datetime.today().timestamp()//86400)*86400) - timedelta(days=4)
end_time = start_time + timedelta(days=3)
start_date = datetime(2022, 12, 31)
canonical_symbol = Symbol.CreateCanonicalOption(eq_symbol, "SPY", Market.USA, "?SPY")
contract_symbols = qb.OptionChainProvider.GetOptionContractList(canonical_symbol, start_date)
op = qb.History(TradeBar, contract_symbols, start_time, end_time)
display(op)
print(np.sort(op.index.get_level_values('expiry').unique()))
start_date = datetime(2022, 12, 31)
canonical_symbol = Symbol.CreateCanonicalOption(index_symbol, "SPXW", Market.USA, "?SPXW")
contract_symbols = qb.OptionChainProvider.GetOptionContractList(canonical_symbol, start_date)
op = qb.History(TradeBar, contract_symbols, start_time, end_time)
display(op)
print(np.sort(op.index.get_level_values('expiry').unique()))
From the output of print(np.sort(op.index.get_level_values('expiry').unique())), it is clear that the expires are either weekly or monthly. I would be grateful if someone could tell me if quantconnect has historical data on 0DTE options. If yes, what changes can I make in the code attached above to access this data for SPY or SPX or for both. Thanks.
Mcdawgzy
Yes it does - I think you need to adjust your options filter to include all contracts.
David E
Yes, this is how I access it for live trading, not sure about research
Philip Cody Pardi
this is the code i use for both backtesting and live trading. there's a more detailed discussion of 0DTE on their discord. couldn't find much of anything on the forums as it's still so new.
Def returns
This took me a second to figure out too. I was trying to request a long stretch of minute resolution data in the research environment and was running into memory issues, so the function breaks down the history requests into chunks. You may not need this feature if the start date and end date are close together, but I don't think it'll hurt. Debug statements are left in for you to check if the returning data frames match your expectations easily.
Kshitij Kapoor
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!