MA of ATR in an EA

 
How do I create a moving average of the ATR inside an EA?  I know the Moving Average indicator in MT4 allow you to choose "Previous indicator" for the applied price, but there is no selection for that in the list of enumerated "applied prices"
 
Trader Mike:
How do I create a moving average of the ATR inside an EA?  I know the Moving Average indicator in MT4 allow you to choose "Previous indicator" for the applied price, but there is no selection for that in the list of enumerated "applied prices"

You create a handle for the atr indicator ,and then ,create a handle for a moving average providing said atr handle as the applied price 

#property copyright "Mql5.com forum thread >>"
#property link      "https://www.mql5.com/en/forum/350345"
#property description "Telegram  : https://t.me/lorentzor\nInstagram : @rlorentzo\nTwitter : @lorentzo_r\nLinkedIn : https://www.linkedin.com/in/lorentzor\nYoutube : https://www.youtube.com/channel/UCM0Lj06cAJagFWvSpb9N5zA\nFacebook  : @LorentzoR"
/* 
ways to connect .> : 
Telegram  : https://t.me/lorentzor
Instagram : https://www.instagram.com/rlorentzo /OR/ @rlorentzo
Twitter   : https://twitter.com/lorentzo_r /OR/ @lorentzo_r
LinkedIn  : https://www.linkedin.com/in/lorentzor
Youtube   : https://www.youtube.com/channel/UCM0Lj06cAJagFWvSpb9N5zA
Facebook  : https://www.facebook.com/LorentzoR /OR/ @LorentzoR
Mql5.com  : https://www.mql5.com/en/users/lorio
*/
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Atr
#property indicator_label1  "Atr"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrWhite
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot MaOfAtr
#property indicator_label2  "MaOfAtr"
#property indicator_type2   DRAW_LINE
#property indicator_color2  C'143,188,139'
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
input int AtrPeriod=14;//Atr Period 
input int MaPeriod=21;//Ma Period
input ENUM_MA_METHOD MaMethod=MODE_SMA;//Ma Method

//--- indicator buffers
double         Atr[];
double         MaOfAtr[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
//atr handle 
int atr_handle=INVALID_HANDLE,ma_handle=INVALID_HANDLE,deflector=0;
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Atr,INDICATOR_DATA);
   SetIndexBuffer(1,MaOfAtr,INDICATOR_DATA);
   atr_handle=iATR(_Symbol,_Period,AtrPeriod);
   if(atr_handle==INVALID_HANDLE){Print("Cant Create Atr Handle ");return(INIT_FAILED);}
   //we create an ma and provide the handle of the atr indicator we just created as a price 
   ma_handle=iMA(_Symbol,_Period,MaPeriod,0,MaMethod,atr_handle);
   if(ma_handle==INVALID_HANDLE){Print("Cant Create Ma on Atr Handle ");return(INIT_FAILED);}
   deflector=AtrPeriod;
   if(MaPeriod>deflector){deflector=MaPeriod;}
//---
   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 &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  //a hasty copier 
  double copiando[];
  int i_from=rates_total-deflector,i_to=0;
  if(prev_calculated==rates_total){i_from=0;}
  if((rates_total-prev_calculated)==1){i_from=1;}
  for(int i=i_from;i>=i_to;i--)
  {
  int j=rates_total-i-1;//point to the storage adress
  int copi=CopyBuffer(atr_handle,0,i,1,copiando);
  if(copi==1){Atr[j]=copiando[0];}
  copi=CopyBuffer(ma_handle,0,i,1,copiando);
  if(copi==1){MaOfAtr[j]=copiando[0];}
  
  }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

there is also a library available for custom buffers : 

https://www.mql5.com/en/code/77

MovingAverages
MovingAverages
  • www.mql5.com
The MovingAverages library is a part of Standard package of MetaTrader 5 client terminal. The library contains functions for calculation of different types of moving averages. Totally, there are 8 functions that can be divided into 2 groups of functions of the same type, each containing 4 of them. The first group contains functions that receive...
Files:
ma_ATR.mq5  5 kb
Reason: