I'll modify the indicator to add Fibonacci levels that appear when price breaks out of the Initial Balance range. Here's the enhanced version:
//+------------------------------------------------------------------+
//| InitialBalanceWithFibs.mq5 |
//| Copyright 2024, Your Name |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, Your Name"
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
//--- plot HighLine
#property indicator_label1 "IB High"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- plot LowLine
#property indicator_label2 "IB Low"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--- input parameters
input int GMTOffset = -5; // GMT Offset for NY Time (EST=-5, EDT=-4)
input color HighColor = clrBlue; // High Line Color
input color LowColor = clrRed; // Low Line Color
input int LineWidth = 2; // Line Width
input bool ShowLabels = true; // Show Price Labels
input bool ShowFibs = true; // Show Fibonacci Levels
input color FibColor = clrGray; // Fibonacci Lines Color
input ENUM_LINE_STYLE FibStyle = STYLE_DOT; // Fibonacci Line Style
input int FibWidth = 1; // Fibonacci Line Width
//--- Fibonacci levels
input double Fib1 = 0.236; // Fibonacci Level 1
input double Fib2 = 0.382; // Fibonacci Level 2
input double Fib3 = 0.500; // Fibonacci Level 3
input double Fib4 = 0.618; // Fibonacci Level 4
input double Fib5 = 0.786; // Fibonacci Level 5
input double Fib6 = 1.000; // Fibonacci Level 6
input double Fib7 = 1.272; // Fibonacci Level 7
input double Fib8 = 1.618; // Fibonacci Level 8
//--- indicator buffers
double HighBuffer[];
double LowBuffer[];
//--- global variables
datetime lastCalculatedDay = 0;
double ibHigh = 0;
double ibLow = 0;
bool breakoutDetected = false;
bool isBullishBreakout = false;
datetime breakoutTime = 0;
string fibPrefix = "IB_Fib_";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0, HighBuffer, INDICATOR_DATA);
SetIndexBuffer(1, LowBuffer, INDICATOR_DATA);
//--- set line properties
PlotIndexSetInteger(0, PLOT_LINE_COLOR, HighColor);
PlotIndexSetInteger(0, PLOT_LINE_WIDTH, LineWidth);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, LowColor);
PlotIndexSetInteger(1, PLOT_LINE_WIDTH, LineWidth);
//--- set empty value
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
//--- name for DataWindow and indicator subwindow label
IndicatorSetString(INDICATOR_SHORTNAME, "IB Range with Fibonacci (08:20-09:20 NY)");
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[])
{
//--- check for bars
if(rates_total < 2)
return(0);
//--- main calculation loop
for(int i = MathMax(1, prev_calculated); i < rates_total; i++)
{
//--- get current bar time
datetime barTime = time[i];
MqlDateTime dt;
TimeToStruct(barTime, dt);
//--- convert to NY time
datetime nyTime = barTime + GMTOffset * 3600;
TimeToStruct(nyTime, dt);
//--- check if it's a new day
datetime currentDay = StringToTime(TimeToString(nyTime, TIME_DATE));
if(currentDay != lastCalculatedDay)
{
//--- reset values for new day
ibHigh = 0;
ibLow = 0;
breakoutDetected = false;
breakoutTime = 0;
lastCalculatedDay = currentDay;
//--- remove previous day's Fibonacci levels
RemoveFibonacciLevels();
//--- find IB range for this day
CalculateIBRange(time, high, low, i, rates_total);
}
//--- set buffer values
if(ibHigh > 0 && ibLow > 0)
{
HighBuffer[i] = ibHigh;
LowBuffer[i] = ibLow;
//--- check for breakout after IB period
datetime ibEndTime = lastCalculatedDay + (9 * 3600 + 20 * 60) - GMTOffset * 3600;
if(barTime > ibEndTime && !breakoutDetected && ShowFibs)
{
CheckBreakout(high[i], low[i], close[i], time[i]);
}
}
else
{
HighBuffer[i] = 0;
LowBuffer[i] = 0;
}
}
//--- draw labels if enabled
if(ShowLabels && ibHigh > 0 && ibLow > 0)
{
DrawPriceLabels();
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Check for breakout |
//+------------------------------------------------------------------+
void CheckBreakout(double high, double low, double close, datetime time)
{
if(breakoutDetected)
return;
//--- bullish breakout
if(close > ibHigh)
{
breakoutDetected = true;
isBullishBreakout = true;
breakoutTime = time;
DrawFibonacciLevels(true);
}
//--- bearish breakout
else if(close < ibLow)
{
breakoutDetected = true;
isBullishBreakout = false;
breakoutTime = time;
DrawFibonacciLevels(false);
}
}
//+------------------------------------------------------------------+
//| Draw Fibonacci levels |
//+------------------------------------------------------------------+
void DrawFibonacciLevels(bool bullish)
{
double range = ibHigh - ibLow;
double levels[] = {0.0, Fib1, Fib2, Fib3, Fib4, Fib5, Fib6, Fib7, Fib8};
string levelNames[] = {"0.0", "23.6", "38.2", "50.0", "61.8", "78.6", "100.0", "127.2", "161.8"};
for(int i = 0; i < ArraySize(levels); i++)
{
double price;
if(bullish)
{
// For bullish: 100% at bottom, 0% at top
price = ibLow + range * (1.0 - levels[i]);
if(levels[i] > 1.0)
price = ibLow - range * (levels[i] - 1.0);
}
else
{
// For bearish: 100% at top, 0% at bottom
price = ibHigh - range * (1.0 - levels[i]);
if(levels[i] > 1.0)
price = ibHigh + range * (levels[i] - 1.0);
}
string objName = fibPrefix + levelNames[i];
//--- create horizontal line
if(ObjectCreate(0, objName, OBJ_HLINE, 0, 0, price))
{
ObjectSetInteger(0, objName, OBJPROP_COLOR, FibColor);
ObjectSetInteger(0, objName, OBJPROP_STYLE, FibStyle);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, FibWidth);
ObjectSetInteger(0, objName, OBJPROP_BACK, true);
ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, objName, OBJPROP_SELECTED, false);
}
//--- create label
string labelName = objName + "_Label";
if(ObjectCreate(0, labelName, OBJ_TEXT, 0, breakoutTime, price))
{
string labelText = levelNames[i] + "% - " + DoubleToString(price, _Digits);
ObjectSetString(0, labelName, OBJPROP_TEXT, labelText);
ObjectSetInteger(0, labelName, OBJPROP_COLOR, FibColor);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 8);
ObjectSetInteger(0, labelName, OBJPROP_ANCHOR, ANCHOR_LEFT_CENTER);
}
}
//--- add breakout direction label
string breakoutLabel = fibPrefix + "Breakout";
double labelPrice = bullish ? ibHigh + range * 0.1 : ibLow - range * 0.1;
if(ObjectCreate(0, breakoutLabel, OBJ_TEXT, 0, breakoutTime, labelPrice))
{
string text = bullish ? "Bullish Breakout" : "Bearish Breakout";
ObjectSetString(0, breakoutLabel, OBJPROP_TEXT, text);
ObjectSetInteger(0, breakoutLabel, OBJPROP_COLOR, bullish ? clrGreen : clrRed);
ObjectSetInteger(0, breakoutLabel, OBJPROP_FONTSIZE, 10);
ObjectSetInteger(0, breakoutLabel, OBJPROP_ANCHOR, ANCHOR_LEFT_CENTER);
}
}
//+------------------------------------------------------------------+
//| Remove Fibonacci levels |
//+------------------------------------------------------------------+
void RemoveFibonacciLevels()
{
string levelNames[] = {"0.0", "23.6", "38.2", "50.0", "61.8", "78.6", "100.0", "127.2", "161.8"};
for(int i = 0; i < ArraySize(levelNames); i++)
{
ObjectDelete(0, fibPrefix + levelNames[i]);
ObjectDelete(0, fibPrefix + levelNames[i] + "_Label");
}
ObjectDelete(0, fibPrefix + "Breakout");
}
//+------------------------------------------------------------------+
//| Calculate Initial Balance Range |
//+------------------------------------------------------------------+
void CalculateIBRange(const datetime &time[], const double &high[],
const double &low[], int currentBar, int rates_total)
{
datetime startTime = lastCalculatedDay + (8 * 3600 + 20 * 60) - GMTOffset * 3600;
datetime endTime = lastCalculatedDay + (9 * 3600 + 20 * 60) - GMTOffset * 3600;
double tempHigh = 0;
double tempLow = DBL_MAX;
bool foundRange = false;
//--- search backwards from current bar
for(int j = currentBar; j >= 0; j--)
{
if(time[j] >= startTime && time[j] <= endTime)
{
if(high[j] > tempHigh) tempHigh = high[j];
if(low[j] < tempLow) tempLow = low[j];
foundRange = true;
}
else if(time[j] < startTime && foundRange)
{
break;
}
}
//--- search forward if needed
if(!foundRange)
{
for(int j = currentBar; j < rates_total; j++)
{
if(time[j] >= startTime && time[j] <= endTime)
{
if(high[j] > tempHigh) tempHigh = high[j];
if(low[j] < tempLow) tempLow = low[j];
foundRange = true;
}
else if(time[j] > endTime && foundRange)
{
break;
}
}
}
if(foundRange && tempLow != DBL_MAX)
{
ibHigh = tempHigh;
ibLow = tempLow;
}
}
//+------------------------------------------------------------------+
//| Draw price labels |
//+------------------------------------------------------------------+
void DrawPriceLabels()
{
//--- delete old labels
ObjectDelete(0, "IB_High_Label");
ObjectDelete(0, "IB_Low_Label");
//--- create high label
if(ObjectCreate(0, "IB_High_Label", OBJ_TEXT, 0, TimeCurrent(), ibHigh))
{
ObjectSetString(0, "IB_High_Label", OBJPROP_TEXT, "IB High: " + DoubleToString(ibHigh, _Digits));
ObjectSetInteger(0, "IB_High_Label", OBJPROP_COLOR, HighColor);
ObjectSetInteger(0, "IB_High_Label", OBJPROP_FONTSIZE, 9);
ObjectSetInteger(0, "IB_High_Label", OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER);
}
//--- create low label
if(ObjectCreate(0, "IB_Low_Label", OBJ_TEXT, 0, TimeCurrent(), ibLow))
{
ObjectSetString(0, "IB_Low_Label", OBJPROP_TEXT, "IB Low: " + DoubleToString(ibLow, _Digits));
ObjectSetInteger(0, "IB_Low_Label", OBJPROP_COLOR, LowColor);
ObjectSetInteger(0, "IB_Low_Label", OBJPROP_FONTSIZE, 9);
ObjectSetInteger(0, "IB_Low_Label", OBJPROP_ANCHOR, ANCHOR_LEFT_UPPER);
}
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- delete all objects
ObjectDelete(0, "IB_High_Label");
ObjectDelete(0, "IB_Low_Label");
RemoveFibonacciLevels();
}
//+------------------------------------------------------------------+