Help with CUM( IIf

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;
1 Like

found it, sorry about that

i should have done:

diff = IIf( pk, px1 - px2, 0 );
diff = IIf( isempty( diff ), 0, diff );
averageDistancePeaks = Cum( diff ) / Cum( pk );
"averageDistancePeaks: " + averageDistancePeaks;

there are empty values in the initial diff

Simply use Nz()

diff = IIf( pk, Nz(px1 - px2), 0 );
averageDistancePeaks = Cum( diff ) / Cum( pk );
"averageDistancePeaks: " + averageDistancePeaks;

oh yes ofcourse :slight_smile: thanks

And you possibly may ignore pk count when diff is Null.

diff = px1-px2;
num = IIf( pk, Nz(diff), 0 );
denom = IIf(IsNull(diff), 0, pk);
averageDistancePeaks = Cum(num) / Cum(denom+1e-9);
"averageDistancePeaks: " + averageDistancePeaks;

yes good point, forgot about that also :slight_smile:

Or:

diff = ( pk != 0 ) *  Nz(px1 - px2);
averageDistancePeaks = SafeDivide( Cum( diff ), Cum( pk ) );
"averageDistancePeaks: " + averageDistancePeaks;

or (with assumption that pk is strictly boolean 1 or 0);

diff = pk *  Nz(px1 - px2);
averageDistancePeaks = SafeDivide( Cum( diff ), Cum( pk ) );
"averageDistancePeaks: " + averageDistancePeaks;
4 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.