How to add Moving Average Indicator on EA price chart

 

I'm trying to add a standard moving average indicator ( iMA() ) to the price chart of an EA. I've googled quite a bit but all I've found explained is how to add a custom MA to a price chart. 

Does a simple 1-line command exist which allows me to add a standard MA to a price chart or do I have to go the route of using buffers? A link to a canonical explanation of this would be highly appreciated.

Thanks in advance. 

 

Just drag the MA onto the chart, same as any chart.

Or do you mean that you want to use the values in the EA.

It is not clear what you want.

 
I'm aware of this possiblity, but I don't want to drag the MA onto the chart, I want the MA to be placed on the chart by the EA based on the MA timeframe chosen in the MA setup/config. 
 
I don't think that is possible, but maybe somebody else knows better
 
GumRai:
I don't think that is possible, but maybe somebody else knows better
Possible only with WinAPI.
 
Do you by any chance have a link handy where the use of WinAPI for such purposes are explained?
 
TheLobos:
Do you by any chance have a link handy where the use of WinAPI for such purposes are explained?

https://www.mql5.com/en/forum/73391

 
#property description "Expert Advisor demonstrating the work with ChartIndicatorAdd() function."
#property description "After launching on the chart (and receiving the error in Journal), open"
#property description "the Expert Advisor's properties and specify correct <symbol> and <period> parameters."
#property description "MACD indicator will be added on the chart."
 
//--- input parameters
input string          symbol="AUDUSD";    // symbol name
input ENUM_TIMEFRAMES period=PERIOD_M12;  // time frame
input int    fast_ema_period=12;          // fast MACD period
input int    slow_ema_period=26;          // slow MACD period
input int      signal_period=9;           // signal period
input ENUM_APPLIED_PRICE apr=PRICE_CLOSE; // price type for MACD calculation
 
int indicator_handle=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   indicator_handle=iMACD(symbol,period,fast_ema_period,slow_ema_period,signal_period,apr);
//--- try to add the indicator on the chart
   if(!AddIndicator())
     {
      //--- AddIndicator() function refused to add the indicator on the chart
      int answer=MessageBox("Do you want to add MACD on the chart anyway?",
                            "Incorrect symbol and/or time frame for adding the indicator",
                            MB_YESNO // "Yes" and "No" selection buttons will be shown
                            );
      //--- if a user still insists on incorrect usage of ChartIndicatorAdd()
      if(answer==IDYES)
        {
         //--- first of all, a Journal entry will be made about that
         PrintFormat("Attention! %s: Trying to add MACD(%s/%s) indicator on %s/%s chart. Receiving error 4114",
                     __FUNCTION__,symbol,EnumToString(period),_Symbol,EnumToString(_Period));
         //--- receive the number of a new subwindow, to which we will try to add the indicator
         int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
         //--- now make an attempt resulting in error
         if(!ChartIndicatorAdd(0,subwindow,indicator_handle))
            PrintFormat("Failed to add MACD indicator on %d chart window. Error code  %d",
                        subwindow,GetLastError());
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
// Expert Advisor performs nothing
  }
//+------------------------------------------------------------------+
//| Function for checking and adding the indicator on the chart      |
//+------------------------------------------------------------------+
bool AddIndicator()
  {
//--- displayed message
   string message;
//--- check if the indicator symbol and chart symbol match each other
   if(symbol!=_Symbol)
     {
      message="Displaying the use of Demo_ChartIndicatorAdd() function:";
      message=message+"\r\n";
      message=message+"Unable to add the indicator calculated on another symbol on the chart.";
      message=message+"\r\n";
      message=message+"Specify the chart symbol in Expert Advisor's property - "+_Symbol+".";
      Alert(message);
      //--- premature exit, the indicator will not be added on the chart
      return false;
     }
//--- check if the indicator's and chart's time frames match each other
   if(period!=_Period)
     {
      message="Unable to add the indicator calculated on another time frame on the chart.";
      message=message+"\r\n";
      message=message+"Specify the chart time frame in Expert Advisor properties - "+EnumToString(_Period)+".";
      Alert(message);
      //--- premature exit, the indicator will not be added on the chart
      return false;
     }
//--- all checks completed, symbol and indicator time frame match the chart
   if(indicator_handle==INVALID_HANDLE)
     {
      Print(__FUNCTION__,"  Creating MACD indicator");
      indicator_handle=iMACD(symbol,period,fast_ema_period,slow_ema_period,signal_period,apr);
      if(indicator_handle==INVALID_HANDLE)
        {
         Print("Failed to create MACD indicator. Error code ",GetLastError());
        }
     }
//--- reset the error code
   ResetLastError();
//--- apply the indicator to the chart 
   Print(__FUNCTION__,"  Adding MACD indicator on the chart");
   Print("MACD is generated on ",symbol,"/",EnumToString(period));
//--- receive the number of a new subwindow, to which MACD indicator is added
   int subwindow=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   PrintFormat("Adding MACD indicator on %d chart window",subwindow);
   if(!ChartIndicatorAdd(0,subwindow,indicator_handle))
     {
      PrintFormat("Failed to add MACD indicator on %d chart window. Error code  %d",
                  subwindow,GetLastError());
     }
//--- Indicator added successfully
   return(true);
  }
Reason: