Incremental Loop to Add Columns

Dear Group,

I am attaching below an afl from one of my subordiantes (May face a flak for doing so without any rectifcation), but the idea is to understand the same from a layman's perspective:

barssofartoday = BarsSince( Day() != Ref(Day(), -1));


startdate = ParamDate( "Start date", Now(0));
enddate = ParamDate( "End date", Now(0));

dn = datenum();
datewindow = dn >= startdate AND dn <= enddate;

Filter=datewindow;
//Filter = (Day() != Ref(Day(), -1));
AddColumn(Barssofartoday," Barssofartoday");
totalvolume15=TimeFrameGetPrice("V",in15Minute,-1);


totalvolume=Sum(totalvolume15,barssofartoday);
avragevolume=(totalvolume) / (barssofartoday);
volumeratio=totalvolume15/avragevolume;

//c1=ValueWhen(barssofartoday==1,totalvolume15);

//i = 1; 
//do
{
 //c1=ValueWhen(barssofartoday=i,totalvolume15); 
  
} 
//while( ++i < BarCount );
AddColumn(totalvolume,"Total Volume");
v1=ValueWhen(barssofartoday==1,totalvolume15);
v2=ValueWhen(barssofartoday==2,totalvolume15);
v3=ValueWhen(barssofartoday==3,totalvolume15);
v4=ValueWhen(barssofartoday==4,totalvolume15);
v5=ValueWhen(barssofartoday==5,totalvolume15);
v6=ValueWhen(barssofartoday==6,totalvolume15);
v7=ValueWhen(barssofartoday==7,totalvolume15);
v8=ValueWhen(barssofartoday==8,totalvolume15);
v9=ValueWhen(barssofartoday==9,totalvolume15);
v10=ValueWhen(barssofartoday==10,totalvolume15);
v11=ValueWhen(barssofartoday==11,totalvolume15);
v12=ValueWhen(barssofartoday==12,totalvolume15);
v13=ValueWhen(barssofartoday==13,totalvolume15);
v14=ValueWhen(barssofartoday==14,totalvolume15);
v15=ValueWhen(barssofartoday==15,totalvolume15);
v16=ValueWhen(barssofartoday==16,totalvolume15);
v17=ValueWhen(barssofartoday==17,totalvolume15);
v18=ValueWhen(barssofartoday==18,totalvolume15);
v19=ValueWhen(barssofartoday==19,totalvolume15);

R1=ValueWhen(barssofartoday==1,volumeratio);
R2=ValueWhen(barssofartoday==2,volumeratio);
R3=ValueWhen(barssofartoday==3,volumeratio);
R4=ValueWhen(barssofartoday==4,volumeratio);
R5=ValueWhen(barssofartoday==5,volumeratio);
R6=ValueWhen(barssofartoday==6,volumeratio);
R7=ValueWhen(barssofartoday==7,volumeratio);
R8=ValueWhen(barssofartoday==8,volumeratio);
R9=ValueWhen(barssofartoday==9,volumeratio);
R10=ValueWhen(barssofartoday==10,volumeratio);
R11=ValueWhen(barssofartoday==11,volumeratio);
R12=ValueWhen(barssofartoday==12,volumeratio);
R13=ValueWhen(barssofartoday==13,volumeratio);
R14=ValueWhen(barssofartoday==14,volumeratio);
R15=ValueWhen(barssofartoday==15,volumeratio);
R16=ValueWhen(barssofartoday==16,volumeratio);
R17=ValueWhen(barssofartoday==17,volumeratio);
R18=ValueWhen(barssofartoday==18,volumeratio);
R19=ValueWhen(barssofartoday==19,volumeratio);

AddColumn(v1,"9.15 to 9.30");
AddColumn(R1,"R1");
AddColumn(v2,"9.30 to 9.45");
AddColumn(R2,"R2");
AddColumn(v3,"9.45 to 10.00");
AddColumn(R3,"R3");
AddColumn(v4,"10.00 to 10.15");
AddColumn(R4,"R4");
AddColumn(v5,"10.15 to 10.30");
AddColumn(R5,"R5");
AddColumn(v6,"10.30 to 10.45");
AddColumn(R6,"R6");
AddColumn(v7,"10.45 to 11.00");
AddColumn(R7,"R7");
AddColumn(v8,"11.00 to 11.15");
AddColumn(R8,"R8");
AddColumn(v9,"11.15 to 11.30");
AddColumn(R9,"R9");
AddColumn(v10,"11.30 to 11.45");
AddColumn(R10,"R10");
AddColumn(v11,"11.45 to 12.00");
AddColumn(R11,"R11");
AddColumn(v12,"12.00 to 12.15");
AddColumn(R12,"R12");
AddColumn(v13,"12.15 to 12.30");
AddColumn(R13,"R13");
AddColumn(v14,"12.30 to 12.45");
AddColumn(R14,"R14");
AddColumn(v15,"12.45 to 1.00");
AddColumn(R15,"R15");
AddColumn(v16,"1.00 to 1.15");
AddColumn(R16,"R16");
AddColumn(v17,"1.15 to 1.30");
AddColumn(R17,"R17");
AddColumn(v18,"1.30 to 1.45");
AddColumn(R18,"R18");
AddColumn(v19,"1.45 to 2.00");
AddColumn(R19,"R19");

//AddColumn(totalvolume,"Total Volume");
//AddColumn(avragevolume,"Arage Volume"); 
//AddColumn(totalvolume15,"15 min Volume");
//AddColumn(volumeratio,"Ratio Volume");
//AddColumn(c1,"c1");



Now what is to be achieved ->

  1. The exploration to run only for the current day in consideration
    /*
    startdate = ParamDate( "Start date", Now(0));
    enddate = ParamDate( "End date", Now(0));
    dn = datenum();
    datewindow = dn >= startdate AND dn <= enddate;
    */

  2. Incremental rise in the columns

for (i=0;i<barsasoftoday;i++)
{ Calculate & AddColumn
}

And / Or

It may show fields as blank, while giving the column name.

For repetitive code we use loop in perfect world.
For current day simply use range setting "All quotes" and Status( "lastbarinrange" ).
As for some other selected day of past.... you just need to set to Range to "From-To dates" and just choose "To" date selector of analysis tool bar instead of param (see picture).

425

/// For last ("current") day set Range to "1 recent bar" (or keep "All quotes") 
/// For past selectable dates use Range "From-To" settings and set "To" date.
/// @link https://forum.amibroker.com/t/incremental-loop-to-add-columns/10703/2
dn = DateNum();
dt = DateTime();
newday = dn != Ref(dn, -1);
todays_bars = BarsSince( newday )+1;

prev_HTF_Vol = TimeFrameGetPrice("V", in15Minute, shift = -1);

totalvolume = Sum(prev_HTF_Vol, todays_bars );
avg_volume = MA(prev_HTF_Vol, todays_bars);
volumeratio = prev_HTF_Vol / avg_volume;

selected_dn = Status("rangetodate");
last_day = dn == selected_dn;

Filter = Status( "lastbarinrange" );

AddColumn(DateTimeConvert(2,selected_dn), "Selected date", formatDateTime);
AddColumn(todays_bars, "Bars of Today");
AddColumn(totalvolume, "Total Volume");

n = 0;
for ( i = 0; i < BarCount; i++ ) {
	if ( last_day[i] /*&& n <= 20*/ ) {
		AddColumn(prev_HTF_Vol[i], DateTimeFormat("%H:%M", dt[i]));	
		n++;
	}	
}
4 Likes

Phew @fxshrat, the whole logic in one loop. Kudos & Thanks a lot.
Very much appreciated by my whole team.

_SECTION_BEGIN("Days in Week");
WeekStart = DayOfWeek() < Ref( DayOfWeek(), -1 );
DaysInWeek = BarsSince( WeekStart )+1;
DaysInWeekNum = LastValue(DaysInWeek);

DaysInPreviousWeek= ValueWhen(DaysInWeek<Ref(DaysInWeek,-1),Ref(DaysInWeek,-1),1);
DaysInPreviousWeekNum = LastValue(DaysInPreviousWeek);

DIW2WeeksAgo= ValueWhen(DaysInWeek<Ref(DaysInWeek,-1),Ref(DaysInWeek,-1),2);
DIW3WeeksAgo= ValueWhen(DaysInWeek<Ref(DaysInWeek,-1),Ref(DaysInWeek,-1),3);
DIW4WeeksAgo= ValueWhen(DaysInWeek<Ref(DaysInWeek,-1),Ref(DaysInWeek,-1),4);

_SECTION_END();

Blank ="$";
_SECTION_BEGIN("OI Price Analysis");

ATP = Aux1;

dt = DateTime();


ATPW1 = Sum(Aux1,DaysInWeek)/DaysInWeek;
ATPW2 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek))-Sum(Aux1,DaysInWeek))/DaysInPreviousWeek;
ATPW3 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo))-Sum(Aux1,DaysInWeek+DaysInPreviousWeek))/DIW2WeeksAgo;
ATPW4 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo+DIW3WeeksAgo))-Sum(Aux1,DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo))/DIW3WeeksAgo;
ATPW5 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo+DIW3WeeksAgo+DIW4WeeksAgo))-Sum(Aux1,DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo+DIW3WeeksAgo))/DIW4WeeksAgo;


Filter =1;

AddColumn(C,"C",1.2);
AddColumn(ATPW1,"W-ATP",1.2);
AddTextColumn(Blank,"$");
AddTextColumn(Blank,"$");
AddColumn(ATP,"ATP",1.2);
AddColumn(V,"V",1.0);
AddColumn(OI,"DEL",1.0);
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-1),"ATP-1");
AddColumn(Ref(V,-1),"V-1");
AddColumn(Ref(OI,-1),"DEL-1");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-2),"ATP-2");
AddColumn(Ref(V,-2),"V-2");
AddColumn(Ref(OI,-2),"DEL-2");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-3),"ATP-3");
AddColumn(Ref(V,-3),"V-3");
AddColumn(Ref(OI,-3),"DEL-3");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-4),"ATP-4");
AddColumn(Ref(V,-4),"V-4");
AddColumn(Ref(OI,-4),"DEL-4");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-5),"ATP-5");
AddColumn(Ref(V,-5),"V-5");
AddColumn(Ref(OI,-5),"DEL-5");
AddTextColumn(Blank,"$");


for ( j = 0; j < DaysinPreviousWeekNum; j++ ) 		
{		
        AddColumn(j,"j",1.0); 
		AddColumn(Aux1[j],"ATP "+DateTimeFormat("%d:%m", dt[j]),1.2);
		AddColumn(V[j],"Vol "+DateTimeFormat("%d:%m", dt[j]),1.0);
}		

AddTextColumn(Blank,"$");
AddTextColumn(Blank,"$");
		
for ( i = 0; i < DaysinWeekNum; i++ ) 		
{		
        AddColumn(i,"I",1.0); 
		AddColumn(Aux1[i],"ATP "+DateTimeFormat("%d:%m", dt[i]),1.2);
		AddColumn(V[i],"Vol "+DateTimeFormat("%d:%m", dt[i]),1.0);
}		

SetSortColumns (1);
_SECTION_END();

An add-on, above code is generating two parts, first is the manual part and other is in loops

Output

The one in loops is not referring to the correct bars.

I didn't get time to go through the whole thing, but at a glance, this is repetitive and will not make much sense.
Probably that's why you think output isn't as expected.

Just because you change the condition variables DaysinPreviousWeekNum and DaysinWeekNum , that doesn't mean they will reference difference parts of the Array.

for ( j = 0; j < DaysinPreviousWeekNum; j++ ) 
// and
for ( i = 0; i < DaysinWeekNum; i++ )

// have 
AddColumn(Aux1[j]...
AddColumn(Aux1[i]…

// and the same for 
AddColumn(V[i]

when i and j = 0, Aux1[0] will be the same for both.

In the laborious manual part of your code, you are using Ref( yy, -X) which is actually different for each column.

I don't want to start the solution part because it is still unclear what you want to do with the For loops.
Did you consider using the ATPW variables instead ?

Yes logically the second loop should be

for ( j = DaysinPreviousWeek; j < (DaysinPreviousWeekNum+DaysinWeekNum); j++ )

Ideally it would be :slightly_smiling_face:
for ( j = DaysinPreviousWeek+1; j < (DaysinPreviousWeekNum+DaysinWeekNum)+1; j++ )

As for the other part, assuming each week has 5 days the first part envisages :
the ATP, Volume & OI for first 5 days of last week + Incremental days of current week.

The second part (Still not coded) requires to find the MAX & MIN of the above 3 arrays.

You have to iterate properly as shown in my code above.
You need to understand how AFL works.
The loops you created do not work the way you want to reach your goal.
Subscripts of arrays start at zero of "row" (-> array) of length of Barcount (so indexes end at Barcount-1).
And BTW if DaysinWeekNum and DaysinPreviousWeekNum are larger than Barcount then it will return subscript out of range error. But as said the loops are wrong all the way. Remove them completely.

BTW this of your code

DayOfWeek() < Ref( DayOfWeek(), -1 );

makes same week start marker as this one of your code

DaysInWeek<Ref(DaysInWeek,-1);

and this one

AddColumn(ATP,"ATP",1.2);
AddColumn(V,"V",1.0);
AddColumn(OI,"DEL",1.0);
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-1),"ATP-1");
AddColumn(Ref(V,-1),"V-1");
AddColumn(Ref(OI,-1),"DEL-1");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-2),"ATP-2");
AddColumn(Ref(V,-2),"V-2");
AddColumn(Ref(OI,-2),"DEL-2");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-3),"ATP-3");
AddColumn(Ref(V,-3),"V-3");
AddColumn(Ref(OI,-3),"DEL-3");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-4),"ATP-4");
AddColumn(Ref(V,-4),"V-4");
AddColumn(Ref(OI,-4),"DEL-4");
AddTextColumn(Blank,"$");
AddColumn(Ref(ATP,-5),"ATP-5");
AddColumn(Ref(V,-5),"V-5");
AddColumn(Ref(OI,-5),"DEL-5");
AddTextColumn(Blank,"$");

Can be shortened to

for ( i = 0; i < 5; i++ ) {
	AddColumn(Ref(ATP,-i),"ATP-"+i,1.2);
	AddColumn(Ref(V,-i),"V-"+i, 1);
	AddColumn(Ref(OI,-i),"DEL-"+i, 1);
	AddTextColumn(Blank,"$");
}

But I have commented that loop in below overall code because the other two loops do output per week already (last week and second to last week).

Anyway this one seems to do what you are trying to achieve (I only focused on looping part).

_SECTION_BEGIN("Days in Week");
WeekStart = DayOfWeek() < Ref( DayOfWeek(), -1 );
DaysInWeek = BarsSince( WeekStart )+1;
DaysInWeekNum = LastValue(DaysInWeek);

DaysInPreviousWeek= ValueWhen(WeekStart,Ref(DaysInWeek,-1),1);
DaysInPreviousWeekNum = LastValue(DaysInPreviousWeek);

DIW2WeeksAgo= ValueWhen(WeekStart,Ref(DaysInWeek,-1),2);
DIW3WeeksAgo= ValueWhen(WeekStart,Ref(DaysInWeek,-1),3);
DIW4WeeksAgo= ValueWhen(WeekStart,Ref(DaysInWeek,-1),4);
_SECTION_END();

Blank ="$";
_SECTION_BEGIN("OI Price Analysis");

ATP = Aux1;

ATPW1 = Sum(Aux1,DaysInWeek)/DaysInWeek;
ATPW2 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek))-Sum(Aux1,DaysInWeek))/DaysInPreviousWeek;
ATPW3 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo))-Sum(Aux1,DaysInWeek+DaysInPreviousWeek))/DIW2WeeksAgo;
ATPW4 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo+DIW3WeeksAgo))-Sum(Aux1,DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo))/DIW3WeeksAgo;
ATPW5 = (Sum(Aux1,(DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo+DIW3WeeksAgo+DIW4WeeksAgo))-Sum(Aux1,DaysInWeek+DaysInPreviousWeek+DIW2WeeksAgo+DIW3WeeksAgo))/DIW4WeeksAgo;

Filter = Status( "lastbarinrange" );

SetSortColumns (1);

AddColumn(C,"C",1.4);
AddColumn(ATPW1,"W-ATP",1.2);
AddTextColumn(Blank,"$");
AddTextColumn(Blank,"$");

//for ( i = 0; i < 5; i++ ) {
//	AddColumn(Ref(ATP,-i),"ATP-"+i,1.2);
//	AddColumn(Ref(V,-i),"V-"+i, 1);
//	AddColumn(Ref(OI,-i),"DEL-"+i, 1);
//	AddTextColumn(Blank,"$");
//}

/// @link https://forum.amibroker.com/t/incremental-loop-to-add-columns/10703/8
dt = DateTime();
prev_dt = LastValue(ValueWhen(WeekStart, dt, 2));
curr_dt = LastValue(ValueWhen(WeekStart, dt));
prevweek_cond = dt >= prev_dt AND dt < curr_dt;

n = 0;
/// previous week before last week
AddTextColumn(" --->","Previous week");
for ( i = 0; i < BarCount; i++ ) {
	if ( prevweek_cond[i] && n < 5 ) {
		AddColumn(n,"j",1);
		dtstr = DateTimeFormat("%d:%m", dt[i]);  
		//AddColumn(C[i], dtstr, 1.4);			
		AddColumn(Aux1[i],"ATP "+dtstr,1.2);
		AddColumn(V[i],"Vol "+dtstr,1.0);
		AddColumn(OI[i],"OI "+dtstr,1.0);
		n++;
	}	
}		

AddTextColumn(Blank,"$");

n = 0;
/// current week last week
AddTextColumn(" --->","Last week");
for ( i = 0; i < BarCount; i++ ) {
	if ( dt[i] >= curr_dt && n < 5 ) {
		AddColumn(n,"i",1); 
		dtstr = DateTimeFormat("%d:%m", dt[i]);  
		//AddColumn(C[i], dtstr, 1.4);			
		AddColumn(Aux1[i],"ATP "+dtstr,1.2);
		AddColumn(V[i],"Vol "+dtstr,1.0);
		AddColumn(OI[i],"OI "+dtstr,1.0);
		n++;
	}	
}
_SECTION_END();

5 Likes

Thanks @fxshrat. As expected it worked out well.
Now to the second level where I wish to find the max & min of the array elements. While that's possible in excel, I will need to put the max of array in loop inside loop (because of the variable number of i)

Some help needed on how to PLOT in a loop.

/// For last ("current") day set Range to "1 recent bar" (or keep "All quotes") 
/// For past selectable dates use Range "From-To" settings and set "To" date.
/// @link https://forum.amibroker.com/t/incremental-loop-to-add-columns/10703/2

dn = DateNum();
dt = DateTime();
newday = dn != Ref(dn, -1);
todays_bars = BarsSince( newday )+1;
todaybarsnum = LastValue(todays_bars);
//check for todays_bars-1 & todays_bars above


day5_Vol  = TimeFrameGetPrice("V", inDaily, shift = -5);
day4_Vol  = TimeFrameGetPrice("V", inDaily, shift = -4);
day3_Vol  = TimeFrameGetPrice("V", inDaily, shift = -3);
earlier_FD_Vol = TimeFrameGetPrice("V", inDaily, shift = -2);
prev_FD_Vol = TimeFrameGetPrice("V", inDaily, shift = -1);
AvgVol = (prev_FD_Vol+earlier_FD_Vol+day3_Vol+day4_Vol+day5_Vol)/5;
current_FD_Vol = TimeFrameGetPrice("V", inDaily, shift = 0);
CurrentVolRatio = current_FD_Vol/prev_FD_Vol;
CurrentAvgVolRatio = current_FD_Vol/AvgVol;

prev_HTF_Vol = TimeFrameGetPrice("V", in15Minute, shift = 0); 
totalvolume = Sum(prev_HTF_Vol, todays_bars);
Incrementalvolume = totalvolume - Ref(totalvolume,-1); 
avg_volume = MA(prev_HTF_Vol , todays_bars); 
volumeratio = prev_HTF_Vol / avg_volume;

/*VWAP can be calculated as follows:
Average of high,low and close for intraday period is calculated as (High+Low+Close)/3
Obtained price from step 1 is multiplied with period’s volume
Cumulative total price is created
Cumulative volume is created
cumulative(price X volume) is divided by cumulative volume
*/

ATPCal = (High+Low+Close)/3;
ATPCal15Min = TimeFrameGetPrice("ATPCal", in15Minute, shift = 0); 
V15Min = TimeFrameGetPrice("V", in15Minute, shift = 0); 
TO15Min = V15Min * ATPCal15Min;
ATP15Min = TO15Min / V15Min;

totalvolume = Sum(V15Min, todays_bars);
totalTO = Sum(TO15Min, todays_bars);

ATPFD = totalTO / totalvolume;

/*
TodayVolume = Sum(V,todaybarsnum);
average = (H+L+C)/3;
VWAP = Sum (average * V, todaybarsnum) / TodayVolume;
*/


selected_dn = Status("rangetodate");
last_day = dn == selected_dn;

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


Filter = Status( "lastbarinrange" );
AddColumn(C,"C",1.2);
AddColumn(ATPFD,"ATPF",1.2);
AddColumn(totalVolume,"VFD",1.0);



AddColumn(DateTimeConvert(2,selected_dn), "Selected date", formatDateTime);
AddColumn(todays_bars, "Bars of Today",1.0);
AddColumn(earlier_FD_Vol,"V-2",1.0); 
AddColumn(prev_FD_Vol,"V-1",1.0);
AddColumn(current_FD_Vol,"V",1.0);
AddColumn(CurrentVolRatio,"VRatio",1.2);
AddColumn(AvgVol,"AvgV",1.0);
AddColumn(CurrentAvgVolRatio,"5AvgVRatio",1.2);
 AddColumn(totalvolume, "VCum",1.0);

n = 0;
for ( i = 0; i < BarCount; i++ ) 
{
	if ( last_day[i] /*&& n <= 20*/ ) 
	{
		AddColumn(todays_bars[i],"BarNum",1.0);
		AddColumn(totalvolume[i],"V "+DateTimeFormat("%H:%M", dt[i]),1.0);
		AddColumn(ATP15Min[i],"ATP "+DateTimeFormat("%H:%M", dt[i]),1.2);
		Plot(ATP15Min[i],"ATP",colorBlue);
		AddColumn(avg_volume[i],"AvgV "+DateTimeFormat("%H:%M", dt[0])+"-"+DateTimeFormat("%H:%M", dt[i]),1.0);
		//check for barcount, first instance.
		AddColumn(prev_HTF_Vol[i],"IncVol "+DateTimeFormat("%H:%M", dt[i]),1.0);	
		AddColumn(volumeratio[i],"VolR "+DateTimeFormat("%H:%M", dt[i]),1.2);	
		//AddColumn(n,"n");
		//AddColumn(i,"i");
		//AddColumn(dt[n],"test dt of n");
		//AddColumn(dt[i],"test dt of i");
		n++;
	}	
}

The output is seen as per below screenshot

ATP%20Plot

Ideally I wish to have it as a line joining ATP of bar after every 15 minutes, but the repetitive values too are embedded.

Similarly Can I plot the HHV & LLV of this ATP for each given day -> Staircase model as it shifts from one day to the other.

The excel output looks as follows
ATP%20Plot2

Quick Question ,
Do you use plot to visually view multiple set of straight horizontal lines ( all of them ) ?
if so read this post Plot horizontal line until crossed

You do not need loop to plot ATP for each bar!
Plot is array function and ATP is array!

As for HHV and LLV look at AB manual.
https://www.amibroker.com/guide/afl/hhv.html
https://www.amibroker.com/guide/afl/llv.html
Also you do not say which period.
So I guess you mean highest lowest since start of day at each bar of day.

/// For last ("current") day set Range to "1 recent bar" (or keep "All quotes") 
/// For past selectable dates use Range "From-To" settings and set "To" date.
/// @link https://forum.amibroker.com/t/incremental-loop-to-add-columns/10703/2

dn = DateNum();
dt = DateTime();
newday = dn != Ref(dn, -1);
todays_bars = BarsSince( newday )+1;
todaybarsnum = LastValue(todays_bars);
//check for todays_bars-1 & todays_bars above


day5_Vol  = TimeFrameGetPrice("V", inDaily, shift = -5);
day4_Vol  = TimeFrameGetPrice("V", inDaily, shift = -4);
day3_Vol  = TimeFrameGetPrice("V", inDaily, shift = -3);
earlier_FD_Vol = TimeFrameGetPrice("V", inDaily, shift = -2);
prev_FD_Vol = TimeFrameGetPrice("V", inDaily, shift = -1);
AvgVol = (prev_FD_Vol+earlier_FD_Vol+day3_Vol+day4_Vol+day5_Vol)/5;
current_FD_Vol = TimeFrameGetPrice("V", inDaily, shift = 0);
CurrentVolRatio = current_FD_Vol/prev_FD_Vol;
CurrentAvgVolRatio = current_FD_Vol/AvgVol;

prev_HTF_Vol = TimeFrameGetPrice("V", in15Minute, shift = 0); 
totalvolume = Sum(prev_HTF_Vol, todays_bars);
Incrementalvolume = totalvolume - Ref(totalvolume,-1); 
avg_volume = MA(prev_HTF_Vol , todays_bars); 
volumeratio = prev_HTF_Vol / avg_volume;

/*VWAP can be calculated as follows:
Average of high,low and close for intraday period is calculated as (High+Low+Close)/3
Obtained price from step 1 is multiplied with period’s volume
Cumulative total price is created
Cumulative volume is created
cumulative(price X volume) is divided by cumulative volume
*/

ATPCal = (High+Low+Close)/3;
ATPCal15Min = TimeFrameGetPrice("ATPCal", in15Minute, shift = 0); 
V15Min = TimeFrameGetPrice("V", in15Minute, shift = 0); 
TO15Min = V15Min * ATPCal15Min;
ATP15Min = TO15Min / V15Min;

totalvolume = Sum(V15Min, todays_bars);
totalTO = Sum(TO15Min, todays_bars);

ATPFD = totalTO / totalvolume;

/*
TodayVolume = Sum(V,todaybarsnum);
average = (H+L+C)/3;
VWAP = Sum (average * V, todaybarsnum) / TodayVolume;
*/


selected_dn = Status("rangetodate");
last_day = dn == selected_dn;

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

// Plot ATP ARRAY
Plot(ATP15Min,"ATP",colorYellow, styleStaircase, Null, Null, 0, 1);
Plot(HHV(ATP15Min, todays_bars),"HHV",colorGreen, styleStaircase, Null, Null, 0, 1);
Plot(LLV(ATP15Min, todays_bars),"LLV",colorRed, styleStaircase, Null, Null, 0, 1);

// Exploration start
Filter = Status( "lastbarinrange" );
AddColumn(C,"C",1.2);
AddColumn(ATPFD,"ATPF",1.2);
AddColumn(totalVolume,"VFD",1.0);



AddColumn(DateTimeConvert(2,selected_dn), "Selected date", formatDateTime);
AddColumn(todays_bars, "Bars of Today",1.0);
AddColumn(earlier_FD_Vol,"V-2",1.0); 
AddColumn(prev_FD_Vol,"V-1",1.0);
AddColumn(current_FD_Vol,"V",1.0);
AddColumn(CurrentVolRatio,"VRatio",1.2);
AddColumn(AvgVol,"AvgV",1.0);
AddColumn(CurrentAvgVolRatio,"5AvgVRatio",1.2);
 AddColumn(totalvolume, "VCum",1.0);

n = 0;
for ( i = 0; i < BarCount; i++ ) 
{
	if ( last_day[i] /*&& n <= 20*/ ) 
	{
		AddColumn(todays_bars[i],"BarNum",1.0);
		AddColumn(totalvolume[i],"V "+DateTimeFormat("%H:%M", dt[i]),1.0);
		AddColumn(ATP15Min[i],"ATP "+DateTimeFormat("%H:%M", dt[i]),1.5);
		AddColumn(avg_volume[i],"AvgV "+DateTimeFormat("%H:%M", dt[0])+"-"+DateTimeFormat("%H:%M", dt[i]),1.0);
		//check for barcount, first instance.
		AddColumn(prev_HTF_Vol[i],"IncVol "+DateTimeFormat("%H:%M", dt[i]),1.0);	
		AddColumn(volumeratio[i],"VolR "+DateTimeFormat("%H:%M", dt[i]),1.2);	
		//AddColumn(n,"n");
		//AddColumn(i,"i");
		//AddColumn(dt[n],"test dt of n");
		//AddColumn(dt[i],"test dt of i");
		n++;
	}	
}

12

5 Likes

@pushkan,

BTW in one of the lines of your last code you are using TimeFrameGetPrice incorrectly...

ATPCal = (High+Low+Close)/3;
ATPCal15Min = TimeFrameGetPrice("ATPCal", in15Minute, shift = 0);

If you look into the manual then you can read there that only OHLC,V, OI are supported to be used in 1st argument of that function:

The TimeFrameGetPrice - retrieves OHLCV fields from other time frames. This works immediatelly without need to call TimeFrameSet at all.
First parameter - pricefield - is one of the following: "O", "H", "L", "C", "V", "I" (open interest).

So you should not do different things than being supported.

So instead you should rather use TimeFrameSet / TimeFrameRestore/TimeFrameExpand if you want to switch to 15-minute from shorter intervals than 15-minute.
So for example,

tmfrm = in15Minute;
shift = 0;
TimeFrameSet( tmfrm );
   ATPCal = Ref((High+Low+Close)/3, -shift);
TimeFrameRestore();
ATPCal15Min = TimeFrameExpand( ATPCal, tmfrm, expandFirst );

I am not sure the rest of the code will be correct also if you change to shorter intervals than 15-minute. I am not eager to investigate that (since it is your job) but IMO you should rather ask yourself... is using Time Frame functions switching to 15-min really necessary? I don't think so. The code you posted does not suggest any benefit using short intervals than 15-min. Your main interval is 15-minute (as to be seen in your posts). So just set periodicity to 15-minute in analysis settings and chart.

And just use this (without timeframeset/....restore/...expand)

shift = 0;
ATPCal = Ref((High+Low+Close)/3, -shift);
	
TO15Min = V * ATPCal;

totalvolume = Sum(V, todays_bars);
totalTO = Sum(TO15Min, todays_bars);

ATPFD = totalTO / totalvolume;

Why do you want to use shorter intervals than 15-min and switching to 15-min from there?

2 Likes

Dear Forum,

Sorry for the late reply. Have been a bit pre-occupied with stuff other than Amibroker & hence dispensed. Apologies.

Thanks @fxshrat for both the solutions. Coming to the second one, the question " The code you posted does not suggest any benefit using short intervals than 15-min. Your main interval is 15-minute (as to be seen in your posts). So just set periodicity to 15-minute in analysis settings and chart."
Well the issue is creeping through from my data provider. While the exchange disseminates 7 data fields namely O,H,LC,V,OI & Turnover; my data provider is pushing through only the first 6. Hence the ATP, which equals Turnover / V, needs to be calculated on an assumption that its equal to the (High+Low+Close)/3. Now since V would vary on each bar, the assumption would be nearer to actual if I start calculating it from the smallest period (1-minute in this case) and then move on to the higher scale. Like you rightly pointed out, the base surely remains 15-minute. Obvious question is why rerun it then? Well in scanty volume & high ATR trades (Read price movements to the tune of 15% in matter of 10 minutes (quiet common in Indian markets these days)) the calculated ATP is much off the actual. Hence the need to rework.

So the idea would be to basically calculated the ATP on 1 minute and then average it to 15 minutes for further utilisation for calculations.

Once again sorry for the late replies. It's not to undermine the help the forum has provided, but the genuine reasons behind it

Well an add-on: Based on the above code, I wish to calculate the cumulative volume till the current bar number on previous 3 days, e.g. at current bar number say 20 today's volume is say Vtoday, I need the cumulative volume till bar number 20 for yesterday, similarly for day before ..

dn = DateNum();
dt = DateTime();
newday = dn != Ref(dn, -1);

Basically the idea would be to access the elements of earlier day Ref(dn,-1), and before that Ref(dn,-2)