Hi there,
The code is copied and pasted from the solution.py of "Accessing a Rolling WIndow" section. The Log (https://www.quantconnect.com/backtest/32193/5483863/cd7e346f8b8cbf5405740e5a12d8a5c7-log.txt) said "2018-06-29 16:00:00 Algorithm Id:(cd7e346f8b8cbf5405740e5a12d8a5c7) completed in 1.69 seconds at 77k data points per second. Processing total of 130,287 data points."
I have tried with the different symbols, StartDates and EndDates, but the results are the same: "Boot Camp: We couldn't reconcile your trades with what we expected..."
How can I solve it?
class FadingTheGap(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 11, 1)
self.SetEndDate(2018, 7, 1)
self.SetCash(100000)
self.AddEquity("TSLA", Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("TSLA", 0), self.ClosingBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 1), self.OpeningBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 45), self.ClosePositions)
self.window = RollingWindow[TradeBar](2)
def ClosingBar(self):
self.window.Add(self.CurrentSlice["TSLA"])
def OpeningBar(self):
if "TSLA" in self.CurrentSlice.Bars:
self.window.Add(self.CurrentSlice["TSLA"])
#1. If our window is not full use return to wait for tomorrow
if not self.window.IsReady:
return
#2. Calculate the change in overnight price
delta = self.window[0].Open - self.window[1].Close
#3. If delta is less than -$10, SetHoldings() to 100% TSLA
if delta < -10:
self.SetHoldings("TSLA", 1)
def ClosePositions(self):
self.Liquidate()
Yuri Lopukhov
Note that TSLA went though split 5-to-1 in the end of August, and historical prices are split-adjusted. So you need to make a small change in the solution in order to get the required results.
Yuri Lopukhov
Sorry, split was 5-for-1, not 5-to-1
Shile Wen
Hi Shinseitaro,
This is an error in the BootCamp caused by the split event in TSLA. We we fix this ASAP, however, if the error hasn't been fixed yet, please change
self.AddEquity("TSLA", Resolution.Minute)
to
self.AddEquity("TSLA", Resolution.Minute).SetDataNormalizationMode(DataNormalizationMode.Raw)
Furthermore, for any future errors in the BootCamp, feel free to move past them as BootCamp lessons aren't locked by order.
Best,
Shile Wen
Shinseitaro
Hi Shile, Thank you!
Quantitative Learner
Hi Shile,
I know we can skip this but we need to know how to solve such issues on our own.
The solution you suggested didn't work. I made the above-suggested changes and copied the code to the Algorithm Lab as a new Project and ran it and it gave the same error:
Backtest Handled Error: TLSA 0 not found in portfolio. Request this data when initializing the algorithm.
class FadingTheGap(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 11, 1)
self.SetEndDate(2018, 7, 1)
self.SetCash(100000)
self.AddEquity("TSLA", Resolution.Minute).SetDataNormalizationMode(DataNormalizationMode.Raw)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("TSLA", 0), self.ClosingBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 1), self.OpeningBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 45), self.ClosePositions)
self.window = RollingWindow[TradeBar](2)
def ClosingBar(self):
if "TSLA" in self.CurrentSlice.Bars:
self.window.Add(self.CurrentSlice["TSLA"])
def OpeningBar(self):
if "TSLA" in self.CurrentSlice.Bars:
self.window.Add(self.CurrentSlice["TSLA"])
#1. If our window is not full use return to wait for tomorrow
if not self.window.IsReady:
return
#2. Calculate the change in overnight price
delta = self.window[0].Open - self.window[1].Close
#3. If delta is less than -$2.5, SetHoldings() to 100% TSLA
if delta < -2.5:
self.SetHoldings("TLSA", 1)
def ClosePositions(self):
self.Liquidate()
Derek Melchin
Hi Quantitative Learner,
The issue occurs because TSLA is misspelled
self.SetHoldings("TLSA", 1)
To resolve the issue, we recommend using the TSLA Symbol throughout the algorithm instead of the "TSLA" string. See the attached backtest for reference.
Best,
Derek Melchin
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.
Jaco Van der Walt
Hi, I've tried that too, with no success…
Vladimir
Jaco Van der Walt,
Try this
Vladimir
Jaco Van der Walt
Thanks so much, Guys, I'll try…
Jaco Van der Walt
Hi, can you please help?
I'm running the last test of the Fading the Gap Mean Reversion Tutorial and I'm getting an error:
During the algorithm initialization, the following exception has occurred: AttributeError : 'FadingTheGap' object has no attribute 'StandardDeviation' at Initialize self.volatility = self.StandardDeviation("TSLA" in main.py: line 16 AttributeError : 'FadingTheGap' object has no attribute 'StandardDeviation'
Here is the code that I'm entering
class FadingTheGap(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 11, 1)
self.SetEndDate(2018, 7, 1)
self.SetCash(100000)
self.AddEquity("TSLA", Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose("TSLA", 0), self.ClosingBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 1), self.OpeningBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("TSLA", 45), self.ClosePositions)
self.window = RollingWindow[TradeBar](2)
#1. Create a manual Standard Deviation indicator to track recent volatility
self.volatility = self.StandardDeviation("TSLA",60) # the resolution is not known seeing that we control the data
# This works out the size of the standard deviation
def OnData(self, data):
if data["TSLA"] is not None:
#2. Update our standard deviation indicator manually with algorithm time and TSLA's close price
self.volatility.Update(self.Time, data["TSLA"].Close)
def OpeningBar(self):
if "TSLA" in self.CurrentSlice.Bars:
self.window.Add(self.CurrentSlice["TSLA"])
#3. Use IsReady to check if both volatility and the window are ready, if not ready 'return'
if not self.window.IsReady or not self.volatility.IsReady:
return
delta = self.window[0].Open - self.window[1].Close
#4. Save an approximation of standard deviations to our deviations variable by dividing delta by the current volatility value:
# Normally this is delta from the mean, but we'll approximate it with current value for this lesson.
deviations = delta / self.volatity.Current.Value
#5. SetHoldings to 100% TSLA if deviations is less than -3 standard deviations from the mean:
if deviations < -3:
self.SetHoldings ("TSLA", 1)
def ClosePositions(self):
self.Liquidate()
def ClosingBar(self):
self.window.Add(self.CurrentSlice["TSLA"])
Louis Szeto
Hi Jaco
StandardDeviation itself is a class object. It is not inherited from the algorithm class or QCAlgorithm class. You simply need StandardDeviation(…) instead of self.StandardDeviation(…)
Best
Louis
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.
Gowestyang
Hello! The problem has come back again, potentially caused by the recent stock split.
Now `SetDataNormalizationMode(DataNormalizationMode.Raw)` cannot fix it… please kindly help…
Alexandre Catarino
gowestyang ,
Thank you for your report.
We have fixed this Bootcamp task. Due to Testa's split, the overnight gap needed to be adjusted from $2.5 to $0.9. Ideally, we should express the gap as a rate of change:
However, the following lesson uses price volatility, not return volatility, and we need to be consistent with the units.
Best regards,
Alex
Did you know you can own a part of QuantConnect and shape the future of quant finance? See more and invest in at wefunder.com/quantconnect
Shinseitaro
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!