Hey , i need to get the high and low for a 30 min consolidated trade graph. I need to compare the current 30 min graph bar values with the previous 30 min graph value.COuld you please tell how to can i implement this.
As of now i am able to consolidate to 30 min bar graph,get the high low but don't know how to compare with the previous 30 min high,low.
Also how can i make time series plot for high and low so it will be good for visualization.
Thanks in Advance
Rahul Chowdhury
Hey Harsh,
We can use a RollingWindow to store the 2 most recent bars. We can update our rolling window each time a new 30-minute bar is available. This is done easily by using the DataConsolidated event handler for our 30-minute consolidator.
# Define and Register Consolidator
thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=30))
self.SubscriptionManager.AddConsolidator(self.spy, thirtyMinuteConsolidator)
# Set DataConsolidated Event Handler, each time a new bar is available the OnThirtyMinuteBar method will be called
thirtyMinuteConsolidator.DataConsolidated += self.OnThirtyMinuteBar
# Define Rolling Window
self.barWindow = RollingWindow[TradeBar](2)
And in our event handler method, we can add our bar to our rolling window and also access the previous bar.
def OnThirtyMinuteBar(self, sender, bar): self.barWindow.Add(bar) if not self.barWindow.IsReady: return currentBar = self.barWindow[0] previousBar = self.barWindow[1]
To plot a time series, we can use the self.Plot(chart_name, series_name, value) method.
self.Plot("My Custom Chart", "High", High)
self.Plot("My Custom Chart", "Low", Low)
Learn more about plotting in the documentation.
Harsh Yadav
THank you for the above,it cleared my doubts
in addition, i need to place an order by comparring the close of 2 consolidation of 30 min each.
for example
if currentbar.close>previousbar.high:
     setholding("SKY",1)
elif currentbar.close<previousbar.low:
    setholding("SKY",0)
Â
the above if else statement should run only once that too at 10:30(based on the current bar and previous bar) only..
and if there is any buying took place at 10:30,sell all of them at end of that day only.
I tried but am getting same result even if i chage the timming.Please help
Â
Rahul Chowdhury
Hi Harsh,
You can use scheduled events to fire code at specific times of the day. For example,
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 1), self.ClosePositions)
This creates a scheduled event which fires our method ClosePositions everyday 1 minute before market close. Learn more about scheduled events in the documentation.
Best
Rahul
Harsh Yadav
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!