//+------------------------------------------------------------------+
//| WMA Lines |
//| Copyright 2023, MetaTrader 5 |
//| |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#property indicator_color3 clrDodgerBlue
#property indicator_color4 clrOrange
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1
// 定义缓冲区
double WMA255_Up[], WMA255_Down[];
double WMA55_Up[], WMA55_Down[];
// 定义 WMA 参数
int WMA255Period = 255;
int WMA55Period = 55;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
// 设置缓冲区和颜色
SetIndexBuffer(0, WMA255_Up, INDICATOR_DATA);
SetIndexBuffer(1, WMA255_Down, INDICATOR_DATA);
SetIndexBuffer(2, WMA55_Up, INDICATOR_DATA);
SetIndexBuffer(3, WMA55_Down, INDICATOR_DATA);
// 设置绘图样式
PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrBlue);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrOrange);
PlotIndexSetInteger(2, PLOT_LINE_COLOR, clrDodgerBlue);
PlotIndexSetInteger(3, PLOT_LINE_COLOR, clrRed);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标计算函数 |
//+------------------------------------------------------------------+
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[])
{
// 确保有足够的柱数进行计算
if (rates_total < WMA255Period || rates_total < WMA55Period)
return 0;
// 循环计算每个柱的 WMA 值
for (int i = 1; i < rates_total; i++)
{
// 计算 WMA(255)
double WMA255_Current = iMA(NULL, 0, WMA255Period, 0, MODE_LWMA, PRICE_CLOSE, i);
double WMA255_Previous = iMA(NULL, 0, WMA255Period, 0, MODE_LWMA, PRICE_CLOSE, i + 1);
// 根据条件填充缓冲区,绘制不同颜色的线
if (WMA255_Current > WMA255_Previous)
{
WMA255_Up[i] = WMA255_Current; // 蓝色线条(上涨)
WMA255_Down[i] = EMPTY_VALUE; // 不绘制下降部分
}
else
{
WMA255_Down[i] = WMA255_Current; // 橙色线条(下降)
WMA255_Up[i] = EMPTY_VALUE; // 不绘制上涨部分
}
// 计算 WMA(55)
double WMA55_Current = iMA(NULL, 0, WMA55Period, 0, MODE_LWMA, PRICE_CLOSE, i);
double WMA55_Previous = iMA(NULL, 0, WMA55Period, 0, MODE_LWMA, PRICE_CLOSE, i + 1);
// 根据条件填充缓冲区,绘制不同颜色的线
if (WMA55_Current > WMA55_Previous)
{
WMA55_Up[i] = WMA55_Current; // 蓝色线条(上涨)
WMA55_Down[i] = EMPTY_VALUE; // 不绘制下降部分
}
else
{
WMA55_Down[i] = WMA55_Current; // 红色线条(下降)
WMA55_Up[i] = EMPTY_VALUE; // 不绘制上涨部分
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
|