# region imports
from AlgorithmImports import *
# endregion
class EmotionalSkyBlueSalmon(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 8, 1) # Set Start Date
self.SetEndDate(2022, 8, 5)
self.SetCash(100000) # Set Strategy Cash
self.spy = self.AddEquity("SPY", Resolution.Minute, extendedMarketHours=False).Symbol
self.spyRTH = self.AddEquity("SPY", Resolution.Minute, extendedMarketHours=False).Symbol
#consolidate daily bar and rolling window
self.rollingWindow = RollingWindow[TradeBar](2)
self.rollingWindowRTH = RollingWindow[TradeBar](2)
self.Consolidate(self.spy, Resolution.Daily, self.CustomBarHandler)
self.Consolidate(self.spyRTH, Resolution.Daily, self.CustomBarHandlerRTH)
def OnData(self, data):
if not self.rollingWindow.IsReady:
return
high = self.rollingWindowRTH[0].High
highExt = self.rollingWindow[0].High
low = self.rollingWindow[0].Low
closeExt = self.rollingWindow[0].Close
close = self.rollingWindowRTH[0].Close
self.Log("High:" + str(high))
self.Log("High:" + str(highExt))
self.Log("Low:" + str(low))
self.Log("Close:" + str(close))
self.Log("EXTClose:" + str(closeExt))
def CustomBarHandler(self, bar):
self.rollingWindow.Add(bar)
def CustomBarHandlerRTH(self, barRTH):
self.rollingWindowRTH.Add(barRTH)
Hey,
I am trying to find the previous day high, low, and close but for the high/low I want to include pre/post market hours. For the Close I want it to actually be the close price at 4 PM. I feel like there is probably a much easier solution, but I tried to create two equities for SPY (one with extended hours and one without) then consolidate both to daily resolutions and use rolling windows to identify the datapoints needed.
Unfortunately the code attached isn't working as I expected. I tried calculating a high/low with and without extended hours but it is always pulling back the same price for each.
Any ideas on what I'm doing wrong or more efficient ways to get the same result?
Thanks for any help!
Louis Szeto
Hi Crafty
You need to set the extendedMarketHour argument as True. (docs) Also, do not double-subscribe to the SPY data. Try with the below snippets (select either one AddEquity line) and see the logs, you'll find out the one with extended market hour has post-market time like 4:01 pm, etc.
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.
Crafty
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!