iHighest for the highest price but....

 

What about the second highest? or the third?

My guess is, for example, get Ihighest for say.. 100 candles, if the highest is candle 10, then I would need to get iHighest from 1 to 9 and then iHighest from 11 to 100, compare the 2 new values and obtain it that way.

Is there any other way to get the second highest?... or third highest?

 
I found this code by googling it but its the same of what I explained, but its going to be dificult to find the third or fourth highest of those 100 candles I think, are there any other ideas out there or methods to do this, thank you in advance.

/**
 * Resolve the price of the second highest high over a number of bars.
 *
 * @param  int - number of bars to inspect
 *
 * @return double - price of the second highest high
 */
double secondHighestHigh(int bars) {
   if (bars < 2) return(0);
   int iFirstHigh = iHighest(NULL, NULL, MODE_HIGH, bars, 0);
   double olderHigh = 0, youngerHigh = 0;
   if (iFirstHigh < bars-1) olderHigh   = High[iHighest(NULL, NULL, MODE_HIGH, bars-iFirstHigh-1, iFirstHigh+1)];
   if (iFirstHigh >      0) youngerHigh = High[iHighest(NULL, NULL, MODE_HIGH, iFirstHigh,        0           )];
   return(MathMax(olderHigh, youngerHigh));
}
 

Create a loop and save all the highs in an array.

Sort the array.

 
Keith Watford:

Create a loop and save all the highs in an array.

Sort the array.

Well explained.

 

I kinda didnt want to use loops so it can run faster.

So use a loop, then use :ArraySort(DataArray,WHOLE_ARRAY,0,MODE_ASCEND); and chose the highs that I need right?

I maximum will need a total of 4 highest highs. Is this the only or fastest way to do so? I want to do the least amount of calculations posible.

tyvm.

 
palepalepale:

I kinda didnt want to use loops so it can run faster.

So use a loop, then use :ArraySort(DataArray,WHOLE_ARRAY,0,MODE_ASCEND); and chose the highs that I need right?

I maximum will need a total of 4 highest highs. Is this the only or fastest way to do so? I want to do the least amount of calculations posible.

tyvm.

Show us your lagging code and we'll show you why.

 
Nelson Wanyama:

Show us your lagging code and we'll show you why.

Its not lagging per say, But I do a lot of optimizations with wide ranges  on the variables so on the longrun it takes considerably more when making loops per tick. I was wondering to get the Highest 4 Highs in a 100 candles (sometimes less, I calculate that number) with an if-else conditional using high-ihighest.

 
palepalepale:

Its not lagging per say, But I do a lot of optimizations with wide ranges  on the variables so on the longrun it takes considerably more when making loops per tick. I was wondering to get the Highest 4 Highs in a 100 candles (sometimes less, I calculate that number) with an if-else conditional using high-ihighest.

I don't know what calculations are used by iHighest(), but I suspect that loops are used anyway.

Is it necessary to calculate every tick or will only on a new bar suffice?

 
Keith Watford:

I don't know what calculations are used by iHighest(), but I suspect that loops are used anyway.

Is it necessary to calculate every tick or will only on a new bar suffice?

THAT is something that I dont know how to do and your right.

I dont know how to do it mr Keith. The rest of the EA I do need to do calculations by tick.

Could you kindly point me to the right direction, My main function is done on OnTick()

How do I implement algo every new bar?

I do need to use those candle by candle results each tick so I need to declare those values as static right?

Thank you Im just an amateur programer.

 
palepalepale:

THAT is something that I dont know how to do and your right.

I dont know how to do it mr Keith. The rest of the EA I do need to do calculations by tick.

Could you kindly point me to the right direction, My main function is done on OnTick()

How do I implement algo every new bar?

I do need to use those candle by candle results each tick so I need to declare those values as static right?

Thank you Im just an amateur programer.

//+------------------------------------------------------------------+
bool IsNewBar()
  {
   static datetime barTime=0;
   datetime thisBarTime=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
   if(barTime!=thisBarTime)
     {
      barTime=thisBarTime;
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
 

SeriesInfoInteger


Returns information about the state of historical data. There are 2 variants of function calls.

b) Returns true or false depending on the success of the function run.


Function is 

SERIES_LASTBAR_DATE

which gives: Open time of the last bar of the symbol-period.


Sooo, thisBartime contains the datetime value of the opentime of the last bar, which then its compared to the opentime of the current bar.


I had to write it so I could understand it, had never seen that "series info integer", This is great help, Im very gratefull sir, have a wonderfull non-covid rest of the year.

Reason: