The MT5 version uses the exact same drawing logic but requires strict #property indicator_plots 0 so the engine does not look for graphical buffers.
Code: Select all
//+------------------------------------------------------------------+
//| Session_Enforcer_MT5.mq5 |
//| On-chart visual budget accountability |
//+------------------------------------------------------------------+
#property copyright "Your Custom Tool"
#property indicator_chart_window
#property indicator_plots 0
//--- Inputs
input string LondonStart = "08:00"; // London Start Time (Broker Time)
input string LondonEnd = "11:30"; // London End Time
input string NYStart = "13:30"; // NY Start Time
input string NYEnd = "16:30"; // NY End Time
input double MaxRisk = 2.0; // Max Risk Cap (R or %)
input int MaxTrades = 3; // Max Trades Per Session
input string AllowedAssets = "EURUSD, XAUUSD"; // Allowed Assets
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit() {
EventSetTimer(1); // Update clock every 1 second
DrawStaticDashboard();
UpdateDashboard(); // Initial run
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
EventKillTimer();
ObjectsDeleteAll(0, "Budg_");
}
//+------------------------------------------------------------------+
//| 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[]) {
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer() {
UpdateDashboard();
}
//+------------------------------------------------------------------+
//| Session & Dashboard Logic |
//+------------------------------------------------------------------+
bool IsInSession(string start_str, string end_str) {
datetime now = TimeCurrent();
string today = TimeToString(now, TIME_DATE);
datetime start_time = StringToTime(today + " " + start_str);
datetime end_time = StringToTime(today + " " + end_str);
// Handle sessions that span across midnight
if (end_time < start_time) {
end_time += PeriodSeconds(PERIOD_D1);
if (now < start_time) {
start_time -= PeriodSeconds(PERIOD_D1);
end_time -= PeriodSeconds(PERIOD_D1);
}
}
return (now >= start_time && now <= end_time);
}
void CreateLabel(string name, string text, int x, int y, color col, int fontSize = 10, bool isValue = false) {
ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_RIGHT_LOWER);
ObjectSetInteger(0, name, OBJ, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);
ObjectSetString(0, name, OBJPROP_TEXT, text);
ObjectSetString(0, name, OBJPROP_FONT, "Trebuchet MS");
ObjectSetInteger(0, name, OBJPROP_COLOR, col);
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, fontSize);
// Anchor values to the right, labels to the right with offset
ObjectSetInteger(0, name, OBJPROP_ANCHOR, isValue ? ANCHOR_RIGHT_LOWER : ANCHOR_RIGHT_LOWER);
}
void DrawStaticDashboard() {
int lbl_x = 150; // Left column X
int val_x = 10; // Right column X
CreateLabel("Budg_Title", "--- SESSION BUDGET ---", val_x, 150, clrWhite, 10, true);
CreateLabel("Budg_Lbl_Window", "Active Window:", lbl_x, 120, clrSilver, 10);
CreateLabel("Budg_Val_Window", "INIT", val_x, 120, clrGray, 10, true);
CreateLabel("Budg_Lbl_Risk", "Max Risk Cap:", lbl_x, 95, clrSilver, 10);
CreateLabel("Budg_Val_Risk", DoubleToString(MaxRisk, 1) + " R", val_x, 95, clrYellow, 10, true);
CreateLabel("Budg_Lbl_Trades", "Trade Limit:", lbl_x, 70, clrSilver, 10);
CreateLabel("Budg_Val_Trades", IntegerToString(MaxTrades), val_x, 70, clrYellow, 10, true);
CreateLabel("Budg_Lbl_Assets", "Allowed Assets:", lbl_x, 45, clrSilver, 10);
CreateLabel("Budg_Val_Assets", AllowedAssets, val_x, 45, clrYellow, 10, true);
CreateLabel("Budg_Warning", "INIT", val_x, 15, clrGray, 11, true);
}
void UpdateDashboard() {
bool inLondon = IsInSession(LondonStart, LondonEnd);
bool inNY = IsInSession(NYStart, NYEnd);
string activeWindow = "OUT OF SESSION";
color windowColor = clrRed;
string warningText = "HANDS OFF KEYBOARD";
color warningColor = clrRed;
if(inLondon) {
activeWindow = "LONDON";
windowColor = clrAqua;
warningText = "STICK TO THE PLAN";
warningColor = clrLime;
} else if(inNY) {
activeWindow = "NEW YORK";
windowColor = clrOrange;
warningText = "STICK TO THE PLAN";
warningColor = clrLime;
}
ObjectSetString(0, "Budg_Val_Window", OBJPROP_TEXT, activeWindow);
ObjectSetInteger(0, "Budg_Val_Window", OBJPROP_COLOR, windowColor);
ObjectSetString(0, "Budg_Warning", OBJPROP_TEXT, warningText);
ObjectSetInteger(0, "Budg_Warning", OBJPROP_COLOR, warningColor);
ChartRedraw();
}