Hi - I'm trying to get the high & low of the first 5 minutes of SPY on 1st June based on the below code. For some reason when I look at the prices shown in Debug, it's showing 412.38 as the ‘low’ of the opening 5 minute range, when I expect it to be around the 415.09 price.
See image below - I expect the high & low to be around the blue cross area, but for some reason the code is picking up the blue circle as the ‘low’ of the range when it's not on 1st June.
412.38 seems to be low of final candle on 31 May - why does this issue occur and how do I fix it?
Output from the Logs:
2022-06-01 10:09:00 :Price: 412.37 // High: 416.2 // Low: 412.38 // Width: 3.819999999999993# ------------------------------------------------------------
STOCK = "SPY"; PERIOD = 5; WAIT = 0; MULTIPLIER = 1
# ------------------------------------------------------------
class Test(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 6, 1)
self.SetEndDate(2022, 6, 1)
self.SetCash(100000)
self.stock = self.AddEquity(STOCK, Resolution.Minute).Symbol
self.hh = self.MAX(self.stock, PERIOD, Resolution.Minute, Field.High)
self.ll = self.MIN(self.stock, PERIOD, Resolution.Minute, Field.Low)
self.SetWarmUp(PERIOD, Resolution.Minute)
self.openingrange_HH = 0
self.openingrange_LL = 0
self.tradeLimit = True
self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.AfterMarketOpen(self.stock, PERIOD), self.DailyCheck)
self.Schedule.On(self.DateRules.EveryDay(self.stock), self.TimeRules.BeforeMarketClose(self.stock, 5), self.Liquidate)
def DailyCheck(self):
if self.IsWarmingUp: return
self.openingrange_HH = self.hh.Current.Value
self.openingrange_LL = self.ll.Current.Value
self.tradeLimit = True
def OnData(self, data):
if self.IsWarmingUp: return
if (self.openingrange_HH == 0 or self.openingrange_LL == 0): return
price = self.Securities[self.stock].Price
risk = self.Portfolio.TotalPortfolioValue / 10000
rangewidth = self.openingrange_HH - self.openingrange_LL
shareqty = Math.Round(risk / rangewidth)
if not self.Portfolio[self.stock].Invested and self.tradeLimit == True:
if price > self.openingrange_HH:
self.SetHoldings("SPY", 1.0)
self.Debug("Price: " + str(price) + " // High: " + str(self.openingrange_HH) + " // Low: " + str(self.openingrange_LL) + " // Width: " + str(rangewidth))
Vladimir
Ivanc
All data for the first 5 minutes after market opens available at 9:36 or later.
2022-06-01 09:36:00 : Price: 415.42 // High: 416.2 // Low: 415.05If you are satisfied with my answer, please accept it and don't forget to like it.
Mcdawgzy
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!