//+------------------------------------------------------------------+
//| MyIndicator |
//| Copyright 2023, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Your Name"
#property link "https://www.yourwebsite.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1 // 根据您的指标需要调整缓冲区数量
#property indicator_plots 1 // 根据您的指标需要调整绘图数量
#property indicator_color1 clrRed // 设置第一条线的颜色
#property indicator_width1 1 // 设置第一条线的宽度
#property indicator_type1 DRAW_LINE // 设置第一条线的类型
//--- 输入参数
input int InpPeriod=14; // 计算周期
input ENUM_MA_METHOD InpMethod=MODE_SMA; // 计算方法
//--- 指标缓冲区
double Buffer1[]; // 主缓冲区
//--- 全局变量
int prevBars = 0; // 用于跟踪K线数量的变化
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 指标缓冲区映射
SetIndexBuffer(0, Buffer1);
SetIndexStyle(0, DRAW_LINE);
SetIndexLabel(0, "My Indicator");
//--- 初始化完成
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[])
{
//--- 检查是否有新K线形成
if(prev_calculated == 0 || rates_total != prev_calculated + 1)
{
// 完全重新计算或首次计算
ArrayInitialize(Buffer1, EMPTY_VALUE);
// 这里添加您的完整计算逻辑
for(int i=InpPeriod; i<rates_total; i++)
{
// 示例计算 - 替换为您自己的指标逻辑
Buffer1 = iCustom(NULL, 0, "Examples\\MACD", 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i);
}
}
else
{
// 仅计算最新K线
int i = rates_total - 1;
// 这里添加您的最新K线计算逻辑
// 示例计算 - 替换为您自己的指标逻辑
Buffer1 = iCustom(NULL, 0, "Examples\\MACD", 12, 26, 9, PRICE_CLOSE, MODE_MAIN, i);
}
//--- 更新K线计数
prevBars = rates_total;
//--- 返回prev_calculated的值以便下次调用
return(rates_total);
}
//+------------------------------------------------------------------+
//| 定时器函数 - 可选,用于更频繁的刷新 |
//+------------------------------------------------------------------+
void OnTimer()
{
//--- 如果需要更频繁的刷新,可以在这里添加代码
ChartRedraw();
}
//+------------------------------------------------------------------+ |