# Find values in order

Hello,
I hope you can kindly help me solve my problem.
How can I find the HHV values ​​that are above the 100 level in order from most recent to last occurrence in the last 30 bars, if any?

as below picture

``````AddColumn( H_A , "A"  , 1.2 ) ;
AddColumn( H_B , "B"  , 1.2 ) ;
AddColumn( H_C , "C"  , 1.2 ) ;
AddColumn( H_D , "D"  , 1.2 ) ;
AddColumn( H_E , "E"  , 1.2 ) ;
``````

and how to use ( Cum ) To know their number ?

thank you for help

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

HI all

``````pack_Per = 30 ;
cciPer = 5 ;
levelup = 100 ;
leveldn = -100 ;
Zig_pram = Param ( " A_W" ,  cciPer , 2 , 50 , 1 )  ;
// 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 ) ;

ttt = Zig ( cciLp , Zig_pram ) ;

function NthHV( array, period, nth ) {
return Percentile( array, period, (period-nth) / (period-1) * 100 );
}

//////////  BarsSince (NthHV (cciLp, period, nth=2 ) ) > Ref ( cciLp , - 1)
//ValueWhen(is_almost_there, C)
Period = 30;
vale_a = BarsSince (    cciLp == HHV(cciLp, Period)   ) ;
vale_b =  BarsSince (  cciLp == NthHV(cciLp, period, nth=2 ) ) ;
vale_c =  BarsSince (  cciLp == NthHV(cciLp, period, nth=3 )  )   ;
vale_d = BarsSince (   cciLp == NthHV(cciLp, period, nth=4 )  ) ;
vale_e = BarsSince (   cciLp == NthHV(cciLp, period, nth=5 )  ) ;
bi = BarIndex();

NthHighest_H1 = IIf ( cciLp > levelup AND cciLp > Ref ( cciLp , - 1) , HHV(cciLp, Period) , 0 ) ;
NthHighest_H2 = IIf ( cciLp > levelup AND cciLp > Ref ( cciLp , - 1)  , NthHV(cciLp, period, nth=2 ) , 0 ) ;
NthHighest_H3 = IIf ( cciLp > levelup AND cciLp > Ref ( cciLp , - 1)  , NthHV(cciLp, period, nth=3 ) , 0 ) ;
NthHighest_H4 = IIf ( cciLp > levelup AND cciLp > Ref ( cciLp , - 1)  , NthHV(cciLp, period, nth=4 ) , 0 ) ;
NthHighest_H5 = IIf ( cciLp > levelup AND cciLp > Ref ( cciLp , - 1)  , NthHV(cciLp, period, nth=5 ) , 0 ) ;

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

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

Filter = 1 ;

AddColumn( NthHighest_H2 , "NthHighest_H2 "  , 1.2 ) ;
AddColumn( vale_b , "vale_b"  , 1.2 ) ;

AddColumn( NthHighest_H3 , "NthHighest_H3 "  , 1.2 ) ;
AddColumn( vale_c , "vale_c"  , 1.2 ) ;

AddColumn( NthHighest_H4 , "NthHighest_H4"  , 1.2 ) ;
AddColumn( vale_d , "vale_d"  , 1.2 ) ;

AddColumn( NthHighest_H5 , "NthHighest_H5 "  , 1.2 ) ;
AddColumn( vale_e , "vale_e"  , 1.2 ) ;

``````

how to use BarsSince() with nth ?

thank you

``````r = RSI(4);
Plot(r, "r", colorBlue, styleLine);
level = 70;
PlotGrid(level);
z = Zig(r, 25);
Plot(z, "z", colorRed, styleDots);

IsPeak = z > Ref(z, 1) AND z > Ref(z, -1) AND r > level;
PlotShapes(shapeSmallCircle * IsPeak, colorRed, 0, r, 12);

c_r = SparseCompress(IsPeak, r);
c_dt = SparseCompress(IsPeak, DateTime());

Filter = 1;

``````

2 Likes

I feel so lucky to have your support

thank you

1 Like

dear awilson
i could not get ValueWhen or barsince right

``````r = RSI(4);
Plot(r, "r", colorBlue, styleLine);
level = 70;
PlotGrid(level);
z = Zig(r, 25);
Plot(z, "z", colorRed, styleDots);

IsPeak = z > Ref(z, 1) AND z > Ref(z, -1) ;
PlotShapes(shapeSmallCircle * IsPeak, colorRed, 0, r, 12);

c_r = SparseCompress(IsPeak, r);
c_dt = SparseCompress(IsPeak, DateTime());
C_RB = Ref(c_r, -1) ;
C_RC = Ref(c_r, -2) ;
VALUE_C_R = ValueWhen ( c_r , H , 1   ) ;
VALUE_C_RB = ValueWhen ( C_RB , H , 1  ) ;
VALUE_C_RC = ValueWhen ( C_RC , H , 1 ) ;

Filter = 1;
``````

This is what you asked for

In my example I used rsi to demonstrate the technic you could use to get what you want,
and used 70 as level, because RSI maximium is 100.

``````r = RSI(4);
Plot(r, "r", colorBlue, styleLine);
level = 70;
PlotGrid(level);
z = Zig(r, 25);
Plot(z, "z", colorRed, styleDots);

IsPeak = z > Ref(z, 1) AND z > Ref(z, -1) AND r > level;
PlotShapes(shapeSmallCircle * IsPeak, colorRed, 0, r, 12);

c_r = SparseCompress(IsPeak, r);
c_dt = SparseCompress(IsPeak, DateTime());