//+------------------------------------------------------------------+
//| EWMA.mq5 |
//| Copyright 2020, Vladimir Mikhailov |
//| mikh.vl@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Vladimir Mikhailov"
#property link "mikh.vl@gmail.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_applied_price PRICE_CLOSE
//--- plot Moving Average
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- input parameters
input double Alfa=0.1; //Alfa {0;1}
//--- indicator buffers
double iBuffer[], a;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,iBuffer,INDICATOR_DATA);
if(Alfa < 0 || Alfa > 1)
{
Print("Wrong Alfa! Set Alfa to default - 0.1.");
a = 0.1;
}
else
{
a = Alfa;
}
IndicatorSetString(INDICATOR_SHORTNAME, "EWMA(" + DoubleToString(a,3) + ")");
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
//--- calc start moving price
if(prev_calculated == 0)
{
iBuffer[0] = price[0];
for(int i = 1; i < rates_total - 1; i++)
iBuffer[i] = a * price[i] + (1 - a) * iBuffer[i - 1];
}
//--- calc current moving price
iBuffer[rates_total - 1] = a * price[rates_total - 1] + (1 - a) * iBuffer[rates_total - 2];
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
Comments