How to create a moving average function and plot it?

 

Hi,

My skills in MQL5 are quite weak compared to MQL4 and I've never been able to truly carry over referencing indicators in MQL5.

In the example below, the high/low channels plot just fine; however, the moving average does not plot, at least not correctly.

I'm attempting to re-write the iMA( function similar to that of MQL4.  Of course, in MQL5 the iMA( function is a handle so an equivalent MQL4 iMA( function needs to be rewritten.

I'm asking not just for iMA( or moving averages, but other functions similar to this (e.g. iRSI(, iStochastic(, etc.).

How would these indicator functions be rewritten in MQL5?  Can an equivalent be done?  I've many indicators in MQL4 that are multi-symbol and would be nice to create equivalent indicator functions that can be used throughout.  If the functions themselves cannot be rewritten, how would they be handled when referencing different symbols or timeframes?

Thank you.

//+------------------------------------------------------------------+
//|                                             High_Low_Channel.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3

//--- plot Upper
#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Lower
#property indicator_label2  "Lower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot MA
#property indicator_label3  "MA"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2
//--- input parameters
input int      HighLowBars=8;
//--- indicator buffers
double         UpperBuffer[];
double         LowerBuffer[];
double         MABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,MABuffer,INDICATOR_DATA);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tickvolume[],
                const long &volume[],
                const int &spread[])
  {
//---
   ArraySetAsSeries(UpperBuffer,true);
   ArraySetAsSeries(LowerBuffer,true);
   ArraySetAsSeries(MABuffer,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);

   int bars = rates_total-1;
   if(prev_calculated>0) { bars = rates_total-(prev_calculated); }
   for(int i=bars;i>=0;i--)
   {
     UpperBuffer[i] = high[ArrayMaximum(high,i,HighLowBars)];
     LowerBuffer[i] = low[ArrayMinimum(low,i,HighLowBars)];
     MABuffer[i] = iMa(_Symbol,0,50,0,MODE_EMA,PRICE_CLOSE,i);
     if(i<10) { Print("i = "+i); }
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

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

double CopyBufferMQL4(int handle,int index,int shift)
  {
   double buf[];
   switch(index)
     {
      case 0: if(CopyBuffer(handle,0,shift,1,buf)>0)
         return(buf[0]); break;
      case 1: if(CopyBuffer(handle,1,shift,1,buf)>0)
         return(buf[0]); break;
      case 2: if(CopyBuffer(handle,2,shift,1,buf)>0)
         return(buf[0]); break;
      case 3: if(CopyBuffer(handle,3,shift,1,buf)>0)
         return(buf[0]); break;
      case 4: if(CopyBuffer(handle,4,shift,1,buf)>0)
         return(buf[0]); break;
      default: break;
     }
   return(EMPTY_VALUE);
  }
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.03.10
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 
Keith Watford:
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.

Done.

 
Matthew Renner :

Hi,

My skills in MQL5 are quite weak compared to MQL4 and I've never been able to truly carry over referencing indicators in MQL5.

In the example below, the high/low channels plot just fine; however, the moving average does not plot, at least not correctly.

I'm attempting to re-write the iMA( function similar to that of MQL4.  Of course, in MQL5 the iMA( function is a handle so an equivalent MQL4 iMA( function needs to be rewritten.

I'm asking not just for iMA( or moving averages, but other functions similar to this (e.g. iRSI(, iStochastic(, etc.).

How would these indicator functions be rewritten in MQL5?  Can an equivalent be done?  I've many indicators in MQL4 that are multi-symbol and would be nice to create equivalent indicator functions that can be used throughout.  If the functions themselves cannot be rewritten, how would they be handled when referencing different symbols or timeframes?

Thank you.

Read the help ( iMA ) first.

Remember: in MQL5, the indicator handle WILL BE CREATED ONCE !!! And it does it in OnInit ().

You must correct your mistake - until you correct your mistake, you cannot move on.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
iMA - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: