Article-16308-MQL5-Moving-A.../EMA/EMAonPriceCloseRAW.mq5

66 lines
5.1 KiB
MQL5
Raw Permalink Normal View History

2026-03-23 20:06:06 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| EMAonPriceCloseRAW.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//--- plot MA
#property indicator_label1 "EMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input int InpPeriod = 10; // EMA Period
//--- indicator buffers
double MABuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
//--- CA?5H=0O 8=8F80;870F8O
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[])
{
//--- 2 F8:;5 >B =C;O (=0G0;> 8AB>@88) 4> B5:CI53> 10@0
for(int i=0; i<rates_total; i++)
{
double result=0;
double smf=2.0/(InpPeriod+1.0); // D0:B>@ A3;06820=8O
double prev_value=(i>=1 ? MABuffer[i-1] : close[i]); // ?@54K4CI55 @0AAG8B0==>5 7=0G5=85 EMA
//--- @0AAG8BK205< EMA =0 >A=>20=88 F5=K Close 10@0, ?@>H;>3> 7=0G5=8O EMA 8 D0:B>@0 A3;06820=8O
result=close[i]*smf+prev_value*(1-smf);
//--- 0?8AK205< @57C;LB0B @0AGQB0 2 1CD5@
MABuffer[i]=result;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+