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)
Vladimir
Luke Algo,
Try this, talib compatible.
Vladimir
The same one with gap chart.
Luke Algo
I tried using code similar to yours except I have the resolution of my add equity set to 1 minute as this is the time frame I want to work with other than for the consolidator.
self.AddEquity("AMD", Resolution.Minute, Market.USA, True, 1, False)
When I run it it's still coming up with strange numbers. For example for on 2020-4-15 its showing the open of the consolidated bar to be 52.16 and the previous close to be 50.94, but when I look up that date for AMD for those times it never dropped to those prices. It looks like those are the values for the day before what they should be. The value that should be today's open value is yesterdays open value and the value that should be yesterdays close close value is the day before yesterdays close value. Also, the value the close value is correct in that it is the value at market close, however the open value is the value that happened at the open of premarket and not at the open of the actual market. I have extended hours set to False in AddEquity so I'm not sure why the consolidated bars open is at the beginning of premarket.
Luke Algo
I believe I solved the problem for anyone who is trying to do something similar. Instead of using a consolidator I used object store to save the close values at the relevant times of day and delete the previous days saved data.
Shile Wen
Hi Luke,
Consolidators consolidate all of yesterday's data for one bar for today. However, we can still get today's open through the Consolidator by accessing its .WorkingBar for the current bar in progress, and I've shown this in the attached backtest.
Best,
Shile Wen
Luke Algo
Shile Wen, I've tried using your code. For some reason the data isn't matching TradingView's charts at all. I believe I may need to make it so that the bar starts at 9:30 to get that correct data. Is there a way to do this? Also, does setting the timedelta(1) make it a daily bar or do I need to add in the resolution as well?
Luke Algo
Ok, I've tried to implement the custom start and end times. There aren't many examples of it I could find, but this is what I have at this point:
I'm not sure where I went wrong, but it's giving me an error that says:
FuncPeriodSpecification: Please use a function that computes a date/time in the past (e.g.: Time.StartOfWeek and Time.StartOfMonth)
Luke Algo
I've changed the Customstart portion from above to:
def Customstart(self, dt):
start = dt.replace(day=1, hour=9, minute=30)
end = dt.replace(day=1, hour=16, minute=00)
return CalendarInfo(start, end - start)
I'm no longer getting the error and the algorithm will run, but the data doesn't seem to be right at all. On a lot of the data for the consolidated bars the price on tradingview never even went near the open or the close for the consolidated bars in the period the consolidated bar is supposed to represent. I'm still not sure what the issue is. If anyone could point me in the right direction I would greatly appreciate it. I'm basically just trying to create a daily bar out of minute bars that I can use to get the previous days market close and the open of the working bar for today's market open.
Vladimir
Is there anything wrong with getting GAP data this way?
Luke Algo
The values your way seem to be pretty close. The reason I wasn't trying to do it this way is because I have to set the after hours data to false. I don't need the after hours data right now, but in the future it would be good to be able to have the ability to turn it on if I need it for some other strategy or indicator that I program, while still having this gap finder work correctly.
Derek Melchin
Hi Luke,
To consolidate the data from open to close when subscribed to extended market data, we need to update the consolidator manually.
Instead of using a RollingWindow, a single variable will suffice because we only need to save 1 trailing bar at any given time. See the attached backtest for a working example. The cells in the notebook show the OHLC values of the consolidated bars in this example closely match the values of the regular daily bars.
Best,
Derek Melchin
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.
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!