# Find values in order

this what I did but the result is wrong to find the value of the peak when value greater than level + 100
and also i consider Tomasz question ( if there are NO three peaks in given number of bars ?)

for that I used ( iif ) but still I did not get it .

the code I used

``````cciPer = 5 ;
pack_Per = 30 ;

levelup = 100 ;
leveldn = -100 ;

// using fxShart cci loop code for test  ( link )
///     https://forum.amibroker.com/t/help-with-calculation-of-cci-using-loops/25087/10?u=needhelp

tp = Avg; // (High+Low+Close)/3;
cciMA = MA( tp, cciPer);
for ( j = 0, zum = 0; j < cciPer; j++) {
zum += abs(cciMA - Ref(tp,-j));
}
md2 = zum / cciPer;
cciLp = (tp - cciMA) / (.015 * md2);

fff= cciLp ;
cciLp_bar= Prec ( fff , 2 ) != Ref ( Prec ( fff , 2 ) ,-1 );
Total_cciLp_bars = Cum ( cciLp_bar ) ;

function NthHV( array, period, nth ) {
return Percentile( array, period, (period-nth) / (period-1) * 100 );
}
Zig_pram = Param ( " A_W" ,  cciPer , 2 , 50 , 1 )  ;
ttt = Zig ( cciLp , Zig_pram ) ;

/////////////  https://forum.amibroker.com/t/the-highest-nth-peak-in-a-given-period-of-bars/10544/4?u=needhelp
y_A =  IIf ( fff > levelup , peakbars(cciLp,cciPer,1) , 0 ) ;
y_B =  IIf ( fff > levelup , peakbars(cciLp,cciPer,2) , 0 ) ;
y_C =  IIf ( fff > levelup , peakbars(cciLp,cciPer,3) , 0 ) ;
y_D =  IIf ( fff > levelup , peakbars(cciLp,cciPer,4) , 0 ) ;

Plot(ttt,"ttt",colorBlack,styleLine);

sss= StrToNum (NumToStr ( Total_cciLp_bars  ) );
Period = IIf ( sss < pack_Per , sss , pack_Per ) ;
H_A = ValueWhen ( y_A , fff ) ;
H_A_BARSINCE =   y_A ;
H_B =  ValueWhen ( y_B , fff )  ;
H_B_BARSINCE =  y_B ;
H_C =   ValueWhen ( y_C , fff )  ;
H_C_BARSINCE =  y_C ;
H_D =   ValueWhen ( y_D , fff ) ;
H_D_BARSINCE =  y_D ;

Filter = 1 ;
AddColumn( H_A_BARSINCE , "H_A_BARSINCE"  , 1.2 ) ;
AddColumn( H_A , "H_A"  , 1.2 ) ;

AddColumn( H_B_BARSINCE , "H_B_BARSINCE "  , 1.2 ) ;
AddColumn( H_B , "H_B"  , 1.2 ) ;

AddColumn( H_C_BARSINCE , "H_C_BARSINCE"  , 1.2 ) ;
AddColumn( H_C , "H_C"  , 1.2 ) ;

AddColumn( H_D_BARSINCE , "H_D_BARSINCE"  , 1.2 ) ;
AddColumn( H_D , "H_D"  , 1.2 ) ;

``````

thanks