Wave Count - Zig Zag

Hello Experts,

I am an armature amibroker user with very little knowledge of AFL conding.
I try to calculate the upwave and downwave volume on the swing. Swing is found using Zig Zag function. I want to get the total number of upwave and downwave. Below is what I have done so far, the exploration doesnt give me the total number of DOWNWaveCount & UPWaveCount. It always returns 1.

I am not sure whether its variable scope issue and how to fix it. Please throw some lights.

UPWaveCount=0;
DOWNWaveCount=0;

_SECTION_BEGIN("Wave V.1.0");
percent = Param("Change %",0.3,0.01,3,0.1);
barcomplete = BarIndex() < LastValue(BarIndex());
j = Zig(C,percent);
Vw[0] = 0;

for(i = 3; i < BarCount; i++)
{
	if (j[i]>j[i-1] AND j[i-1]<j[i-2] AND barcomplete[i]) 
		Vw[i]=V[i];
	else
	{
		if (j[i]<j[i-1] AND j[i-1]>j[i-2] AND barcomplete[i]) 
		   Vw[i]=V[i];
	    else
		   Vw[i] =V[i]+Vw[i-1];
	}
}	

_SECTION_END();


		SwingWaveUP=IIf(j>Ref(j,-1),Vw,0);
		SwingWaveDOWN=IIf(j<Ref(j,-1),Vw,0);
		
		UPWave=Cross(SwingWaveUP,0) ;	
		DOWNWave=Cross(SwingWaveDOWN,0);

		biUPWave=ValueWhen(DOWNWave,BarIndex()-1);
		biDownWave=ValueWhen(UPWave,BarIndex()-1);	

		IIf(UPWave,DOWNWaveCount++,IIf(DOWNWave,UPWaveCount++,-1));
		
SetBarFillColor( IIf(j>Ref(j,-1),colorDarkGreen,colorDarkRed) ); 
PlotOHLC(0,SwingWaveUP,0,SwingWaveUP,"SwingWaveUP",5 , ParamStyle("Histogram style", styleHistogram|styleThick | styleNoLabel, maskHistogram ) ) ;
PlotOHLC(0,SwingWaveDOWN,0,SwingWaveDOWN,"SwingWaveDOWN",4 , ParamStyle("Histogram style", styleHistogram|styleThick | styleNoLabel, maskHistogram ) ) ;


Filter=1;

AddColumn(UPWave,"UPWave");
AddColumn(DOWNWave,"DOWNWave");
AddColumn(biDownWave,"biDownWave");
AddColumn(biUPWave,"biUPWave");
AddColumn(DOWNWaveCount,"DOWNWaveCount");
AddColumn(UPWaveCount,"UPWaveCount");
AddColumn(BarIndex(),"Bar Index");

I suggest you to read about Cum() function

1 Like

Thank you for the pointer Seb!