So, after an important assist from John CW and an "Aha!" moment about constants 
(0.1 vs. 0.25) here is the snippet of code that I am using. I am satisfied that 
it captures Mr. Ehlers' intentions, but I think it might be interesting to 
average the peak and trough values discretely. To wit, store the past n troughs 
(peaks) in a ring buffer and use the average of the buffer.

Many thanks to those who helped out.

John

"""
Peaks = Valleys = 0.0;
for ( i = 2; i < BarCount; i++ ) {
    BP0 = BP[i];
    BP1 = BP[i-1];
    BP2 = BP[i-2];

    if ( BP1 > BP0 && BP1 > BP2 )
        Peaks[i] = BP1;
    else
        Peaks[i] = Peaks[i-1];

    if ( BP1 < BP0 && BP1 < BP2 )
        Valleys[i] = BP1;
    else
        Valleys[i] = Valleys[i-1];
}
AvgPeak = MA( Peaks, 50 );
AvgValley = MA( Valleys, 50 );

if ( ParamToggle( "Mode", "Cycle|Trend", 0 ) == 0 ){
    Plot( BP, "BP" + _PARAM_VALUES(), colorRed );
} else {
    Plot( Trend, "Trend" + _PARAM_VALUES(), colorBlue );
    Plot( AvgPeak*fraction, "Peak", colorBlack, styleLine );
    Plot( AvgValley*fraction, "Valley", colorBlack, styleLine );
}
"""

Reply via email to