from AlgorithmImports import *
from datetime import datetime, timedelta
class ATMIronButterflyAlgo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2022, 3, 31)
self.SetCash(1000000)
self.spxw = self.AddIndex("SPXW", Resolution.Minute)
self.option = self.AddIndexOption("SPXW", Resolution.Minute)
self.option.SetFilter(timedelta(0), timedelta(7))
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.At(15, 00),
self.checkEntry)
def checkEntry(self):
currentPrice = self.Securities[self.spx.Symbol].Price
chain = self.OptionChain(self.option.Symbol, flatten=True)
if not chain:
self.log("no chain")
return
self.log(f"Underlying price from chain: {chain.Underlying.Price}")
Why doesn't chain.Underlying.Price work with the schdeule.on() callback? The debug log says 0.0.
Mia Alissi
The issue with accessing
chain.Underlying.Price
in theSchedule.On()
callback is likely due to the fact that the option chain data may not be available or updated at the scheduled time, resulting in a price of 0.0. Here are some steps to address this:Verify Data Availability: Ensure that the option chain data is available at the scheduled time by checking if the chain is not empty before accessing its properties.
Use Current Slice: Consider using the
current_slice
property ofQCAlgorithm
to access the latest data available at the time of the scheduled event.Log Chain Data: Add logging to confirm whether the option chain is being populated with data at the scheduled time.
Check Underlying Price: Verify if the underlying price is being updated correctly by logging the underlying security's price separately.
Adjust Schedule Timing: If necessary, adjust the timing of the scheduled event to a time when data is more likely to be available, such as after market open or before market close.
Review Documentation: Refer to the documentation on
Schedule.On()
and option chain data handling to ensure correct usage and understanding of data availability.Debugging: Use debugging techniques to step through the code execution and inspect the state of the option chain and underlying price at the scheduled time.
Here's an example of how you might adjust your code to check for data availability and log the underlying price:
This code uses
self.current_slice.option_chains.get(self.option.Symbol)
to access the option chain data, which should provide the most recent data available at the time of the scheduled event. Additionally, it logs both the underlying price from the chain and the current underlying price to help diagnose the issue.Sources: Handling Data - QuantConnect.com, US Equity Options - QuantConnect.com, US Index Options - QuantConnect.com, Greeks and IV Implementation, Index Option Call I T M Greeks Expiry Regression Algorithm
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.
Norris Codman
The current_slice doesn't seem to get any option chains. Here are the samples of logs.
2022-01-03 10:00:00 :No option chain data available.2022-01-03 10:00:00 :No option chain data available.2022-01-03 14:00:00 :No option chain data available.Norris Codman
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!