The errors you're encountering are due to some MQL5-specific functions and syntax that might not be fully compatible with your version of MetaTrader 5 or due to some missing declarations. Let's address these issues step by step.
Error Analysis and Fixes:
-
IndicatorShortName is undeclared:
- This function is used to set the short name of the indicator that appears on the chart. However, in some versions of MQL5, this function might not be recognized. We can replace it with a comment or use an alternative method to set the indicator name.
-
SetIndexEmptyValue is undeclared:
- This function is used to set the empty value for indicator buffers. If it's not recognized, we can manually set the empty values in the buffers.
-
Unexpected tokens in the SetIndexEmptyValue line:
- This is related to the previous issue. We'll handle it by manually initializing the buffers.
Revised MQL5 Code:
Here is the corrected version of the code with the necessary fixes:
//+------------------------------------------------------------------+
//| InitialOpeningBalanceRange.mq5 |
//| Copyright 2023, AI Assistant |
//| https://example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, AI Assistant"
#property link "https://example.com"
#property version "1.02"
#property strict
#property indicator_chart_window
#property indicator_buffers 12
#property indicator_plots 12
//--- Indicator Plots (Range)
#property indicator_label1 "ORB High"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_label2 "ORB Low"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrangeRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--- Indicator Plots (Fib Levels)
#property indicator_label3 "Fib 0%"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrBlue
#property indicator_style3 STYLE_DASH
#property indicator_width3 1
#property indicator_label4 "Fib 23.6%"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrRoyalBlue
#property indicator_style4 STYLE_DASH
#property indicator_width4 1
#property indicator_label5 "Fib 38.2%"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrSkyBlue
#property indicator_style5 STYLE_DASH
#property indicator_width5 1
#property indicator_label6 "Fib 45%"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrLimeGreen
#property indicator_style6 STYLE_DASH
#property indicator_width6 1
#property indicator_label7 "Fib 50%"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrLightBlue
#property indicator_style7 STYLE_DASH
#property indicator_width7 1
#property indicator_label8 "Fib 61.8%"
#property indicator_type8 DRAW_LINE
#property indicator_color8 clrDodgerBlue
#property indicator_style8 STYLE_DASH
#property indicator_width8 1
#property indicator_label9 "Fib 80%"
#property indicator_type9 DRAW_LINE
#property indicator_color9 clrMediumPurple
#property indicator_style9 STYLE_DASH
#property indicator_width9 1
#property indicator_label10 "Fib 100%"
#property indicator_type10 DRAW_LINE
#property indicator_color10 clrDarkBlue
#property indicator_style10 STYLE_DASH
#property indicator_width10 1
#property indicator_label11 "Fib 127.2%"
#property indicator_type11 DRAW_LINE
#property indicator_color11 clrNavy
#property indicator_style11 STYLE_DASH
#property indicator_width11 1
#property indicator_label12 "Fib 161.8%"
#property indicator_type12 DRAW_LINE
#property indicator_color12 clrMidnightBlue
#property indicator_style12 STYLE_DASH
#property indicator_width12 1
//--- Inputs
input int NY_Time_Offset_Hours = -4; // NY Time Offset from GMT (e.g., -4 for DST, -5 for Standard)
input bool Show_Extensions = true; // Show Fibonacci Extensions (127.2%, 161.8%)
input color Bullish_Fib_Color = clrBlue; // Base Color for Bullish Fib Lines
input color Bearish_Fib_Color = clrRed; // Base Color for Bearish Fib Lines
input color Fib45_Color = clrLimeGreen; // Color for 45% Level
input color Fib80_Color = clrMediumPurple; // Color for 80% Level
input int Fib_Line_Style = STYLE_DASH; // Fib Line Style (0=Solid, 1=Dash, etc.)
input int Fib_Line_Width = 1; // Fib Line Width
//--- Buffers
double HighBuffer[];
double LowBuffer[];
double Fib0Buffer[];
double Fib236Buffer[];
double Fib382Buffer[];
double Fib45Buffer[];
double Fib50Buffer[];
double Fib618Buffer[];
double Fib80Buffer[];
double Fib100Buffer[];
double Fib1272Buffer[];
double Fib1618Buffer[];
//--- Global Variables
datetime lastDay = 0;
double rangeHigh = 0;
double rangeLow = 0;
datetime rangeEndTime = 0;
int breakoutDirection = 0; // 0=none, 1=bullish, -1=bearish
datetime breakoutTime = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Range Buffers
SetIndexBuffer(0, HighBuffer);
SetIndexBuffer(1, LowBuffer);
// Fib Buffers
SetIndexBuffer(2, Fib0Buffer);
SetIndexBuffer(3, Fib236Buffer);
SetIndexBuffer(4, Fib382Buffer);
SetIndexBuffer(5, Fib45Buffer);
SetIndexBuffer(6, Fib50Buffer);
SetIndexBuffer(7, Fib618Buffer);
SetIndexBuffer(8, Fib80Buffer);
SetIndexBuffer(9, Fib100Buffer);
SetIndexBuffer(10, Fib1272Buffer);
SetIndexBuffer(11, Fib1618Buffer);
// Set empty values manually
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 1000; j++) { // Assuming a buffer size of 1000
switch (i) {
case 0: HighBuffer[j] = EMPTY_VALUE; break;
case 1: LowBuffer[j] = EMPTY_VALUE; break;
case 2: Fib0Buffer[j] = EMPTY_VALUE; break;
case 3: Fib236Buffer[j] = EMPTY_VALUE; break;
case 4: Fib382Buffer[j] = EMPTY_VALUE; break;
case 5: Fib45Buffer[j] = EMPTY_VALUE; break;
case 6: Fib50Buffer[j] = EMPTY_VALUE; break;
case 7: Fib618Buffer[j] = EMPTY_VALUE; break;
case 8: Fib80Buffer[j] = EMPTY_VALUE; break;
case 9: Fib100Buffer[j] = EMPTY_VALUE; break;
case 10: Fib1272Buffer[j] = EMPTY_VALUE; break;
case 11: Fib1618Buffer[j] = EMPTY_VALUE; break;
}
}
}
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[])
{
if (rates_total < 2) return(0);
for (int i = MathMax(prev_calculated - 1, 0); i < rates_total; i++)
{
datetime gmtTime = time[i];
datetime nyTime = gmtTime + NY_Time_Offset_Hours * 3600;
MqlDateTime nyStruct;
TimeToStruct(nyTime, nyStruct);
datetime currentDay = StructToTime(nyStruct);
currentDay -= nyStruct.hour * 3600 + nyStruct.min * 60 + nyStruct.sec;
if (currentDay != lastDay)
{
CalculateDailyRange(time, high, low, close, rates_total, currentDay);
lastDay = currentDay;
breakoutDirection = 0;
breakoutTime = 0;
rangeHigh = 0;
rangeLow = DBL_MAX;
}
// Extend range lines
if (rangeHigh > 0) HighBuffer[i] = rangeHigh;
else HighBuffer[i] = EMPTY_VALUE;
if (rangeLow < DBL_MAX) LowBuffer[i] = rangeLow;
else LowBuffer[i] = EMPTY_VALUE;
// Check for breakout after range is set
if (breakoutDirection == 0 && nyTime > rangeEndTime)
{
if (close[i] > rangeHigh)
{
breakoutDirection = 1; // Bullish
breakoutTime = time[i];
PlotFibLevels(i, 1);
}
else if (close[i] < rangeLow)
{
breakoutDirection = -1; // Bearish
breakoutTime = time[i];
PlotFibLevels(i, -1);
}
}
// Extend Fib lines if breakout has occurred
if (breakoutDirection != 0 && time[i] >= breakoutTime)
{
Fib0Buffer[i] = Fib0Buffer[i-1];
Fib236Buffer[i] = Fib236Buffer[i-1];
Fib382Buffer[i] = Fib382Buffer[i-1];
Fib45Buffer[i] = Fib45Buffer[i-1];
Fib50Buffer[i] = Fib50Buffer[i-1];
Fib618Buffer[i] = Fib618Buffer[i-1];
Fib80Buffer[i] = Fib80Buffer[i-1];
Fib100Buffer[i] = Fib100Buffer[i-1];
Fib1272Buffer[i] = Fib1272Buffer[i-1];
Fib1618Buffer[i] = Fib1618Buffer[i-1];
}
else
{
Fib0Buffer[i] = EMPTY_VALUE;
Fib236Buffer[i] = EMPTY_VALUE;
Fib382Buffer[i] = EMPTY_VALUE;
Fib45Buffer[i] = EMPTY_VALUE;
Fib50Buffer[i] = EMPTY_VALUE;
Fib618Buffer[i] = EMPTY_VALUE;
Fib80Buffer[i] = EMPTY_VALUE;
Fib100Buffer[i] = EMPTY_VALUE;
Fib1272Buffer[i] = EMPTY_VALUE;
Fib1618Buffer[i] = EMPTY_VALUE;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Function to calculate the daily range |
//+------------------------------------------------------------------+
void CalculateDailyRange(const datetime &time[], const double &high[], const double &low[], const double &close[], int rates_total, datetime nyDayStart)
{
rangeHigh = 0;
rangeLow = DBL_MAX;
bool inRange = false;
int startSeconds = 8*3600 + 20*60; // 08:20
int endSeconds = 9*3600 + 20*60; // 09:20
for (int j = 0; j < rates_total; j++)
{
datetime gmtTime = time[j];
datetime nyTime = gmtTime + NY_Time_Offset_Hours * 3600;
MqlDateTime nyStruct;
TimeToStruct(nyTime, nyStruct);
datetime barDayStart = StructToTime(nyStruct) - (nyStruct.hour*3600 + nyStruct.min*60 + nyStruct.sec);
if (barDayStart != nyDayStart) continue;
int barSeconds = nyStruct.hour*3600 + nyStruct.min*60 + nyStruct.sec;
if (barSeconds >= startSeconds && barSeconds < endSeconds)
{
inRange = true;
if (high[j] > rangeHigh) rangeHigh = high[j];
if (low[j] < rangeLow) rangeLow = low[j];
}
else if (inRange)
{
rangeEndTime = nyTime;
break;
}
}
if (rangeHigh > 0 && rangeLow < DBL_MAX)
{
for (int k = rates_total - 1; k >= 0; k--)
{
datetime nyTimeK = time[k] + NY_Time_Offset_Hours * 3600;
if (nyTimeK >= rangeEndTime)
{
HighBuffer[k] = rangeHigh;
LowBuffer[k] = rangeLow;
break;
}
}
}
}
//+------------------------------------------------------------------+
//| Function to plot Fibonacci levels based on breakout direction |
//+------------------------------------------------------------------+
void PlotFibLevels(int barIndex, int direction)
{
double fib0, fib100;
double rangeSize = MathAbs(rangeHigh - rangeLow);
if (direction == 1) // Bullish: 0% = high, 100% = low
{
fib0 = rangeHigh;
fib100 = rangeLow;
}
else if (direction == -1) // Bearish: 0% = low, 100% = high
{
fib0 = rangeLow;
fib100 = rangeHigh;
}
else return;
double diff = fib100 - fib0;
Fib0Buffer[barIndex] = fib0 + 0.0 * diff;
Fib236Buffer[barIndex] = fib0 + 0.236 * diff;
Fib382Buffer[barIndex] = fib0 + 0.382 * diff;
Fib45Buffer[barIndex] = fib0 + 0.45 * diff;
Fib50Buffer[barIndex] = fib0 + 0.5 * diff;
Fib618Buffer[barIndex] = fib0 + 0.618 * diff;
Fib80Buffer[barIndex] = fib0 + 0.80 * diff;
Fib100Buffer[barIndex] = fib0 + 1.0 * diff;
Fib1272Buffer[barIndex] = Show_Extensions ? fib0 + 1.272 * diff : EMPTY_VALUE;
Fib1618Buffer[barIndex] = Show_Extensions ? fib0 + 1.618 * diff : EMPTY_VALUE;
}
Key Changes Made:
-
Removed IndicatorShortName:
- This function is not standard in MQL5. Instead, the indicator name is set via
#property directives at the top.
-
Replaced SetIndexEmptyValue:
- Manually initialized the buffers with
EMPTY_VALUE in the OnInit function.
-
Fixed Buffer Initialization:
- Added a loop to manually set empty values for all buffers.
Steps to Compile and Use:
-
Copy the Code:
- Copy the entire code block above.
-
Create a New Indicator in MetaEditor:
- Open MetaEditor in MetaTrader 5.
- Click
File -> New -> Custom Indicator.
- Give it a name (e.g.,
InitialOpeningBalanceRange).
- Delete any boilerplate code that is automatically generated.
- Paste the copied code into the editor.
-
Compile the Code:
- Click the
Compile button (or press F7).
- If there are no errors, the indicator is ready to use.
-
Attach the Indicator to a Chart:
- Open a chart in MetaTrader 5.
- Drag and drop the newly compiled indicator from the Navigator panel onto the chart.
This should resolve the compilation errors and allow you to use the indicator as intended. If you encounter any further issues, please let me know!