Referencing One Bar Intraday


I am trying to reference one bar (the open, the high, the low and the close) within the day to make trading decisions.

The rollingwindow and history requests seem to only discuss referencing previous bars to update indicators. What I want to do is reference, say, only the 12:00pm (or the 12:01pm, 12:02, etc) bar no matter what time in the day it is (obviously it would have to be after 12:00pm in my example).

I have looked through all of the rollingwindow and history requests discussions and cannot find my answer.

Thank you in advance for your help.

Update Backtest

Schedule an event for 12PM and update a high low close etc variable in there and you can reference it any time outside of the scheduler

//In the Class
public decimal Close;

//In OnData()
Schedule.On(DateRules.EveryDay(Ticker), TimeRules.At(12, 00), () =>
Close = data[Ticker].Close;

This is what I would do, not sure if there is a better way!


Thank you, William Patterson.

I haven't yet tried the approach you mentioned as I am a little confused on something.

I want to reference one candle but I want the algoirthm to start to reference that one candle after that candle resolves. In other words, I want everything to reference the 12pm candle but I want the trading to start at 12:15pm. 

Any other feedback would be appreciated. I cannot seem to figure this out.


I think this does what you want


Thank you, WIlliam Patterson.

That appears to be what I am looking for. And if I am not mistaken, I can change the "NoonBar" to anything to fit whatever time I am looking for, right?

One question: why is it that when I hover over the graph, it shows the open, high, low and the close as the same number (100,000)? 

Thank you again.


That plots the equity of the strategy, the code doesn't buy or sell anything so no changes in the equity


Shouldn't the open/high/low/close change in accordance with the underlying price?


It's plotting the portfolio value, not the stock


It works when I clone your algorithm, but then when I literally just copy/paste the code, it doesn't work for me.

The error I am getting is with "public void OnData (TradeBars data)" <-- "a namespace cannot directly contain members such as fields or methods" 


"            Schedule.On(DateRules.EveryDay("SPY"), TimeRules.At(09, 30), () =>" <-- "an object reference is required for the non-static field, method or property 'DateRules.EveryDay(Symbol)'. An object reference is required for the non-static field, method, or property 'TimeRules.At(int, int, int)'. The name 'Schedule' does not exist in the current context."

Also is this the correct code for when I eventually want to reference it to place orders based on the OHLC data?

Thank you for all of your help.

namespace QuantConnect.Algorithm.CSharp

public class BasicAlgorithm : QCAlgorithm
//define opening tradebar

public TradeBar OpenBar;

public void OnData(TradeBars data)
Schedule.On(DateRules.EveryDay("SPY"), TimeRules.At(09, 30), () =>
OpenBar = data["SPY"];
if (Time.Hour == 09 && Time.Minute == 45)
Log("Open: " + OpenBar.Open + " High: " + OpenBar.High + " Low: " + OpenBar.Low + " Close: " + OpenBar.Close);

A scheduled event can trigger code for a  specific time period every day and store the intraday bar values. Then orders can be placed using those stored values.

The difference between public void OnData(Slice data)  and public void OnData(TradeBars data) is that LEAN provides dedicated event handlers for each data type. This concept in C# is called overloading. Specifically TradeBars data will be handled using OnData(TradeBars) while the rest will be handled via OnData(Slice


I suspected it was a scheduled event, but am confused as to how I would reference the values. 

1. Can you please provide an example, where, say, I am referencing a 12:00pm-12:01pm bar, 12:00-12:05pm bar, 12:00-12:15pm bar, etc and then referencing the open, high, low and close of such bars?

To be more specific, the confusion lies in how to reference a time period of "12 TO 12:01" (one bar), "12 TO 12:05" (one bar), "12 TO 12:15" (one bar), etc. From my understanding, it seems like I would only be referencing the 12:00pm bar.

2. Would I use OnData(TradeBars) to log the data and then use OnData(Slice to reference the data? Is this understanding correct?

Thank you for your help.


While scheduled events trigger code for a specific time and day, it is important to note that this is not requesting a time period. If we want to reference the bars from 12:00-12:05, we can request historical data at minute resolution like so. This way you will have all TradeBar data from 12:00-12:05 and the OHLC values necessary can be used.

self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(12, 5), self.SpecificTime)

def SpecificTime():
data = self.History(5, Resolution.Minute)


The concept of overloading with OnData(TradeBars) and OnData(Slice) is that while Slice data contains all information for a given time slice, the TradeBar data in this time slice will be passed through OnData (TradeBars). The TradeBar data can also be called in Slice, but if OnData(TradeBars) is implemented, there should be something specific that you would like to be done to only TradeBars data.


Update Backtest


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.


This discussion is closed