MQL4 Errors for OnCalculate
MQL4 Errors for OnCalculate
Loading saved threads...
JJTrader · External communityPost link
External question — Stack Overflow Stack Exchange
Author: JJTrader
Original post: https://stackoverflow.com/questions/76308336
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am hoping you can help fix my code. I am trying to code a MT4 indicator to indicate a powerful downward bar.
No matter what I do, I still get :1 errors, 1 warnings
OnCalculate function declared with wrong type or/and parameters PowerfulRedBar.mq4 line 20 column6
OnCalculate function not found in custom indicator line 1 column 1
Any suggestions?
Thanks!!!
#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;
}
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/76312491
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Your OnCalculate variables are incorrect (along with a few other errors). Try the following code
#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);
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: PaulB Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/76312491 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Your OnCalculate variables are incorrect (along with a few other errors). Try the following code #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); }
Checking account access…