Below is a complete MQL5 code for a custom MT5 indicator that calculates and plots the "Initial Opening Balance Range" (which I interpret as the high-low price range) based on the period from 08:20 to 09:20 NY time (Eastern Time, ET) each trading day. This is often used in strategies like Opening Range Breakout (ORB) or Market Profile's Initial Balance.
Important Notes:
- Timezone Handling: MT5 uses the broker's server time (often GMT/UTC-based). NY time (ET) is typically GMT-4 (during Daylight Saving Time, March to November) or GMT-5 (Standard Time, November to March). The indicator includes an input parameter
NY_Time_Offset_Hours to adjust for this offset (e.g., set to -4 or -5). You may need to adjust it based on your broker's time and current DST rules. For accuracy, you can use external APIs or manually verify.
- How It Works:
- The indicator scans historical bars to find the exact period from 08:20 to 09:20 NY time each day.
- It calculates the highest high and lowest low in that 1-hour window.
- It plots two horizontal lines (high and low) for the range, starting from the end of the range period and extending to the right (until the next day's range is calculated).
- The lines are reset daily.
- It works on any timeframe (e.g., M1, M5, H1), but I recommend M5 or higher for accuracy.
- Limitations:
- This assumes a 24/5 market (e.g., forex). For stocks or indices, ensure your symbol has data during that time.
- It doesn't handle weekends or holidays automatically—ranges are calculated only on days with data.
- If your broker's time doesn't align perfectly, the range might be off; test on a demo account.
- Installation:
- Copy the code into a new file in MetaEditor (e.g., name it
InitialOpeningBalanceRange.mq5).
- Compile it.
- Attach to a chart via Navigator > Indicators > Custom.
- Adjust inputs as needed.
MQL5 Code: InitialOpeningBalanceRange.mq5
//+------------------------------------------------------------------+
//| InitialOpeningBalanceRange.mq5 |
//| Copyright 2023, Your Name or AI Assistant |
//| https://example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, AI Assistant"
#property link "https://example.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- Indicator Plots
#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
//--- Inputs
input int NY_Time_Offset_Hours = -4; // NY Time Offset from GMT (e.g., -4 for DST, -5 for Standard)
input color HighLineColor = clrDodgerBlue; // Color for High Line
input color LowLineColor = clrOrangeRed; // Color for Low Line
input int LineStyle = STYLE_SOLID; // Line Style (0=Solid, 1=Dash, etc.)
input int LineWidth = 2; // Line Width
//--- Buffers
double HighBuffer[];
double LowBuffer[];
//--- Global Variables
datetime lastDay = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, HighBuffer);
SetIndexBuffer(1, LowBuffer);
IndicatorShortName("Initial Opening Balance Range (08:20-09:20 NY)");
// Set empty values
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
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[])
{
// Ensure enough bars
if (rates_total < 2) return(0);
// Loop from the oldest to newest bar
for (int i = MathMax(prev_calculated - 1, 0); i < rates_total; i++)
{
// Get current bar time in GMT
datetime gmtTime = time[i];
// Convert to NY time (adjust for offset)
datetime nyTime = gmtTime + NY_Time_Offset_Hours * 3600;
// Extract date and time components in NY time
MqlDateTime nyStruct;
TimeToStruct(nyTime, nyStruct);
// Check if this is a new day (reset ranges daily)
datetime currentDay = StructToTime(nyStruct); // Midnight of current NY day
currentDay -= nyStruct.hour * 3600 + nyStruct.min * 60 + nyStruct.sec; // Set to 00:00 NY
if (currentDay != lastDay)
{
// New day: Calculate the range for 08:20-09:20 NY
CalculateDailyRange(time, high, low, rates_total, currentDay);
lastDay = currentDay;
}
// Assign buffer values (extend lines to the right)
HighBuffer[i] = (HighBuffer[i-1] > 0) ? HighBuffer[i-1] : 0;
LowBuffer[i] = (LowBuffer[i-1] > 0) ? LowBuffer[i-1] : 0;
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Function to calculate the daily range |
//+------------------------------------------------------------------+
void CalculateDailyRange(const datetime &time[], const double &high[], const double &low[], int rates_total, datetime nyDayStart)
{
double rangeHigh = 0;
double rangeLow = DBL_MAX;
bool inRange = false;
// Define start and end times in NY (seconds since midnight)
int startSeconds = 8*3600 + 20*60; // 08:20
int endSeconds = 9*3600 + 20*60; // 09:20
// Loop through bars to find those in the NY time range
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; // Skip if not same day
// Calculate seconds since NY midnight for this bar
int barSeconds = nyStruct.hour*3600 + nyStruct.min*60 + nyStruct.sec;
// Check if bar is within or overlaps the range
if (barSeconds >= startSeconds && barSeconds < endSeconds)
{
inRange = true;
if (high[j] > rangeHigh) rangeHigh = high[j];
if (low[j] < rangeLow) rangeLow = low[j];
}
else if (inRange)
{
// We've passed the range; stop scanning this day
break;
}
}
// If a valid range was found, set the buffers starting from the end of the range
if (rangeHigh > 0 && rangeLow < DBL_MAX)
{
// Find the bar index where the range ends (approximately)
for (int k = rates_total - 1; k >= 0; k--)
{
datetime nyTime = time[k] + NY_Time_Offset_Hours * 3600;
MqlDateTime nyStruct;
TimeToStruct(nyTime, nyStruct);
int barSeconds = nyStruct.hour*3600 + nyStruct.min*60 + nyStruct.sec;
if (barSeconds >= endSeconds && StructToTime(nyStruct) - barSeconds == nyDayStart)
{
HighBuffer[k] = rangeHigh;
LowBuffer[k] = rangeLow;
break;
}
}
}
}
How to Use:
- Attach to Chart: Drag the indicator onto your MT5 chart.
- Inputs:
NY_Time_Offset_Hours: Set to -4 (DST) or -5 (non-DST). Check current offset via tools like timeanddate.com.
- Customize colors, style, and width.
- What You'll See: Two lines (blue for high, red for low) representing the 08:20-09:20 NY range, extending rightward until the next day.
- Testing: Backtest on historical data or use in a demo account. For example, on EURUSD H1, it should plot daily ranges.
- Enhancements: If you need additions like rectangles, alerts on breakouts, or auto-DST detection, let me know for modifications!
If this doesn't match what you meant (e.g., if "opening balance" refers to something specific like account balance or a different concept), provide more details for adjustments!
//+------------------------------------------------------------------+
//| InitialOpeningBalanceRange.mq5 |
//| Copyright 2023, AI Assistant |
//| https://example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, AI Assistant"
#property link "https://example.com"
#property version "1.01"
#property strict
#property indicator_chart_window
#property indicator_buffers 10
#property indicator_plots 10
//--- 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 50%"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrLightBlue
#property indicator_style6 STYLE_DASH
#property indicator_width6 1
#property indicator_label7 "Fib 61.8%"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrDodgerBlue
#property indicator_style7 STYLE_DASH
#property indicator_width7 1
#property indicator_label8 "Fib 100%"
#property indicator_type8 DRAW_LINE
#property indicator_color8 clrDarkBlue
#property indicator_style8 STYLE_DASH
#property indicator_width8 1
#property indicator_label9 "Fib 127.2%"
#property indicator_type9 DRAW_LINE
#property indicator_color9 clrNavy
#property indicator_style9 STYLE_DASH
#property indicator_width9 1
#property indicator_label10 "Fib 161.8%"
#property indicator_type10 DRAW_LINE
#property indicator_color10 clrMidnightBlue
#property indicator_style10 STYLE_DASH
#property indicator_width10 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 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 Fib50Buffer[];
double Fib618Buffer[];
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, Fib50Buffer);
SetIndexBuffer(6, Fib618Buffer);
SetIndexBuffer(7, Fib100Buffer);
SetIndexBuffer(8, Fib1272Buffer);
SetIndexBuffer(9, Fib1618Buffer);
IndicatorShortName("Initial Opening Balance Range with Fib (08:20-09:20 NY)");
// Set empty values
for (int i = 0; i < 10; i++) {
SetIndexEmptyValue(i, EMPTY_VALUE);
}
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); // Plot immediately at this bar
}
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];
Fib50Buffer[i] = Fib50Buffer[i-1];
Fib618Buffer[i] = Fib618Buffer[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;
Fib50Buffer[i] = EMPTY_VALUE;
Fib618Buffer[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)
{
// Range calculated; set end time to the bar's time (NY)
rangeEndTime = nyTime;
break;
}
}
// Set range lines starting from range end
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;
// Calculate levels
double diff = fib100 - fib0;
Fib0Buffer[barIndex] = fib0 + 0.0 * diff;
Fib236Buffer[barIndex] = fib0 + 0.236 * diff;
Fib382Buffer[barIndex] = fib0 + 0.382 * diff;
Fib50Buffer[barIndex] = fib0 + 0.5 * diff;
Fib618Buffer[barIndex] = fib0 + 0.618 * 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;
// Dynamically adjust colors based on direction (optional, can be customized)
if (direction == 1) {
IndicatorSetInteger(INDICATOR_COLOR2 + 1, Bullish_Fib_Color); // Adjust for Fib plots (indices 3-10)
// Note: For full color customization per level, more code would be needed.
} else {
IndicatorSetInteger(INDICATOR_COLOR2 + 1, Bearish_Fib_Color);
}
}