This MQL4 indicator dynamically shades the Friday "Danger Zone" across the chart window and plots a trendline and point-measurement label across the Sunday/Monday gap.
Code: Select all
//+------------------------------------------------------------------+
//| FundingPips_WeekendGap.mq4 |
//| Gap Policy & Accidental Hold Visualizer |
//+------------------------------------------------------------------+
#property copyright "Trading Tools"
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_plots 0
input int InpDangerZoneHour = 20; // Friday Danger Zone Start Hour (Broker Time)
input color InpDangerZoneColor = C'45,15,15'; // Danger Zone Shading Color
input color InpGapLineColor = clrRed; // Weekend Gap Trajectory Color
input color InpTextColor = clrWhite; // Gap Label Text Color
input string InpPrefix = "FP_WGap_"; // Object Name Prefix
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, InpPrefix);
ChartRedraw(0);
}
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);
int start = (prev_calculated > 1) ? prev_calculated - 1 : 1;
for(int i = start; i < rates_total; i++)
{
MqlDateTime dtPrev, dtCurr;
TimeToStruct(time[i - 1], dtPrev);
TimeToStruct(time[i], dtCurr);
// 1. Highlight Friday Danger Zone
if(dtCurr.day_of_week == 5 && dtCurr.hour >= InpDangerZoneHour)
{
string zoneName = InpPrefix + "Zone_" + TimeToString(time[i], TIME_DATE);
if(ObjectFind(0, zoneName) < 0)
{
// Create shading box (non-selectable so it won't distort chart auto-scaling)
ObjectCreate(0, zoneName, OBJ_RECTANGLE, 0, time[i], 2000000.0, time[i] + PeriodSeconds(), 0.00001);
ObjectSetInteger(0, zoneName, OBJPROP_COLOR, InpDangerZoneColor);
ObjectSetInteger(0, zoneName, OBJPROP_BACK, true);
ObjectSetInteger(0, zoneName, OBJPROP_FILL, true);
ObjectSetInteger(0, zoneName, OBJPROP_SELECTABLE, false);
}
else
{
// Dynamically extend to cover subsequent Friday bars until market close
ObjectSetInteger(0, zoneName, OBJPROP_TIME, 1, time[i] + PeriodSeconds());
}
}
// 2. Measure Weekend Gap (Friday close -> Sunday/Monday open)
if(dtPrev.day_of_week == 5 && (dtCurr.day_of_week == 0 || dtCurr.day_of_week == 1))
{
double gapSize = open[i] - close[i - 1];
double gapPoints = MathAbs(gapSize) / _Point;
string lineName = InpPrefix + "Line_" + TimeToString(time[i], TIME_DATE);
if(ObjectFind(0, lineName) < 0)
{
ObjectCreate(0, lineName, OBJ_TREND, 0, time[i - 1], close[i - 1], time[i], open[i]);
ObjectSetInteger(0, lineName, OBJPROP_COLOR, InpGapLineColor);
ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 2);
ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, lineName, OBJPROP_RAY_RIGHT, false);
ObjectSetInteger(0, lineName, OBJPROP_BACK, false);
}
string labelName = InpPrefix + "Lbl_" + TimeToString(time[i], TIME_DATE);
if(ObjectFind(0, labelName) < 0)
{
ObjectCreate(0, labelName, OBJ_TEXT, 0, time[i], open[i]);
ObjectSetString(0, labelName, OBJPROP_TEXT, StringFormat(" Weekend Gap: %.0f pts", gapPoints));
ObjectSetInteger(0, labelName, OBJPROP_COLOR, InpTextColor);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 9);
ObjectSetInteger(0, labelName, OBJPROP_ANCHOR, ANCHOR_LEFT);
}
}
}
return(rates_total);
}