Article-16308-MQL5-Moving-A.../LWMA/LWMAonPriceClose.mq5

95 lines
6.8 KiB
MQL5
Raw Permalink Normal View History

2026-03-23 20:06:06 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| LWMAonPriceClose.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 "LWMA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input int InpPeriod = 10; // LWMA Period
//--- indicator buffers
double MABuffer[];
//--- global variables
int ExtPeriod; // ?5@8>4 @0AGQB0 LWMA
//+------------------------------------------------------------------+
//| 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 LWMA
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-1
ArrayInitialize(MABuffer,EMPTY_VALUE);
start=ExtPeriod-1;
}
//--- 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 LWMA 4;O B5:CI53> 10@0 F8:;0
for(int i=start; i<rates_total; i++)
{
//--- @0AAG8BK205< 25A0 (wsum) 8 AC<<C 25A>2KE :>MDD8F85=B>2 (sum) ExtPeriod 10@>2
double sum =0.0;
int wsum=0;
for(int j=ExtPeriod; j>0; j--)
{
wsum+=j;
sum +=close[i-j+1]*(ExtPeriod-j+1);
}
//--- ?>;CG05< 7=0G5=85 LWMA 4;O B5:CI53> 10@0 F8:;0
result=sum/wsum;
//--- 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);
}
//+------------------------------------------------------------------+