converting iMA from mql4 to mql5

 

I understand that the previous syntax was this

double iMA(string symbol,
           int timeframe,
           int period,
           int ma_shift,
           int ma_method,
           int applied_price,
           int shift)

and assuming i want values like this:

double maValue = iMA(NULL,PERIOD_CURRENT,14,2,MODE_SMA,PRICE_CLOSE,0);

how do i exactly key in into the sql5 syntax?

double iMAMQL4(string symbol,
               int tf,
               int period,
               int ma_shift,
               int method,
               int price,
               int shift)
  {
   ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
   ENUM_MA_METHOD ma_method=MethodMigrate(method);
   ENUM_APPLIED_PRICE applied_price=PriceMigrate(price);
   int handle=iMA(symbol,timeframe,period,ma_shift,
                  ma_method,applied_price);
   if(handle<0)
     {
      Print("The iMA object is not created: Error",GetLastError());
      return(-1);
     }
   else
      return(CopyBufferMQL4(handle,0,shift));
  }

thank you.

 

Please read the MQL5 documentation on the iMA() function (there is an example) — Documentation on MQL5: Technical Indicators / iMA

There are also examples in the CodeBase. Here is one — Moving average code for beginners by William210

Simply put, you obtain the Indicator's handle in the OnInit() event handler, and then later in the event handlers, you use CopyBuffer to obtain the values.


 
Hi, this is something i was also stuck on i appreciate your difficulty. This is a simple step by step process i use when creating indicators. This has been applied to iMA() indicator. 

Try the following these steps : 
1) Create an array
2) Set array as series 
3) Define the moving average
4) Copy price info into arrays
5) Return the moving average

double maArray[];                                                              // 1) Create an array
ArraySetAsSeries(maArray,true);                                    // 2) Set array as series
int ma = iMA(_Symbol,_Period,20,0,MODE_SMA,PRICE_CLOSE);    // 3) Define the moving average
CopyBuffer(ma,0,0,1,maArray);                                      // 4) Copy price info into arrays

double maValue = maArray[0];                                      // 5) Return the moving average

We can now use  maValue to return our moving average


Also the following articles can help mql4 to mql5 conversions

and 

Hope this helps!