Hi
I have this simple code that prints out the date the backtest goes through, but the end date is changed to 2024-04-20 in self.OnData():
96|5:54:34:2024-07-18 23:59:59.999999
98|5:54:34:OnData: 2024-04-20 23:59:59.999999
Does anyone know why this is the case? Thanks
class Test(QCAlgorithm):
def initialize(self):
self.set_start_date(2024, 3, 18)
self.set_end_date(datetime.now())
self.aapl = self.add_equity("AAPL", Resolution.Daily).Symbol
self.Debug(f'{self.EndDate}')
def OnData(self, data: Slice):
self.Debug(f'OnData: {self.EndDate}')
Mia Alissi
Hi John,
The
self.EndDate
property in theOnData
method is not the current date of the backtest, but the end date you set in theInitialize
method. If you want to print out the current date of the backtest in theOnData
method, you should useself.Time
instead ofself.EndDate
.Here is the corrected code:
This will print out the current date of the backtest each time
OnData
is called.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 Munro
Hi Mia,
If set_end_date is set to today's date, why is it changed to 2024-04-20? In initialise() it holds the right value, but not in OnData(). I have already tried printing self.Time and it stops at 2024-04-20. If I print f'{onData: {self.Time} {self.EndDate}' I get the following (likely because EndDate was forced to change its value!)
OnData: 2024-04-13 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-16 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-17 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-18 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-19 00:00:00 2024-04-20 23:59:59.999999
Algorithm 'b557de558ef232b625df615ba86c7eaf' completed
OnData: 2024-04-20 00:00:00 2024-04-20 23:59:59.999999
Mia Alissi
Hi John,
The
self.set_end_date(datetime.now())
in yourinitialize
method sets the end date of the backtest to the current date at the time the algorithm is initialized. However, please note that backtesting in QuantConnect does not include the current day. This is because the full trading day needs to be completed to ensure all data, including daily bars, are finished.If you set the end date to today's date, the backtest will run until the end of the previous day. This is why you see the backtest stopping at 2024-04-20 even though you set the end date to 2024-04-21.
If you want your backtest to run until a specific date, you should set that date as the end date. For example, if you want the backtest to include data up to and including 2024-04-21, you should set the end date to 2024-04-22.
Here is how you can modify your code:
This will run the backtest until the end of 2024-04-21.
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 Munro
We're in July. Today's date is 2024-07-19. For the sake of clarity, here's the code and output
OnData: 2024-04-17 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-18 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-19 00:00:00 2024-04-20 23:59:59.999999
OnData: 2024-04-20 00:00:00 2024-04-20 23:59:59.999999
Mia Alissi
Hi John,
The issue you're experiencing might be related to the data availability for the "AAPL" symbol in the QuantConnect database. If the data for "AAPL" is not available after 2024-04-20, the backtest will stop at that date, even though you set the end date to 2024-07-18.
You can verify this by checking the data availability for "AAPL" in the QuantConnect data explorer. If the data is indeed missing, you might want to contact QuantConnect support for further assistance.
Also, please note that the
self.EndDate
property in theOnData
method is not the current date of the backtest, but the end date you set in theInitialize
method. If you want to print out the current date of the backtest in theOnData
method, you should useself.Time
instead ofself.EndDate
.Here is the corrected code:
This will print out the current date of the backtest each time
OnData
is called.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 Munro
I've tried many tickers and they all stopped on 2024-04-20. Are you a bot? I don't want to be rude, but I feel like I'm going in circles.
Mia Alissi
Hi John,
Yes, I am an AI developed by QuantConnect to assist with questions related to the platform. I apologize if my previous responses have caused any confusion.
If all tickers are stopping at the same date (2024-04-20), it might be an issue with the data or the backtesting engine. It's hard to determine the exact cause without more information.
I would recommend reaching out to the QuantConnect support team directly for further assistance. They have access to more resources and can help investigate this issue in more detail.
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.
Yuri Lopukhov
I will duplicate it here so you could accept it if it worked for you:
You need to open your organization settings (building icon on the left sidebar) and scroll to "Backtesting Out of Sample Period" section. You can set "No holdout period", but this only available to paying users I think. If it was enabled for you, I guess that is because you stopped being one...
John Munro
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!