Save as an Indicator (.mq5). It uses MT5's PeriodSeconds() and iTime structure for precise real-time state management.
Code: Select all
//+------------------------------------------------------------------+
//| ExecutionAnchor_MT5.mq5 |
//| Mindful Scalping UI Overlay |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_plots 0
//--- Inputs
input color InpColorFast = clrMaroon;
input color InpColorOpt = clrTeal;
input color InpColorMacro = clrNavy;
input color InpColorText = clrWhite;
//--- Object Names
string obj_bg = "ExecAnchor_BG";
string obj_header = "ExecAnchor_Header";
string obj_timer = "ExecAnchor_Timer";
string obj_body1 = "ExecAnchor_Body1";
string obj_body2 = "ExecAnchor_Body2";
string obj_body3 = "ExecAnchor_Body3";
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
EventSetTimer(1); // 1-second tick for the timer
CreateUI();
UpdateDashboard();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
EventKillTimer();
ObjectsDeleteAll(0, "ExecAnchor_");
ChartRedraw();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[])
{
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
UpdateDashboard();
}
//+------------------------------------------------------------------+
//| Core Logic & UI Updates |
//+------------------------------------------------------------------+
void UpdateDashboard()
{
int tfMinutes = PeriodSeconds(_Period) / 60;
color stateColor = InpColorMacro;
string headerStr = "";
string b1 = "", b2 = "", b3 = "";
// Determine State
if(tfMinutes < 5)
{
stateColor = InpColorFast;
headerStr = "⚠️ RUSHED EXECUTION ZONE (" + EnumToString(_Period) + ")";
b1 = "There is no prize for the fastest timeframe.";
b2 = "If you are second-guessing entries or missing closes,";
b3 = "drop to a slower chart where you can think clearly.";
}
else if(tfMinutes >= 5 && tfMinutes <= 15)
{
stateColor = InpColorOpt;
headerStr = "✅ CLEAN EXECUTION ZONE (" + EnumToString(_Period) + ")";
b1 = "Match execution to actual processing speed.";
b2 = "Focus on clean, consistent decisions without";
b3 = "the rushed pressure of micro-timeframes.";
}
else
{
stateColor = InpColorMacro;
headerStr = "📘 MACRO CONTEXT ZONE (" + EnumToString(_Period) + ")";
b1 = "Higher timeframes dictate structural bias.";
b2 = "Ensure your execution timeframe aligns with";
b3 = "your demonstrated reaction speed.";
}
// Timer Calculation
datetime currentCandleOpen = iTime(_Symbol, _Period, 0);
datetime nextCandleOpen = currentCandleOpen + PeriodSeconds(_Period);
int secondsRemaining = (int)(nextCandleOpen - TimeCurrent());
string timerStr = "Market Closed";
if(secondsRemaining > 0)
{
int h = secondsRemaining / 3600;
int m = (secondsRemaining % 3600) / 60;
int s = secondsRemaining % 60;
timerStr = StringFormat("Candle Close: %02d:%02d:%02d", h, m, s);
}
// Update Objects
ObjectSetInteger(0, obj_bg, OBJPROP_BGCOLOR, stateColor);
ObjectSetString(0, obj_header, OBJPROP_TEXT, headerStr);
ObjectSetString(0, obj_timer, OBJPROP_TEXT, timerStr);
ObjectSetString(0, obj_body1, OBJPROP_TEXT, b1);
ObjectSetString(0, obj_body2, OBJPROP_TEXT, b2);
ObjectSetString(0, obj_body3, OBJPROP_TEXT, b3);
ChartRedraw();
}
//+------------------------------------------------------------------+
//| UI Generation |
//+------------------------------------------------------------------+
void CreateUI()
{
int x = 20, y = 20;
// Background
ObjectCreate(0, obj_bg, OBJ_RECTANGLE_LABEL, 0, 0, 0);
ObjectSetInteger(0, obj_bg, OBJPROP_CORNER, CORNER_RIGHT_LOWER);
ObjectSetInteger(0, obj_bg, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, obj_bg, OBJPROP_YDISTANCE, y);
ObjectSetInteger(0, obj_bg, OBJPROP_XSIZE, 350);
ObjectSetInteger(0, obj_bg, OBJPROP_YSIZE, 110);
ObjectSetInteger(0, obj_bg, OBJPROP_COLOR, clrNONE);
ObjectSetInteger(0, obj_bg, OBJPROP_BACK, true);
// Text Creation Macro
CreateText(obj_header, x + 10, y + 80, 10, true);
CreateText(obj_timer, x + 10, y + 60, 10, true);
CreateText(obj_body1, x + 10, y + 40, 8, false);
CreateText(obj_body2, x + 10, y + 25, 8, false);
CreateText(obj_body3, x + 10, y + 10, 8, false);
}
void CreateText(string name, int x, int y, int fontSize, bool isBold)
{
ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_RIGHT_LOWER);
ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
ObjectSetString(0, name, OBJPROP_FONT, "Consolas");
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, fontSize);
ObjectSetInteger(0, name, OBJPROP_COLOR, InpColorText);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
}