Capture the high and low of the first 5-minute candle of the day

Hi everyone,

I'm trying to build an AFL that will Capture the high and low of the first 5-minute candle of the day (at 9:15 AM) and then display those values on the chart - even when I'm viewing a higher timeframe (like 15-min, 30-min, etc.).

Here’s what I want to achieve:

  1. The logic should detect the first 5-minute bar of the day.
  2. Store that bar’s High and Low values (only once per day).
  3. Display the stored values as text (using GfxTextOut)
  4. The values must stay accurate even if I switch to a non-5-minute chart (like 15-min).

Below is the code I’ve tried. Unfortunately, it returns the high and low of the first candle of the day based on the current chart timeframe, rather than the actual 5-minute candle values I need.

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("X & Y Coordinates");

Title = " ";

td = 1.2;
td1 = 1.0;
x=Param("xposn",1,0,1000,1);
y=Param("yposn",1,0,1000,1);

_SECTION_END();	

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

TimeFrameSet(in5Minute);

fb = Day()!=Ref(Day(), -1);

val_5MinHigh = ValueWhen(fb, High);
val_5MinLow = ValueWhen(fb, Low);

TimeFrameRestore();

GfxTextOut( "5Min High:"+TimeFrameExpand(val_5MinHigh, in5Minute), x+10, y+80 );
GfxTextOut( "5Min Low:"+TimeFrameExpand(val_5MinLow, in5Minute), x+10, y+120 );

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

Plot( C, "Close", Colorred,  styleCandle | styleThick | styleDots);

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

Any help or working example would be greatly appreciated!

Thanks in advance!

Firstly, you can't access lower timeframe from a higher one in AB. Use chatgpt to know why.

There are various means to achieve this, but
it depends on next question...
Secondly, is this for one symbol or for all corresponding symbols in RTD?

1 Like

You have to open the 5 minute chart, store the high and low in static vars, and when you change to other periodicities (i.e. 15 minute), you retrieve the value stored. You HAVE TO open the 5 minute chart anyway to retrieve the 5 minute high and low as @nsm51 has mentioned.

In the second chart below (15 min) you can see the 5 minute high and low.

if (Interval() == in5Minute)
{
	fb = Day()!=Ref(Day(), -1);
	val_5MinHigh = ValueWhen(fb, High);
	val_5MinLow = ValueWhen(fb, Low);
	StaticVarSet("5min_high",val_5MinHigh);
	StaticVarSet("5min_low",val_5MinLow);
}
else
{
	val_5MinHigh = StaticVarGet("5min_high");
	val_5MinLow = StaticVarGet("5min_low");
}
	

Plot(val_5MinHigh,"val_5MinHigh", colorGreen,styleLine);
Plot(val_5MinLow,"val_5MinHigh", colorRed,styleLine);


2 Likes

This worked for me.
Thank you @Armin_Tamzarian

1 Like