I'm having a problem in backtesting mode.
It shows : 'NoneType' object has no attribute 'Close'
at OnData
if self.window[0].Close < self.stop_price:
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 39
However, I am doing a check by data.ContainsKey
#region imports
from AlgorithmImports import *
#endregion
class AdaptableTanCormorant(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 5, 21) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("AAPL", Resolution.Minute)
self.stop_price = None
self.target_price = None
#self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('SPY', 30),
# self.open_daily_check)
self.window = RollingWindow[TradeBar](3)
self.date_=None
return
def OnData(self, data: Slice):
if not data.ContainsKey("AAPL"):
return
self.window.Add(data["AAPL"])
if not self.Portfolio.Invested and self.window.IsReady:
first_bar=self.window[0]
second_bar=self.window[1]
third_bar=self.window[2]
if first_bar.Low == second_bar.Low and second_bar.Low == third_bar.Low:
self.SetHoldings('AAPL',1)
self.stop_price = first_bar.Low - (first_bar.High - first_bar.Low)
self.target_price = first_bar.High + (first_bar.High - first_bar.Low)*2
if self.Portfolio.Invested:
if self.window[0].Close < self.stop_price:
self.Liquidate()
self.stop_price = None
self.target_price = None
Varad Kabade
Hi Roman Gobov,
We recommend using:
or:
instead of:
for checking the data for a security in current data slice. Sometimes although the key for security may be available in the slice data, the bar data for that security may not be available.
For example, this may happen when non-price(splits, dividends, etc.) data is available for that symbol while bar data is not.
Best,
Varad Kabade
Roman Gobov
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!