Hi community,
I have some issues with accessing pre-market prices using a rolling window.
If I print pre-market prices using a rolling window, all pre-market prices are equal to the opening price. Why is that the case? In the example code below I aim to have scheduled event pre-market that should print the pre-market price and a on open event that should print the price at the open. Perhaps there is something wrong in my code.
Thanks in advance!
class TachyonQuantumProcessor(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 6, 1) # Set Start Date
self.SetEndDate(2019,6,5)
self.SetCash(100000) # Set Strategy Cash
self.AAPL = self.AddEquity("AAPL", Resolution.Hour, Market.USA, True, 1, True)
self.lookback = 3
self.AAPLWindow = RollingWindow[TradeBar](self.lookback)
self.Schedule.On(self.DateRules.EveryDay("AAPL"), self.TimeRules.AfterMarketOpen("AAPL",-120), self.EveryDayBuy)
self.Schedule.On(self.DateRules.EveryDay("AAPL"), self.TimeRules.AfterMarketOpen("AAPL",1), self.EveryDaySell)
def EveryDayBuy(self):
if not self.AAPLWindow.IsReady: return
self.Debug("Buy")
self.Debug(self.Time)
self.Debug(self.AAPLWindow[0])
def EveryDaySell(self):
if not self.AAPLWindow.IsReady: return
self.Debug("Sell")
self.Debug(self.Time)
self.Debug(self.AAPLWindow[0])
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if data.ContainsKey("AAPL"):
self.AAPLWindow.Add(data["AAPL"])
if not self.AAPLWindow.IsReady: return
Xin Wei
Hi Serkan,
Thank you for the question. The fact is that Hourly and Daily resolution data at QuantConnect don't support extended market hour data. Those data can only be accessed using Minute, Second, or Tick resolution data. The reason is a bit complicated. If you are curious, you can read Jared's answer in this post.
Therefore, the strategy should pull Minute resolution data instead. Then, there're two ways to proceed. We can either consolidate the minute data into hourly data so that the new hourly data would include the extended market hour data or use the minute data directly with a longer looking back period for the rolling window object. I've attached a backtest implementing the consolidation way. Please check it out.
Also, here's a tip: when in doubt, it's often helpful to check our Data Explorer for raw data. In this case, you can see that minute data includes the range of extended market hours while hourly data doesn't.
Xin
Serkan Sarayli
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!