Article-16308-MQL5-Moving-A.../LWMA/LWMAonPriceCloseRAW.mq5
2026-03-23 20:06:06 +07:00

67 lines
5.1 KiB
MQL5

//+------------------------------------------------------------------+
//| LWMAonPriceCloseRAW.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[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
//---
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[])
{
//--- в цикле от InpPeriod-1 до текущего бара
double result=0.0;
for(int i=InpPeriod-1; i<rates_total; i++)
{
//--- рассчитываем веса (wsum) и сумму весовых коэффициентов (sum) InpPeriod баров
double sum =0.0;
int wsum=0;
for(int j=InpPeriod; j>0; j--)
{
wsum+=j;
sum +=close[i-j+1]*(InpPeriod-j+1);
}
//--- получаем значение LWMA для текущего бара цикла
result=sum/wsum;
//--- Записываем результат расчёта в буфер
MABuffer[i]=result;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+