How to define next pick

 

Hy Everybody,

I have the following situation:

extern double A=1.1;

double L, M;

L=(Close[1]+Close[0])/2-A;

M=(Low[1]+Low[0])/2-A;

N= (L+ M)/2;

The question: Is there any way to find out next "N" Highest or Lowest values since "N" Last Highest or Lowest values(they can happen in any amount of periods ) ?

Thanks a lot in advance to everyone.

 
Did you mean something like the following ?

/**
 * function Nfn( A, bar ) returns the N value at the given bar.
 */
double Nfn(double A,int bar)
{
    return ( ( Close[ bar+1 ] + Close[ bar ] + Low[ bar+1 ] + Low[ bar ] ) / 4 - A );
}

/**
 * function nextNPeakIndex( A, index ) returns the first index following the given bar at which the N value is higher than at index-1.
 */
int nextNPeakIndex(double A,int index)
{
    if ( index <= 0 )
        return( 0 );
    double peak = Nfn( A, index-1 );
    while ( index > 0 ) {
        index--;
	double x = Nfn( A, index-1 );
	if ( x < peak ) {
            return( index );
        }
	peak = x;
    }
    return( 0 );
}

/**
 * function nextNTroughIndex( A, index ) returns the first index following the given bar at which the N value is lower than at index-1.
 */
int nextNTroughIndex(double A,int index)
{
    if ( index <= 0 )
        return( 0 );
    double trough = Nfn( A, index-1 );
    while ( index > 0 ) {
        index--;
	double x = Nfn( A, index-1 );
        if ( x > trough ) {
            return( index );
        }
	trough = x;
    }
    return( 0 );
}
 
Only one question: Is not "bar" = "Bars" and "index" to be declared but as what?
Best regards,
georgegs69
 
No, in the function Nfn, "bar" is a local variable which is declared as a function parameter and thus assigned a value at the function call.
Likewise, "index" is a parameter in the other two functions. All those functions need to be placed as separate functions outside of the start function.

Note that there was a bug in the code (repeated twice) and I have updated it by editing my previous posting.

Though I wonder if I misunderstood what you asked for? Maybe you looked for the decision logic to tell whether this peak or trough happend most recently?
That could be something like:
if ( nextNPeakIndex( A, 4 ) == 2 ) {
    // There was a local peak in the N value at bar 2
    ...
} else if ( nextNTroughIndex( A, 4 ) == 2 ) {
    // There was a local trough in the N value at bar 2
    ...
}
Note that you can't really use Nfn( A, 0 ) for anything, because that may change at each tick; that bar is not complete. Well, if you are not worried about that temporal mismatch, then you may use it, but I wouldn't. Thus, the last effective bar to use is bar 1. Then, you can only find peaks and troughs prior to the last effective bar, and that's why I used bar 2 in the code.
 

Thnaks richplank!
Appreciate!