MT4 to MT5 translation problem

 

Hi, 

I'm getting different behaviour in MT5 when I translate the following code:

MT4:

int recentLow = iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,5,0);
double stopLossPrice = Low[recentLow]-(iATR(Symbol(),PERIOD_CURRENT,14,0)/2);

MT5:

int recentLow = iLowest(Symbol(),PERIOD_CURRENT,MODE_LOW,5,0);
double atrGap = iATR(Symbol(),PERIOD_CURRENT,14)/2;
double stopLossPrice = Low[recentLow]-atrGap;

For some reason, I do not get the same resulting value in stopLossPrice.

Could anyone please provide some pointers where I might be going wrong here?

Thanks

 

In MQL5, the indicator functions do not return buffer data values as you do in MQL4. They return an indicator handle (an int, not a double).

You then have to use that handle with the CopyBuffer function to obtain the buffer data values. Study the example code provided for the iATR function.

int iATR(
   string           symbol,        // symbol name
   ENUM_TIMEFRAMES  period,        // period
   int              ma_period      // averaging period 
   );

Return Value

Returns the handle of a specified technical indicator,  in case of failure returns INVALID_HANDLE. The computer memory can be freed from an indicator that is no more utilized, using the IndicatorRelease() function, to which the indicator handle is passed.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Thanks Fernando,


Appreciate you pointing me in the right direction!