hi guys, this is probably easy, i probably had the problem before but I forgot why it happens.
so I have the code shown below. What I want to do is keep track of the average distance between peaks. So if at a peak the distance between the current peak and the previous peak is stored at the bar where the peak occurs, using:
IIf( pk, ( px1 - px2 ) / 2, 0 )
there are values at the location of the peaks.
But if I now do:
averageDistancePeaks = Cum( IIf( pk, ( px1 - px2 ) / 2, 0 ) );
the array is filled with zeros.
yet if I use:
averageDistancePeaks = Cum( IIf( pk, 1, 0 ) );
it counts the number of peaks just fine. What is the reason?
thanks, Ed
bi = x = BarIndex();
fvb = FirstVisibleValue( x );
lvb = LastVisibleValue( x );
rightstrength = Param( "Right Strength", 5, 2, 50, 1 );
leftstrength = Param( "Left Strength", 5, 2, 50, 1 );
fact = Param( "Chart Time Frame Factor", 1, 1, 10, 1 );
rightStrength = rightStrength * fact;
leftStrength = leftStrength * fact;
pk = H > Ref( HHV( H, leftstrength ), -1 ) AND Ref( HHV( H, rightstrength ), rightstrength ) <= H;
tr = L < Ref( LLV( L, leftstrength ), -1 ) AND Ref( LLV( L, rightstrength ), rightstrength ) >= L;
for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk, H, i ) );
VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
PlotShapes( shapeSmallCircle * tr, colorGreen, 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10 );
"IIf( pk, ( px1 - px2 ) / 2, 0 ): " + IIf( pk, ( px1 - px2 ) / 2, 0 );
"Cum( IIf( pk, ( px1 - px2 ) / 2, 0 ) ): " + Cum( IIf( pk, ( px1 - px2 ) / 2, 0 ) );
"Cum( pk ): " + Cum( pk );
averageDistancePeaks = Cum( IIf( pk, ( px1 - px2 ) / 2, 0 ) ) / Cum( pk );
"averageDistancePeaks: " + averageDistancePeaks;