Using Moving Avereage with custom ENUM_APPLIED_PRICE variable for Heiken Ashi based MA calulation

 

I'm currently learning how to apply moving average with indicators values. For example, in below code I am trying to have moving average data for Heiken Ashi indicator value.

//+------------------------------------------------------------------+
//|Variable Initializer                                           |
//+------------------------------------------------------------------+
int hahandler;
ENUM_APPLIED_PRICE haclose;
int hama;
int ma;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   double hac[];
   hahandler = iCustom(_Symbol,PERIOD_CURRENT,"Examples\\Heiken_Ashi.ex5");
   CopyBuffer(hahandler,3,0,1,hac);
   haclose = hac[0];
   hama= iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_EMA,haclose);
   ma= iMA(_Symbol,PERIOD_CURRENT,20,0,MODE_EMA,PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double hac[],indicator_ma[],normal_ma[];
   CopyBuffer(hahandler,3,0,1,hac);
   haclose = hac[0];
   CopyBuffer(ma,0,0,1,normal_ma);
   CopyBuffer(hama,0,0,1,indicator_ma);
   Print("HA_MA:"+indicator_ma[0]+", MA:"+normal_ma[0]+", HAClose:"+hac[0]);
  }
//+------------------------------------------------------------------+

But when Checking the value of boat moving average it appears to be same. What should I do to pass a indicator value to the moving average function iMA().

Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Ashish Sah: I'm currently learning how to apply moving average with indicators values. For example, in below code I am trying to have moving average data for Heiken Ashi indicator value. But when Checking the value of boat moving average it appears to be same. What should I do to pass a indicator value to the moving average function iMA().

Usually the Heiken Ashi is a multi-buffer indicator displayed as candles plot, and as such, cannot be used as the "applied price" for another indicator.

Forum on trading, automated trading systems and testing trading strategies

How to code Moving Average applied on Bollinger Bands

Fernando Carreiro, 2023.10.11 22:34

To summarise the issue ...

Currently MQL5 does not offer a method to redirect secondary plots to the "applied price" parameter of an indicator function. It only works with the primary plot.

Possible work-arounds (can't be classified as solutions) ...

  • To collect the data from the secondary plots via the CopyBuffer function and then apply a moving average using the functions in in "MovingAverages.mqh" (for the moving average example).
  • To break-down the calculations of the original multi-plot indicator, and then use the "applied price" method on the elementary indicator functions.

Also, neither of the above two work-around are ideal, or efficient or even generic.

There may be other work-arounds that have not yet been proposed or discussed.