Detect which number candle is the highest

 

Hi All,

I am new to MQL world and need some assistance.

I need to find out which if price in the last 10 candles has touched my 20 Moving average.

I am not sure what is the best way to write this and how to do it. Can you please help?


Thanks

 
Mt4 or mt5? Do you know there is a documentation? Just look at the top of this page...
 
Carl Schreiber:
Mt4 or mt5? Do you know there is a documentation? Just look at the top of this page...


Its MT4, What do I shall I type to search this? I cant seem to get the right answer

 
SmartSarb:


Its MT4, What do I shall I type to search this? I cant seem to get the right answer


iHighest

Returns the shift of the maximum value over a specific number of bars depending on type.

int  iHighest( 
   string           symbol,          // symbol 
   int              timeframe,       // timeframe 
   int              type,            // timeseries 
   int              count,           // cont 
   int              start            // start 
  );

Parameters

symbol

[in]  Symbol the data of which should be used for search. NULL means the current symbol.

timeframe

[in]  Timeframe. It can be any of ENUM_TIMEFRAMES enumeration values. 0 means the current chart timeframe.

type

[in]  Series array identifier. It can be any of the Series array identifier enumeration values.

count=WHOLE_ARRAY

[in]  Number of bars (in direction from the start bar to the back one) on which the search is carried out.

start=0

[in]  Shift showing the bar, relative to the current bar, that the data should be taken from.

Returned value

The shift of the maximum value over a specific number of bars or -1 if error. To check errors, one has to call the GetLastError() function.

Example:

 double val; 
//--- calculating the highest value on the 20 consecutive bars in the range 
//--- from the 4th to the 23rd index inclusive on the current chart 
   int val_index=iHighest(NULL,0,MODE_HIGH,20,4); 
   if(val_index!=-1) val=High[val_index]; 
   else PrintFormat("Error in call iHighest. Error code=%d",GetLastError());
 
Mohammad Soubra:

iHighest

Returns the shift of the maximum value over a specific number of bars depending on type.

Parameters

symbol

[in]  Symbol the data of which should be used for search. NULL means the current symbol.

timeframe

[in]  Timeframe. It can be any of ENUM_TIMEFRAMES enumeration values. 0 means the current chart timeframe.

type

[in]  Series array identifier. It can be any of the Series array identifier enumeration values.

count=WHOLE_ARRAY

[in]  Number of bars (in direction from the start bar to the back one) on which the search is carried out.

start=0

[in]  Shift showing the bar, relative to the current bar, that the data should be taken from.

Returned value

The shift of the maximum value over a specific number of bars or -1 if error. To check errors, one has to call the GetLastError() function.

Example:


Mohammed, that does give me the value of the highest candle but what I after is which number candle is the highest. I.e it can be Candle 4 or 5 or 7 or 9, that is the sort of information I am after

 
SmartSarb: I need to find out which if price in the last 10 candles has touched my 20 Moving average.
For each of the 10 candles, get the high, the low and the MA. price touched if H >= MA && MA >= L
 
whroeder1:
For each of the 10 candles, get the high, the low and the MA. price touched if H >= MA && MA >= L

Thanks Whraeder1, is there a loop that I can write to get the MA value of the last 10 cancles and compare with the value of last 10 Moving average value? That would make my code a lot simpler and shorter else I will have to write a long long code.
 
SmartSarb:

Thanks Whraeder1, is there a loop that I can write to get the MA value of the last 10 cancles and compare with the value of last 10 Moving average value? That would make my code a lot simpler and shorter else I will have to write a long long code.

void OnStart()
{
//---'   '
   int high_touch_count=0; // count of the number of bars that is greater than the MA
   MqlRates rates[];
   ArraySetAsSeries(rates,true);
   CopyRates(Symbol(),Period(),0,10,rates);
   
   double ma[10];
   
   for(int i=0;i<10;i++)
      ma[i] = iMA(Symbol(),Period(),20,0,MODE_SMA,PRICE_CLOSE,i);
   
   for(int i=0;i<ArraySize(rates);i++)
   {
      if(rates[i].high >= ma[i] && rates[i].low < ma[i])
         high_touch_count++;
   }
}
 
SmartSarb: Its MT4,

is there a loop that I can write to get the MA value of the last 10 cancles and compare with the value of last 10 Moving average value? That would make my code a lot simpler and shorter else I will have to write a long long code.


  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section (bottom of the Root page?)General rules and best pratices of the Forum. - General - MQL5 programming forum
  2. What do you mean "is there a loop?" There isn't, until you write one!
       int nTouch=0;
       for(int i=0;i<10;i++){
          double ma = iMA(Symbol(),Period(),20,0,MODE_SMA,PRICE_CLOSE,i);
          if(High[i] >= ma && Low[i] < ma) ++nTouch;
       }
    Was that so hard?