OnCalculate 的 MQL4 错误

问题描述 投票:0回答:2

我希望你能帮助修复我的代码。我正在尝试编写一个 MT4 指标来指示强大的下行柱。

无论我做什么,我仍然会收到:1 个错误,1 个警告

使用错误类型或/和参数声明的 OnCalculate 函数 PowerfulRedBar.mq4 第 20 行第 6 列

在自定义指标第 1 行第 1 列中未找到 OnCalculate 函数

有什么建议吗?

谢谢!!!

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2
extern int minBarsLeft = 5; // Number of bars to the left for comparison
extern int minRangeMultiplier = 2; // Minimum range multiplier for a powerful bar
double buffer[];
void OnInit()
{
IndicatorBuffers(1);
SetIndexBuffer(0, buffer);
SetIndexStyle(0, DRAW_ARROW);
SetIndexArrow(0, 233);
SetIndexLabel(0, "Powerful Red Bar");
}
void OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double& open[],
const double& high[],
const double& low[],
const double& close[],
const long& tick_volume[],
const long& volume[],
const int& spread[])
{
int startBar = MathMax(prev_calculated - minBarsLeft, 0);
for (int i = startBar; i < rates_total; i++)
{
bool isPowerfulRedBar = IsPowerfulRedBar(i, minBarsLeft, minRangeMultiplier, high, low, close, open);
buffer[i] = isPowerfulRedBar ? low[i] - (0.5 * Point) : EMPTY_VALUE;
}
}

bool IsPowerfulRedBar(const int barIndex,
const int numBarsLeft,
const int rangeMultiplier,
const double &high[],
const double &low[],
const double &close[],
const double &open[])
{
double currentRange = high[barIndex] - low[barIndex];
// Check if the bar is red
if (close[barIndex] >= open[barIndex])
return false;
// Check if the bar has the minimum range
if (currentRange < rangeMultiplier * AverageRange(barIndex, numBarsLeft, high, low))
return false;
// Check if the bar has a greater range than the bars to the left
for (int i = 1; i <= numBarsLeft; i++)
{
if ((high[barIndex - i] - low[barIndex - i]) >= currentRange)
return false;
}
return true;
}
double AverageRange(const int startIndex,
const int numBars,
const double &high[],
const double &low[])
{
double sum = 0;
for (int i = startIndex - numBars; i < startIndex; i++)
{
sum += high[i] - low[i];
}
return sum / numBars;
}

mql4
2个回答
0
投票

您的 OnCalculate 变量不正确(以及一些其他错误)。尝试以下代码

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

extern int minBarsLeft = 5; // Number of bars to the left for comparison
extern int minRangeMultiplier = 2; // Minimum range multiplier for a powerful bar
double buffer[];

//+-------------------------------------------------------------+
//| Custom indicator initialization function                    |
//+-------------------------------------------------------------+
int OnInit()
{
   IndicatorBuffers(1);
   SetIndexBuffer(0, buffer);
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, 233);
   SetIndexLabel(0, "Powerful Red Bar");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator OnCalculate 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[])
{
   int startBar = MathMax(prev_calculated - minBarsLeft, 0);
   for (int i = startBar; i < rates_total; i++)
   {
      bool isPowerfulRedBar = IsPowerfulRedBar(i, minBarsLeft, minRangeMultiplier, high, low, close, open);
      buffer[i] = isPowerfulRedBar ? low[i] - (0.5 * Point) : EMPTY_VALUE;
   }
return(rates_total);
}
//+------------------------------------------------------------------+
//| Custom Functions                                                 |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
bool IsPowerfulRedBar(const int barIndex, const int numBarsLeft, const int rangeMultiplier, const double &high[], const double &low[], const double &close[], const double &open[])
//+------------------------------------------------------------------+
{
   double currentRange = high[barIndex] - low[barIndex];
   if (close[barIndex] >= open[barIndex]) return false;
   if (currentRange < rangeMultiplier * AverageRange(barIndex, numBarsLeft, high, low)) return false;
   for (int i = 1; i <= numBarsLeft; i++) if ((high[barIndex - i] - low[barIndex - i]) >= currentRange) return false;
return(true);
}
//+------------------------------------------------------------------+
double AverageRange(const int startIndex, const int numBars, const double &high[], const double &low[])
//+------------------------------------------------------------------+
{
   double sum = 0;
   for (int i = startIndex - numBars; i < startIndex; i++) sum += high[i] - low[i];
return(sum/numBars);
}

0
投票

我希望你能帮助修复我的代码。我正在尝试编写一个 MT4 指标来指示强大的下行柱。

无论我做什么,我仍然会收到:1 个错误,1 个警告

使用错误类型或/和参数声明的 OnCalculate 函数 PowerfulRedBar.mq4 第 20 行第 6 列

在自定义指标第 1 行第 1 列中未找到 OnCalculate 函数

有什么建议吗?

谢谢!!!

© www.soinside.com 2019 - 2024. All rights reserved.