我运行了一下代码:
from AlgorithmImports import *
class FatOrangeDog(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 4, 1)
self.SetEndDate(2020, 4, 8)
self.SetCash(100000)
self.AddEquity("AAPL", Resolution.Daily)
def OnData(self, slice: Slice):
self.Debug(slice.Time)
打印出如下结果:
2020-04-01 00:00:00
2020-04-02 00:00:00
2020-04-03 00:00:00
2020-04-04 00:00:00
2020-04-07 00:00:00
2020-04-08 00:00:00
20200404这一天是周六,on_data能获取到数据?
中间缺少了20200405和20200406,这两天分别是周日和周一而不是周末。
Mia Alissi
The
OnData
method in your algorithm is the primary event handler for receiving financial data events. It is triggered sequentially at the point in time the data is available; in backtesting and live. For daily data, this means the event is processed at midnight in backtesting or 6am the following day in live trading.However, when fill-forward is enabled for your asset, the
OnData
event handler will be called regularly even if there was no new data. This is the default behavior.In your case, it seems like the
OnData
method is being called on 2020-04-04 (Saturday) because of the fill-forward feature. The data for this day is likely the same as the data for 2020-04-03 (Friday), the last trading day.As for the missing data on 2020-04-05 (Sunday) and 2020-04-06 (Monday), it's possible that there was no new data for these days. If the stock market was closed on these days, then there would be no new data, and the
OnData
method would not be called (unless fill-forward is enabled).If you believe there should be data for these days, you might want to check your data source or contact QuantConnect support for further assistance.
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.
HONGYUZHANG
我的问题是如果我把获取数据的频率设置为天,为什么我一周只能获取到四天的数据?
from AlgorithmImports import *
class FatOrangeDog(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 7, 23)
self.SetEndDate(2023, 7, 30)
self.SetCash(100000)
self.AddEquity("AAPL", Resolution.Daily)
options = self.AddOption("AAPL",Resolution.Daily)
options.SetFilter(lambda option_filter_universe: option_filter_universe.IncludeWeeklys().Strikes(-15, 15).Expiration(0, 5))
options.PriceModel = OptionPriceModels.BinomialCoxRossRubinstein()
self.optionsymbol = options.Symbol
def OnData(self, slice: Slice):
option_chain_earnings = slice.OptionChains.get("?AAPL")
if option_chain_earnings:
self.Debug(slice.Time)
打印的结果如下:
2023-07-25 00:00:00 :2023-07-25 00:00:002023-07-26 00:00:00 :2023-07-26 00:00:002023-07-27 00:00:00 :2023-07-27 00:00:002023-07-28 00:00:00 :2023-07-28 00:00:00Mia Alissi
The
OnData
method is triggered when there is new data available for the assets in your algorithm. In your case, you are subscribing to daily data for the "AAPL" equity and its options.The
OnData
method is being triggered on 2023-07-25, 2023-07-26, 2023-07-27, and 2023-07-28. These are all weekdays, which is when the stock market is open and new data is available.The reason you are not seeing data for 2023-07-23 (Sunday), 2023-07-24 (Monday), 2023-07-29 (Saturday), and 2023-07-30 (Sunday) is likely because these are either weekends or holidays when the stock market is closed. There is no new data on these days, so the
OnData
method is not triggered.If you believe there should be data for these days, you might want to check your data source or contact QuantConnect support for further assistance.
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.
HONGYUZHANG
按照上面股票数据的逻辑我会在周六收到周五的日线数据,但是期权我为什么没有在周六收到周五的日线数据?
Mia Alissi
The behavior you're observing might be due to the difference in how equity and option data are handled in QuantConnect.
For equity data, when you're using daily resolution, the
OnData
method is triggered at midnight of the next day. If fill-forward is enabled (which is the default), you will receive data on non-trading days (like weekends), which is actually the data from the last trading day.For option data, the behavior might be different. The
OnData
method is triggered when there's new data available. If the market is closed on a certain day (like weekends or holidays), there might not be any new data, and theOnData
method might not be triggered, even if fill-forward is enabled.If you believe there's an issue with the data, you might want to check your data source or contact QuantConnect support for further assistance.
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.
HONGYUZHANG
当数据频率设置为每日,我在20230722 0时 星期六可以获取到股票数据,这个数据是星期五的数据。
但是我在星期六 0点 获取期权链,获取不到21号(星期五)过期的期权合约。
你能解释一下我如果在某一天的 0时获取期权链中的期权合约,获取的是当日的数据还是昨日的数据?期权合约的价格是是哪天的价格?
Mia Alissi
When you request data with daily resolution in QuantConnect, the
OnData
method is triggered at midnight of the next day. This means that at 0:00 on 2023-07-22 (Saturday), you are receiving the equity data for 2023-07-21 (Friday).For option data, the behavior might be different. The
OnData
method is triggered when there's new data available. If the market is closed on a certain day (like weekends or holidays), there might not be any new data, and theOnData
method might not be triggered. This could be why you are not able to retrieve the option contracts that expired on 2023-07-21 (Friday) at 0:00 on 2023-07-22 (Saturday).As for the price of the option contracts, it should be the closing price of the previous trading day. So if you're retrieving data at 0:00 on 2023-07-22 (Saturday), the price should be the closing price of 2023-07-21 (Friday).
Please note that this is the expected behavior based on the information provided in the extracts. If you believe there's an issue with the data, you might want to check your data source or contact QuantConnect support for further assistance.
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.
HONGYUZHANG
你刚才提到期权合约我在周六 零时,检索到的价格是周五的收盘价。但是我在周六零时的期权链检索不到到期日为周五的合约,这就很矛盾。期权临近到期日最后一天的数据我该怎么检索
Mia Alissi
I understand your confusion. The behavior you're observing might be due to how option contracts are handled in QuantConnect. When an option contract expires, it is removed from the option chain. This means that if an option contract expired on Friday, it would not be present in the option chain at 0:00 on Saturday.
If you need to access the data for an option contract on its expiration day, you would need to retrieve it on the day of expiration itself, before the market closes. After the market closes on the expiration day, the contract is considered expired and is removed from the option chain.
Here's an example of how you can retrieve option data on the day of expiration:
In this example,
OnData
checks the option chain for any contracts that expire on the current day and prints out their last price.Please note that this is the expected behavior based on the information provided in the extracts. If you believe there's an issue with the data, you might want to check your data source or contact QuantConnect support for further assistance.
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.
HONGYUZHANG
问题是当我的数据频率为天时,onData的触发时间仅为每天的零时,假设某个期权合约为21号过期,onData只会在21日0时触发和22日0时触发。当21日0时触发时,21日下午4点到期的期权合约还没有最终价格。当22日0时触发时,到期的合约已经被移出了期权链,我又无法访问到这些合约的最终价格。
HONGYUZHANG
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!