Vizion-Trading-EA/AILabels.mqh
SahrJohn SahrJohn 526860b9cd EA: sync live build (Jul-Nov 2026) — trade report direction/setup fix, VZ indicators, risk modules
- AIGateway/profitgtx: report_trade takes direction and setup from the position's
  opening deal ("dir_src":"entry"); the closing deal inverted every direction and
  restarts lost the setup (99% "auto")
- add MATheory, RiskGuards, RiskSizing, TradePolicy (setup blocklist, strict AI gate)
- add VZ_Core + VZ_Features/MFIB/Regime/Setups/Sessions indicators, VZ_FeaturesExport
  service, VZ_DumpMA, VZ_TogglePause
- margin-aware sizing, news, labels and session updates since 2026-07-14

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 13:48:22 -06:00
#ifndef AI_LABELS_MQH
#define AI_LABELS_MQH
//+------------------------------------------------------------------+
//| AILabels.mqh |
//| Draggable status panel. Replaces both the old top-left status |
//| line and the old top-right AI labels, and carries everything |
//| they showed. |
//| |
//| The old top-right labels anchored ANCHOR_LEFT_UPPER against |
//| CORNER_RIGHT_UPPER and so ran off the right edge of the chart — |
//| only their first character was ever visible. Every label here |
//| sets OBJPROP_ANCHOR explicitly. |
//| |
//| Anchored CORNER_LEFT_UPPER so it lands where the old status line |
//| was. Keys anchor LEFT at the panel's inner-left edge, values |
//| anchor RIGHT at its inner-right edge, so both columns stay put |
//| regardless of text length and nothing overflows the box. |
//| |
//| Position lives in g_panel_x / g_panel_y rather than in the inputs |
//| so it can be dragged. Drag the panel background to move it; the |
//| position is saved per symbol+timeframe and restored by |
//| PanelInit(). Delete the saved globals to reset to the inputs. |
//+------------------------------------------------------------------+
#define VZP_PREFIX "VZP_"
#define VZP_BG_OBJ "VZP_BG"
// Palette
#define VZP_BG C'22,24,30'
#define VZP_BORDER C'60,64,74'
#define VZP_RULE C'48,52,60'
#define VZP_TITLE C'225,228,238'
#define VZP_KEY C'126,134,148'
#define VZP_VAL C'220,224,232'
#define VZP_GOOD C'80,220,140'
#define VZP_WARN C'255,182,72'
#define VZP_BAD C'255,96,96'
#define VZP_DIM C'118,122,132'
int g_panel_x = 8; // live position: offset from the top-LEFT chart corner
int g_panel_y = 14;
int g_vzp_y = 0; // running y cursor while laying out
int g_vzp_row = 0; // row counter, drives object names
string g_vzp_drawn[]; // object names drawn in the current pass (stale ones are swept afterwards)
void VZP_Mark(const string n)
{
int k = ArraySize(g_vzp_drawn);
ArrayResize(g_vzp_drawn, k + 1);
g_vzp_drawn[k] = n;
}
int g_vzp_h = 0; // row height, derived from font size
//+------------------------------------------------------------------+
//| Position persistence |
//+------------------------------------------------------------------+
string VZP_PosKey(const string axis)
{
return "VZP_" + axis + "_" + _Symbol + "_" + IntegerToString((int)Period());
}
void PanelInit()
{
g_panel_x = Panel_X;
g_panel_y = Panel_Y;
string kx = VZP_PosKey("X");
string ky = VZP_PosKey("Y");
if(GlobalVariableCheck(kx)) g_panel_x = (int)GlobalVariableGet(kx);
if(GlobalVariableCheck(ky)) g_panel_y = (int)GlobalVariableGet(ky);
// A panel dragged off-screen, or saved on a larger window, would be
// unreachable — pull it back into view.
int cw = (int)ChartGetInteger(0, CHART_WIDTH_IN_PIXELS);
int ch = (int)ChartGetInteger(0, CHART_HEIGHT_IN_PIXELS);
if(cw > 0 && (g_panel_x < 0 || g_panel_x > cw - 40)) g_panel_x = Panel_X;
if(ch > 0 && (g_panel_y < 0 || g_panel_y > ch - 40)) g_panel_y = Panel_Y;
}
void PanelSavePos()
{
GlobalVariableSet(VZP_PosKey("X"), (double)g_panel_x);
GlobalVariableSet(VZP_PosKey("Y"), (double)g_panel_y);
}
//+------------------------------------------------------------------+
//| Primitives |
//+------------------------------------------------------------------+
void VZP_Box(const string id, const int x, const int y, const int w, const int h,
const color bg, const color border, const bool draggable = false)
{
string n = VZP_PREFIX + id;
VZP_Mark(n);
if(ObjectFind(0, n) < 0)
ObjectCreate(0, n, OBJ_RECTANGLE_LABEL, 0, 0, 0);
ObjectSetInteger(0, n, OBJPROP_CORNER, CORNER_LEFT_UPPER);
ObjectSetInteger(0, n, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, n, OBJPROP_YDISTANCE, y);
ObjectSetInteger(0, n, OBJPROP_XSIZE, w);
ObjectSetInteger(0, n, OBJPROP_YSIZE, h);
ObjectSetInteger(0, n, OBJPROP_BGCOLOR, bg);
ObjectSetInteger(0, n, OBJPROP_BORDER_TYPE, BORDER_FLAT);
ObjectSetInteger(0, n, OBJPROP_COLOR, border);
ObjectSetInteger(0, n, OBJPROP_BACK, false);
// Only the background is selectable — that is what the user grabs to move
// the panel. Rows stay unselectable so they never intercept the click.
ObjectSetInteger(0, n, OBJPROP_SELECTABLE, draggable);
ObjectSetInteger(0, n, OBJPROP_HIDDEN, true);
}
void VZP_Text(const string id, const int x, const int y, const string text,
const color clr, const int size, const bool right_align)
{
string n = VZP_PREFIX + id;
VZP_Mark(n);
if(ObjectFind(0, n) < 0)
ObjectCreate(0, n, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, n, OBJPROP_CORNER, CORNER_LEFT_UPPER);
// Explicit anchor: leaving this at the default is what broke the old labels.
ObjectSetInteger(0, n, OBJPROP_ANCHOR, right_align ? ANCHOR_RIGHT_UPPER : ANCHOR_LEFT_UPPER);
ObjectSetInteger(0, n, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0, n, OBJPROP_YDISTANCE, y);
ObjectSetString (0, n, OBJPROP_TEXT, text);
ObjectSetString (0, n, OBJPROP_FONT, "Segoe UI");
ObjectSetInteger(0, n, OBJPROP_FONTSIZE, size);
ObjectSetInteger(0, n, OBJPROP_COLOR, clr);
ObjectSetInteger(0, n, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, n, OBJPROP_HIDDEN, true);
}
// Key/value row. Key starts at the inner-left edge and grows right; value ends
// at the inner-right edge and grows left. Neither can overflow the box.
void VZP_Row(const string key, const string val, const color vclr)
{
int keyX = g_panel_x + 9;
int valX = g_panel_x + Panel_Width - 9;
string r = IntegerToString(g_vzp_row);
VZP_Text("K" + r, keyX, g_vzp_y, key, VZP_KEY, Panel_FontSize, false);
VZP_Text("V" + r, valX, g_vzp_y, val, vclr, Panel_FontSize, true);
g_vzp_y += g_vzp_h;
g_vzp_row++;
}
// Full-width line, left aligned. Used for free text (briefings, AI notes).
void VZP_Line(const string text, const color clr)
{
VZP_Text("L" + IntegerToString(g_vzp_row), g_panel_x + 9, g_vzp_y,
text, clr, Panel_FontSize, false);
g_vzp_y += g_vzp_h;
g_vzp_row++;
}
void VZP_Rule()
{
VZP_Box("R" + IntegerToString(g_vzp_row), g_panel_x + 7, g_vzp_y + 3,
Panel_Width - 14, 1, VZP_RULE, VZP_RULE);
g_vzp_y += 9;
g_vzp_row++;
}
//+------------------------------------------------------------------+
//| Value formatting helpers |
//+------------------------------------------------------------------+
color VZP_BiasColor(const int dir)
{
if(dir > 0) return VZP_GOOD;
if(dir < 0) return VZP_BAD;
return VZP_DIM;
}
color VZP_GatewayColor(const string status)
{
if(status == "ACTIVE") return VZP_GOOD;
if(status == "OFFLINE_ADVISORY") return VZP_WARN;
if(status == "ERROR") return VZP_BAD;
return VZP_DIM;
}
string VZP_NewsGate()
{
if(News_Trade_Block_Active) return "BLOCKED";
if(News_Trade_Allowed_Direction == 1) return "BUY-ONLY";
if(News_Trade_Allowed_Direction == -1) return "SELL-ONLY";
return "open";
}
color VZP_NewsGateColor()
{
if(News_Trade_Block_Active) return VZP_BAD;
if(News_Trade_Allowed_Direction != 0) return VZP_WARN;
return VZP_DIM;
}
string VZP_Clip(const string s, const int n)
{
if(StringLen(s) <= n) return s;
return StringSubstr(s, 0, n - 1) + "…";
}
// Free text as prose, broken on spaces. Always emits exactly max_lines rows so
// the row count — and therefore the object names — stay stable between
// redraws, which is what stops stale labels being stranded on the chart.
void VZP_Wrapped(const string text, const color clr, const int width, const int max_lines)
{
string s = text;
StringReplace(s, "\r", " ");
StringReplace(s, "\n", " ");
StringTrimLeft(s);
StringTrimRight(s);
for(int emitted = 0; emitted < max_lines; ++emitted)
{
if(StringLen(s) == 0) { VZP_Line("", clr); continue; }
if(StringLen(s) <= width) { VZP_Line(s, clr); s = ""; continue; }
// Last line we are allowed: clip the remainder rather than drop it.
if(emitted == max_lines - 1) { VZP_Line(VZP_Clip(s, width), clr); s = ""; continue; }
int cut = -1;
for(int i = width; i > width / 2; --i)
if(StringGetCharacter(s, i) == ' ') { cut = i; break; }
if(cut < 0) cut = width; // one long unbroken token — hard break it
VZP_Line(StringSubstr(s, 0, cut), clr);
s = StringSubstr(s, cut + 1);
StringTrimLeft(s);
}
}
// One briefing line. AIFetchBriefing() now formats these as
// "H1: SELL trending"
// — a call plus a few words, no confidence percentage — so one row is enough.
// The timeframe prefix is stripped: it is already the line's own tag.
void VZP_BriefLine(const string tf, const string raw)
{
string s = raw;
string pfx = tf + ": ";
if(StringFind(s, pfx) == 0)
s = StringSubstr(s, StringLen(pfx));
color c = VZP_DIM;
if(StringLen(s) == 0)
{
s = "—";
}
else
{
string low = s;
StringToLower(low);
if(StringFind(low, "buy") >= 0 || StringFind(low, "bull") >= 0) c = VZP_GOOD;
else if(StringFind(low, "sell") >= 0 || StringFind(low, "bear") >= 0) c = VZP_BAD;
else c = VZP_VAL;
}
VZP_Line(tf + " " + VZP_Clip(s, 32), c);
}
//+------------------------------------------------------------------+
//| Panel |
//+------------------------------------------------------------------+
void UpdateAILabel(const bool force = false)
{
if(!Panel_Show)
return;
// UpdateLabels() runs on every tick; the panel does not need to.
static uint last_draw = 0;
uint now_ms = GetTickCount();
if(!force && last_draw != 0 && (now_ms - last_draw) < 250)
return;
last_draw = now_ms;
// While the background is selected the user may be mid-drag. Follow the
// object instead of forcing it back: VZP_Box() re-asserts XDISTANCE from
// g_panel_x on every redraw, and at ~4 redraws/second that yanked the panel
// out from under the cursor before CHARTEVENT_OBJECT_DRAG — which only
// fires on mouse-release — ever had a chance to run. That is what made the
// panel feel immovable.
if(ObjectFind(0, VZP_BG_OBJ) >= 0 &&
ObjectGetInteger(0, VZP_BG_OBJ, OBJPROP_SELECTED))
{
g_panel_x = (int)ObjectGetInteger(0, VZP_BG_OBJ, OBJPROP_XDISTANCE);
g_panel_y = (int)ObjectGetInteger(0, VZP_BG_OBJ, OBJPROP_YDISTANCE);
}
g_vzp_h = Panel_FontSize + 5;
g_vzp_y = g_panel_y + 7;
g_vzp_row = 0;
ArrayResize(g_vzp_drawn, 0);
// Background first so later objects stack on top of it. Height is patched
// once the rows below have determined how tall the panel actually is.
VZP_Box("BG", g_panel_x, g_panel_y, Panel_Width, 10, VZP_BG, VZP_BORDER, true);
// ── header ──
string tf = EnumToString((ENUM_TIMEFRAMES)Period());
StringReplace(tf, "PERIOD_", "");
VZP_Text("TITLE", g_panel_x + 9, g_vzp_y,
"profitgtx " + _Symbol + " " + tf,
VZP_TITLE, Panel_FontSize + 1, false);
g_vzp_y += g_vzp_h + 2;
g_vzp_row++;
VZP_Rule();
// ── market state ──
int bias_dir = IsBullBias() ? 1 : (IsBearBias() ? -1 : 0);
string bias_txt = (bias_dir > 0) ? "BULL" : (bias_dir < 0) ? "BEAR" : "NEUTRAL";
VZP_Row("Mode", ModeToString(Current_Mode), VZP_VAL);
VZP_Row("State", StateToString(Current_State), VZP_VAL);
VZP_Row("Bias", bias_txt, VZP_BiasColor(bias_dir));
if(Use_MA_Theory)
{
VZP_Row("Stack", MAT_MomentumText(), VZP_BiasColor(MAT_MomentumDir()));
VZP_Row("HTF", MAT_HTFText(), VZP_BiasColor(g_mat_htf_trend));
VZP_Row("Big MAs", MAT_BigMAText(), VZP_BiasColor(MAT_BigMABias()));
VZP_Row("Floor", MAT_FloorText(), VZP_GOOD);
VZP_Row("Ceiling", MAT_CeilingText(), VZP_WARN);
}
VZP_Row("Warn/Praise",
IntegerToString(Active_Warnings) + " / " + IntegerToString(Active_Praise_Signals),
(Active_Praise_Signals >= Active_Warnings) ? VZP_GOOD : VZP_WARN);
// ADX / ATR / Stoch K/D rows removed — those read off the indicator
// subwindows already; the panel carries state, not raw indicator values.
VZP_Rule();
// ── coordinator ──
// With Coordinator_Gates_Setups=false the coordinator only lends its direction; it cannot veto.
VZP_Row("Coordinator",
(Coordinator_AllowTrade ? "ALLOW" : (Coordinator_Gates_Setups ? "BLOCK" : "advisory"))
+ " " + ((Coordinator_Bias_Direction > 0) ? "bull" : (Coordinator_Bias_Direction < 0) ? "bear" : "neutral")
+ " x" + DoubleToString(Coordinator_Lot_Multiplier, 2),
Coordinator_AllowTrade ? VZP_GOOD : (Coordinator_Gates_Setups ? VZP_BAD : VZP_DIM));
VZP_Rule();
// ── AI ──
VZP_Row("Gateway", GW_Status, VZP_GatewayColor(GW_Status));
int ai_total = AI_Validation_Accepts + AI_Validation_Rejects;
double ai_rate = (ai_total > 0) ? ((double)AI_Validation_Accepts / ai_total) * 100.0 : 0.0;
VZP_Row("AI accept",
StringFormat("+%d -%d (%.0f%%)", AI_Validation_Accepts, AI_Validation_Rejects, ai_rate),
(ai_total == 0) ? VZP_DIM : (ai_rate >= 50.0 ? VZP_GOOD : VZP_WARN));
VZP_Rule();
// ── session + news ──
string sess = StringLen(GW_Session_Name) > 0 ? GW_Session_Name : "—";
string phase = StringLen(GW_Session_Phase) > 0 ? GW_Session_Phase : "—";
VZP_Row("Session", sess, VZP_VAL);
VZP_Row("Phase",
(GW_Session_Confidence > 0.0)
? phase + " " + DoubleToString(GW_Session_Confidence, 0) + "%"
: phase,
VZP_VAL);
string news_dir = (News_Bias_Direction > 0) ? "BULL"
: (News_Bias_Direction < 0) ? "BEAR" : "neutral";
VZP_Row("News bias",
news_dir + " " + DoubleToString(News_Bias_Strength, 0),
VZP_BiasColor(News_Bias_Direction));
// ── news brief ──
// Replaces the old "News gate" row with one actionable line:
// "<event> -> <symbol> <action>"
// e.g. "Iraq war escalates -> XAUUSD BUY". The gate still decides the
// action, so nothing the old row showed is lost — it just reads as a call
// instead of a bare enum. Event text is the gateway headline when we have
// one, else the last MT5 calendar event.
string news_event = StringLen(GW_News_Headlines) > 0 ? GW_News_Headlines
: News_Last_Headline;
string action;
color action_clr;
if(News_Trade_Block_Active) { action = "STAND ASIDE"; action_clr = VZP_BAD; }
else if(News_Trade_Allowed_Direction == 1) { action = "BUY only"; action_clr = VZP_GOOD; }
else if(News_Trade_Allowed_Direction == -1) { action = "SELL only"; action_clr = VZP_BAD; }
else if(News_Bias_Direction > 0) { action = "BUY"; action_clr = VZP_GOOD; }
else if(News_Bias_Direction < 0) { action = "SELL"; action_clr = VZP_BAD; }
else { action = "no bias"; action_clr = VZP_DIM; }
string call = _Symbol + " " + action;
VZP_Wrapped(StringLen(news_event) > 0 ? news_event + " -> " + call : call,
action_clr, 34, 2);
VZP_Rule();
// ── book ──
VZP_Row("Open", IntegerToString(CountOpenTradesForSymbol()) + " / "
+ IntegerToString(Max_Open_Trades), VZP_VAL);
VZP_Row("Today", StringFormat("%d (%dB / %dS)", TodayTrades, BuyTrades, SellTrades), VZP_VAL);
int cd = Trade_Cooldown_Seconds - (int)(TimeCurrent() - Last_Trade_Time);
VZP_Row("Cooldown", (cd > 0) ? IntegerToString(cd) + "s" : "ready",
(cd > 0) ? VZP_WARN : VZP_GOOD);
VZP_Rule();
// ── multi-TF briefings ──
VZP_BriefLine("M1", GW_Brief_M1);
VZP_BriefLine("M5", GW_Brief_M5);
VZP_BriefLine("M15", GW_Brief_M15);
VZP_BriefLine("M30", GW_Brief_M30);
VZP_BriefLine("H1", GW_Brief_H1);
VZP_BriefLine("H4", GW_Brief_H4);
VZP_BriefLine("D1", GW_Brief_D1);
// ── last AI decision. One fixed line so the row count never changes and
// no stale objects are left behind when the text shortens. ──
VZP_Line(StringLen(Last_AI_Analysis) > 0 ? VZP_Clip(Last_AI_Analysis, 36) : "", VZP_DIM);
// Patch the background to the height the rows actually consumed.
ObjectSetInteger(0, VZP_BG_OBJ, OBJPROP_YSIZE, (g_vzp_y - g_panel_y) + 6);
// Sweep VZP_ objects that this pass did not draw: a layout change turns row N from a
// key/value pair into a text line (or vice versa) and the old objects would otherwise
// stay on the chart as duplicates (2026-09-18).
for(int i = ObjectsTotal(0, 0, -1) - 1; i >= 0; --i)
{
string n = ObjectName(0, i, 0, -1);
if(StringFind(n, VZP_PREFIX) != 0) continue;
bool drawn = false;
for(int k = 0; k < ArraySize(g_vzp_drawn); ++k) if(g_vzp_drawn[k] == n) { drawn = true; break; }
if(!drawn) ObjectDelete(0, n);
}
ChartRedraw(0);
}
// Called from the EA's OnChartEvent. Returns true when the event was ours.
bool PanelHandleEvent(const int id, const string &sparam)
{
if(id != CHARTEVENT_OBJECT_DRAG) return false;
if(sparam != VZP_BG_OBJ) return false;
g_panel_x = (int)ObjectGetInteger(0, VZP_BG_OBJ, OBJPROP_XDISTANCE);
g_panel_y = (int)ObjectGetInteger(0, VZP_BG_OBJ, OBJPROP_YDISTANCE);
// Drop the selection so the drag handles disappear once the move finishes.
ObjectSetInteger(0, VZP_BG_OBJ, OBJPROP_SELECTED, false);
PanelSavePos();
UpdateAILabel(true);
return true;
}
void CleanupAILabels()
{
for(int i = ObjectsTotal(0) - 1; i >= 0; --i)
{
string n = ObjectName(0, i);
if(StringFind(n, VZP_PREFIX) == 0)
ObjectDelete(0, n);
}
// Names used by the pre-panel implementation.
string legacy[] = {"AILabel", "AIStatsLabel", "AIBriefH1", "AIBriefH4",
"AIBriefD1", "AIAnalysisBox"};
for(int i = 0; i < ArraySize(legacy); ++i)
ObjectDelete(0, legacy[i]);
}
void DisplayAIBriefing(const string briefing)
{
Last_AI_Analysis = briefing;
Print("[AI BRIEFING] ", briefing);
UpdateAILabel(true);
}
void DisplayAIValidation(const string setup, const bool approved, const string reasoning)
{
Last_AI_Analysis = "GW " + setup + ": " + (approved ? "OK" : "NO") + " | " + reasoning;
UpdateAILabel(true);
}
#endif