I'm trying to create a Rolling window of Consolidated bars so that I can compare yesterday's close to today's open to see if there was a gap up during market closed hours. When I run it and debug the values the values are way off. I'm not sure what I'm doing wrong as it seems like it should be pretty simple. I've attached the relevant code.
from System import *
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Data import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.Custom import *
from QuantConnect.Algorithm import *
from QuantConnect.Python import *
from QuantConnect import Market
import pandas as pd
import numpy as np
import talib
from collections import deque
class EMACrossover(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 4, 1) # Set Start Date
self.SetEndDate(2020, 7, 27)
self.SetCash(10000) # Set Strategy Cash
self.SetWarmUp(150)
self.AddEquity("AMD", Resolution.Minute, Market.USA, True, 1, False)
self.sym ="AMD"
self.consolidatedwindow = RollingWindow[TradeBar](10)
self.Consolidate("AMD", Resolution.Daily, lambda x: self.consolidatedwindow.Add(x))
def OnData(self, data):
if not all([data.Bars.ContainsKey("AMD")]):
return
if not (self.consolidatedwindow.IsReady):
return
yesterdayclose = self.consolidatedwindow[1].Close
todayopen = self.consolidatedwindow[0].Open
self.Debug(yesterdayclose)
self.Debug(todayopen)
Frederik Schauer
Hi Luke,
consider that candles - also consolidated ones - always form at the end of the respective time period. So the Rolling Window of daily candles (consolidated from minute candles) contains as latest entry the daily candle from yesterday at the time of OnData calls. So if you print the closing price from rollingWindow[1] on 15.04.2020 you are referring to the closing price on 13.04.2020 not 14.04. The opening price of rollingWindow[0] referred to on 15.04 is actually the opening price not from today (15.04) but from yesterday (14.04).
To get todays opening price you cannot use daily candles - use the first minute candle after market open instead.
You can also check out this tutorial video where he does exactly that - consolidating minute to daily data and using a rolling window to get yesterdays closing price to check for gaps.
Luke Algo
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!