# The highest Nth peak in a given period of bars

Can someone tell me how to find the highest Nth peak in a given period of bars in AFL , I would like to do it with the peak function. I need to know the third highest peak and I do not know how to do it.

1 Like

1 Like

Thanks for answering but I already tried it that way and it did not give me good results because I do not know how to do it with peaks

Question is what you want to do if there are NO three peaks in given number of bars?
Normally you would just call PeakBars() function with Nth parameter set to your preference and see if it returns number of bars lower than your desired range.

2 Likes

Sorry for my English, my question is that of all the peaks that there are in a certain period of time (Ej :80 periods) I would like to know the third highest.

1 Like

You did not answer the question. What if there are NO three peaks in given period. For example you have monotonically rising prices.
Also you need to define peak. Peak in the Zig zag sense or other sense?
If you mean peak in zig-zag sense then you already have the answer in my previous reply, i.e. use PeakBars().

What I mean with peak is that it is a maximum consolidated, that the price goes up and the next session goes down, with a coefficient to give it valid as a peak. What I want to do with all this are some bands based on the oscillator detrend of Dinapoli which always has more than 3 peaks in the 80 periods that I need. If it helps you, I have the programming code for the visual chart platform, which I do not understand very well.

I'm sorry @Alvaro, but this description is even more confusing - "maximum consolidated", "bands", "oscillator detrend Dinapoli".

Pls clarify if this is what you mean - using your last picture, above,

1. Look back over the last 80 bars, and save the location of all peaks
2. sort the list of peaks according to highest price
3. select/use the 3rd highest one.

You might want to think about using a combination of:

• loop - to collect x number of prior peaks, using the `PeakBars(ARRAY, change, n = 1)`, setting the `n` argument to get the prior peak number, as mentioned by @Tomasz
• save the data to a matrix or arrays
• analyse/ sort the data, and extract the bits you need

Also, I think you should address @Tomasz's question - the computer needs to know what to do if there aren't any peaks in the 80 bars - think defensively, ie. you've outlined in the pic above what you think will happen:

always has more than 3 peaks in the 80 periods

, but haven't stated what to do if there aren't any, or aren't enough peaks, eg. when price continues to rise or fall, or even sideways, without there being any peaks within the 80 bars.

(By the way, using the word "always" in regard to trading, tends to provoke scepticism - sometimes I see lots and lots of repeating patterns on charts, then I write a function to identify all instances of them, in order to get a better sense of whether it's real or not, only to find that there are subtle variations which aren't highlighted/ don't conform -> chase rabbits down holes, start again, or move-on)

1 Like

That is just what I want to do. And what would you do if there are no 3 peaks in 80 periods for example to leave the previous value until there are 3 peaks again.

@Alvaro, thanks for the invitation to live your life, but you raised the question, you need to solve it. Your invitation sounds like the sort of thing a "free loader" or amateur psychologist would say.

So far, all you've done is ask a seemingly impossible question, claiming that you've already tried different things, but produced no evidence of it, and asking other to solve your problems.

You've been given lots of free and highly relevant advice, and now want others to work for you for free.

Demonstrate that you're willing to help yourself by having a go, and, producing a properly asked question, and here, and here, and elsewhere on this site.

1 Like

This is the first thing I did and gives wrong values. The rest that has been said in this thread I am trying to solve it although I see it very difficult still for me. I'm sorry if I bothered you, it was not my intention.

``````type_SECTION_BEGIN("Bandas SV & SC");
Cierre=Close;
Periodo=Param("Periodo",7,1,100);
r1=ParamColor("BSSC",colorWhite);
r2=ParamStyle("Style",styleLine);
r3=ParamColor( "BSSV", colorWhite );
r4=ParamStyle("Style",styleLine);

MediaMovilSimple=MA(Close,Periodo);
Detrend=Close-MediaMovilSimple;

function NthHV( array, period, nth )
{
return Percentile( array, period, (period-nth) / (period-1) * 100 );
}
Period =80;
HighestValue = HHV(Detrend, Period);
DetrendMax = NthHV(Detrend, period, nth=3 );

function NthLV( array, period, nth )
{
return Percentile( array, period,100*nth/ (period-1) );
}
period=80;
LowestValue = LLV( Detrend, period );
DetrendMin = NthLV( Detrend, period,nth=3);

m=6*MA(cierre,6)/7;
a1=DetrendMax+m;
BSSC=7*a1/6;
//BSSC=7*DetrendMax/6 + MA(cierre,6);

n=6*MA(cierre,6)/7;
b1=DetrendMin+n;
BSSV=7*b1/6;
//BSSV=7*DetrendMin/6 + MA(cierre,6);

Plot(BSSC,"BSSC",r1,r2);
Plot(BSSV,"BSSV",r3,r4);

_SECTION_END(); or paste code here
``````
2 Likes

Ok, you've had a go. That's good.

What do you mean by:

What is difficult?

You mentioned earlier that you tried the "peak" function, but it's not in the code above. Is there a technical or logical reason why you haven't used it?

Also, in your code, you use the variables `Period` and `period`, which AmiBroker recognises as the same thing, ie. it's not case sensitive - you only need one of them, to service both the `HHV()` and the `LLV()`, unless you think that they should have separate/ independent periodicities.