BarIndex and Timenum of Day Before Yesterday's HHV Volume Bar In 5 Min TF

The Code attached Below finds the proper Value of Highest High Volume of DAY BEFORE YESTERDAY in 5 min TF.

I am clueless for the Specific bar's Time number and Bar Index..

Help would be Appreciated..


DailyMaxVolume = TimeFrameCompress(V, inDaily, compressHigh);
DBYMaxVolume = TimeFrameExpand(Ref(DailyMaxVolume ,-2), inDaily, expandFirst); // Day Before Yesterday's Highest Volume Bar In 5min Time Frame

AddColumn(DBYMaxVolume,"Day Before Yesterday HHV Vol",1.2);


Gloria Filamino

Really, think about it. Your code calculates daily bar volume.
And Volume is getting accumulated!
So the highest daily volume is always the one at the end of market session.

If you want to buy eggs at Walmart (do they have ones?) and you go to the checkout then the highest number of eggs is the one that you have paid for after leaving Walmart. The datetime is always the checkout time (your personal market session end). That's where you would have the highest number of eggs (your highest volume). Weird example but I think you get it.

dn = DateNum();
newday = dn != Ref(dn, -1);
end_dt = ValueWhen(Ref(newday, 1), dt, 2);
printf( DateTimeToStr(SelectedValue(end_dt)) );

On the other hand if you want to get the bar/datetime/time of the highest 5-min volume during the day two day ago then you could go like this

/// @link
dt = DateTime();
dn = DateNum();
newday = dn != Ref(dn, -1);
hh_vol = HighestSince(newday,V);
vol_equal = Almostequal(hh_vol, V);
dt_vol = ValueWhen(vol_equal, dt, 3);
printf( DateTimeToStr(SelectedValue(dt_vol)) );


I have just seen that you use compressHigh but not compressVolume.
So forget about my egg text.... :slight_smile:

Still the second code I have posted does what you are looking for. If instead of DateTime you want get Barindex or Timenum or ... then simply replace them with 'dt' variable as well as adjust printf output.

1 Like

I tried by Replacing dt variable as Bar Index.
Still i am not getting the result what i am looking For..It is always picking the First bar of Day Before Yesterday.
What i am Looking for is,the BAR INDEX of Highest Volume bar of DAY BEFORE YESTERDAY in 5min TF.
If i know the BAR INDEX,then i will find the OHLC of that specific bar using Valuewhen.


I have broken down from what I understand you are looking for Highest Volume Bar, and preferably the BarIndex of that bar in 5min TF in the Day before yesterday.
The code is not concise but broken into baby steps so we can see what is happening.

DN = DateNum();    // get the Date Array

NewD = DN != Ref( DN, -1);    // Look for New Day condition

DayInRef = ValueWhen( NewD, DN, 3); // We reference the 3rd value
// that is day before yesterday

theDate = LastValue( DayInRef );   // This is the Date of Day before yest

vol2d = IIf( theDate == DN, V, 0);   // Temp Array for Vol of that entire Day

HVol2d = HighestSince( DN < theDate, vol2d); // Now get highest Vol of that day

BarIndexHV = ValueWhen( vol2d == LastValue(HVol2d), BarIndex(), 1);
// Get BarIndex of that Bar

printf("Date=%.0f Highest V=%g , Barindex=%g", theDate , LastValue(HVol2d), LastValue( BarIndexHV));
// Date here is in DateNum format, you can change it to human readable if reqd.

// In my case QuickAFL was kicking in so increased the Bars required.


Watch window

We can see that the Highest Vol is calculated and returned through the rest of the succeeding bars.

From the Data I have, 23rd today, 22 was yest, and 21st becomes Day before yest.

//only on current Day or else beyond yesterday HHV get -1e+010
Bi = Barindex();

dailyMaxVolume = TimeFrameCompress(V, inDaily, compressHigh);
prevDayMaxVolume = TimeFrameExpand(Ref(dailyMaxVolume ,-1), inDaily, expandFirst);

Bi_vol = ValueWhen(LastVisibleValue(prevDayMaxVolume) == Volume, Bi, 1);

printf("\n Barindex: %g",Bi);
printf("\n Yesterday HHV BarIndex: %g",Bi_vol);
1 Like

Yes, there was a problem.

IMO, a code has to work on both, arrays and picking elements of that array (LastValue, SelectedValue, .., subscripts). Once you have array you can still get elements later. Advantage of working array version is that it works for picking any element.

The upper two ones work on choosing specific element only.

Now after breakfast here is working one (works on array as well as picking any element of array).

BTW, @travick you can not just set some random value to SetBarsRequired() without thinking. You have to calculate SetBarsRequired value based on actual requirement of your formula. What if you look back 5 days from 1 minute interval and you have zoomed in to just last day? Right, it will not pick proper looked for value by using 500.

So actual line should be like


Anyway here is whole code (picking datetime, barindex, timenum)

/// @link
function PrevArray(Cond, searchedarray, daysback, expandmode) {
	arr = Iif(Cond, searchedarray, -1);
	to_daily = TimeFrameCompress(arr, inDaily, compressHigh);
	to_daily = TimeFrameExpand(Ref(to_daily,-daysback), inDaily, expandFirst);
	return to_daily;

/// code supposed to be for INTRADAY intervals

bi = BarIndex();
dt = DateTime();

hh_vol = TimeFrameCompress(V, inDaily, compressHigh);
hh_vol = TimeFrameExpand(hh_vol, inDaily, expandFirst);

vol_equal = Almostequal(hh_vol, V);

dt_prev = PrevArray(vol_equal, dt, daysback = 2, expandmode = expandfirst);
printf( DateTimeToStr(SelectedValue(dt_prev)) );

bi_prev = PrevArray(vol_equal, bi, daysback, expandmode);
printf( "\nBi: %g", SelectedValue(bi_prev) );

tn_prev = PrevArray(vol_equal, Timenum(), daysback, expandmode);
printf( "\nTimenum: %g", SelectedValue(tn_prev) );

Plot( V, "V", colorDefault, styleHistogram, 0, 1, 0, 2);
Plot( hh_vol, "V", colorRed, styleHistogram );
Plot( vol_equal, "V", colorOrange, styleHistogram | styleOwnScale, 0, 1, 0, 1 );

Plot( BarIndex(), "BI", colorDefault, styleOwnScale );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );

// if you want to decrease bars (TimeFrameCompress deactivates QuickAFL)



Thanks to Fxshrat and Travick.

I am using the FXshrat's one and that worked exactly how i wanted to.

Apologies for replying late.Sparing time and helping others is simply incredible.Awesome guys,
I need to Thank all those who have helped me to find the way out.
Special Thanks to Fxshrat,who has helped me in the past too.

I played around with the AFL to find out if i can use the Same set of codes to find the Day Before Yesterday's High and Low bars' BI and TN in 5 Mins TF.That would be useful for me,But i couldn't.
That caused the late reply.

I have another request of Similar kind,I have described it Below..
The following code does that for Previous day,But i don't know how to convert it for Day Before Yesterday.
It would be great if somebody can alter it to suit for Day Before Yesterday.

dn = DateNum();
S  = Ref(Day(),0) != Ref(Day(),1);
Bs  = Ref(BarsSince(S),-1)+1;

PH  = ValueWhen(S,HHV(High,Bs));//Prv Day High.
PHI = ValueWhen(S,BarIndex()-HHVBars(High,Bs))+1; //Prv Day High Bar Index.
TH  = Ref(ValueWhen(S,ValueWhen(ValueWhen(S,PHI,0) == BarIndex()+1,TimeNum())),-1); //Prv Day High Bar's Time Number.
PrvDayHighBarOpen=Ref(ValueWhen(S,ValueWhen(ValueWhen(S,PHI,0) == BarIndex()+1,O)),-1); // Prv day High Bar's Open
PrvDayHighBarClose=Ref(ValueWhen(S,ValueWhen(ValueWhen(S,PHI,0) == BarIndex()+1,C)),-1);// Prv day High Bar's Close

AddColumn(PH,"Prv High",1.2);
AddColumn(PHI,"Prv High BI",1);
AddColumn(TH,"High TN",1);

Sincere Thanks,
Gloria Filamino

You can read here about the third parameter of valuewhen , 3rd parameter would bring the old or future signal with its accompanying array

S   = Ref(Day(),0) != Ref(Day(),1);
Bs  = Ref(BarsSince(S),-1)+1;

PH  = ValueWhen(S,HHV(High,Bs),2);
PHI = ValueWhen(S,BarIndex()-HHVBars(High,Bs),2)+1;
TH  = Ref(ValueWhen(S,ValueWhen(ValueWhen(S,PHI,-1) == BarIndex()+1,TimeNum()),2),-1);

PL  = ValueWhen(S,LLV(Low,Bs),2);
PLI = ValueWhen(S,BarIndex()-LLVBars(Low,Bs),2)+1;
TL  = Ref(ValueWhen(S,ValueWhen(ValueWhen(S,PLI,-1) == BarIndex()+1,TimeNum()),2),-1);

AddColumn(PH,"Prv High",1.2);
AddColumn(PHI,"Prv High BI",1);
AddColumn(TH,"High TN",1.2);

AddColumn(PL,"Prv Low",1.2);
AddColumn(PLI,"Prv Low BI",1);
AddColumn(TL,"Low TN",1.2);
1 Like

Thanks a Lot Sebastian.
Now it is easy for me to change the days using Fibo series..1,2,3,5,8,13,21,34,55,etc....
I got what i wanted Exactly.
Thanks once again.

Gloria Filamino.