Article-16308-MQL5-Moving-A.../SMA/SMAonPriceCloseECO.mq5

91 lines
6.4 KiB
MQL5
Raw Permalink Normal View History

2026-03-23 20:06:06 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| SMAonPriceCloseECO.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 "SMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input int InpPeriod = 10; // SMA Period
//--- indicator buffers
double MABuffer[];
//--- global variables
int ExtPeriod; // ?5@8>4 @0AGQB0 SMA
//+------------------------------------------------------------------+
//| 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[])
{
//--- >@@5:B8@C5< 2254Q==>5 7=0G5=85 ?5@8>40 @0AGQB0 SMA
ExtPeriod=(InpPeriod<1 ? 10 : InpPeriod);
//--- A;8 =5 E20B05B 8AB>@8G5A:8E 40==KE 4;O @0AGQB0 - 2>72@0I05< 0
if(rates_total<ExtPeriod)
return 0;
int start=0; // 10@ =0G0;0 @0AGQB0
//--- A;8 MB> ?5@2K9 70?CA: 8;8 87<5=5=85 2 8AB>@8G5A:8E 40==KE
if(prev_calculated==0)
{
//--- 8=8F80;878@C5< 1CD5@ 8=48:0B>@0 ?CABK< 7=0G5=85< 8
//--- CAB0=02;8205< =0G0;> @0AGQB0 =0 7=0G5=85 ExtPeriod
ArrayInitialize(MABuffer,EMPTY_VALUE);
start=ExtPeriod;
}
//--- A;8 MB> =5 ?5@2K9 70?CA: - =0G0;>< @0AGQB0 1C45B B5:CI89 10@
else
start=prev_calculated-1;
//--- 2 F8:;5 >B 7=0G5=8O start 4> B5:CI53> 10@0 8AB>@88
for(int i=start; i<rates_total; i++)
{
//--- @0AAG8BK205< A@54=55 F5= close 70 ExtPeriod 10@>2
double result=0;
for(int j=0; j<ExtPeriod; j++)
result+=close[i-j];
result/=ExtPeriod;
//--- 0?8AK205< @57C;LB0B @0AGQB0 2 1CD5@
MABuffer[i]=result;
}
//--- 2>72@0I05< :>;8G5AB2> ?>AG8B0==KE 10@>2 4;O A;54CNI53> 2K7>20 OnCalculate
return(rates_total);
}
//+------------------------------------------------------------------+