Just got through the boot camp and I'm trying to build a simple test algorithm to practice some basic concepts like consolidation. For some reason though, I'm getting this error and can't figure out why… Help!
from AlgorithmImports import *
import numpy as np
class SimpleBreakoutExample(QCAlgorithm):
def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2022,8,31)
self.SetEndDate(2022,9,10)
self.symbol = self.AddEquity("PIXY", Resolution.Second).Symbol
self.Consolidate(self.symbol, Resolution.Minute, self.BarHandler)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(15, 0), self.Liquidate)
def OnData(self, data):
# if within 10 mins of first bar & price goes above opening1minhigh, buy and set stop loss at opening1minlow.
if not self.Portfolio.Invested or self.opening1minBar is not None or data.Time.hour == 9 and data.Time.minute <= 11 and self.Securities[self.symbol].Close > self.opening1minBar.high:
self.SetHoldings("PIXY", 1)
stocknumber = self.CalculateOrderQuantity("PIXY", -1)
self.Highprice = self.opening1minBar.High
# trail exit price once the price exceeds a certain amount.
if self.Portfolio.Invested:
self.stopMarketTicket = self.StopMarketOrder("PIXY", stocknumber, self.opening1minBar.Low)
if self.Securities["PIXY"].Close > self.Highprice:
Highprice = self.Securities["PIXY"].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.Securities["PIXY"].Close * 0.9
self.stopMarketticket.Update(updateFields)
def BarHandler(self, bar):
if bar.Time.hour == 9 and bar.Time.minute == 1:
self.opening1minBar = bar
'SimpleBreakoutExample' object has no attribute 'opening1minBar'
at OnData
self.Highprice = self.opening1minBar.High
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 20
Derek Melchin
Hi Michael,
This error occurs because the algorithm calls OnData every second, but opening1minBar isn't set until the first minute. To fix the issue, add
to the Initialize method and change
to
See the attached backtest for reference.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Michael Kvalvik
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!