Article-16308-MQL5-Moving-A.../SMMA/SMMAonPriceClose.mq5

89 lines
6.6 KiB
MQL5
Raw Permalink Normal View History

2026-03-23 20:06:06 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| SMMAonPriceClose.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 "SMMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input int InpPeriod = 10; // SMMA Period
//--- indicator buffers
double MABuffer[];
//--- global variables
int ExtPeriod; // ?5@8>4 @0AGQB0 SMMA
//+------------------------------------------------------------------+
//| 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 SMMA
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;
double result=0;
int start =0;
//--- 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;
//--- @0AAG8B05< ?@>ABCN A:>;L7OICN A@54=NN 4;O ExtPeriod ?5@2KE 10@>2
for(int i=0; i<start; i++)
result+=close[i];
result/=ExtPeriod;
//--- 70?8AK205< ?5@2>5 >B>1@0605<>5 7=0G5=85 SMMA, @0AAG8B0==>5 :0: SMA
MABuffer[start-1]=result;
}
//--- 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
//--- @0AAG8BK205< 7=0G5=85 SMMA 4;O B5:CI53> 10@0 F8:;0
for(int i=start; i<rates_total; i++)
MABuffer[i]=(MABuffer[i-1]*(ExtPeriod-1)+close[i])/ExtPeriod;
//--- 2>72@0I05< :>;8G5AB2> ?>AG8B0==KE 10@>2 4;O A;54CNI53> 2K7>20 OnCalculate
return(rates_total);
}
//+------------------------------------------------------------------+