DearMQL5/Examples/EA_DearMQL5_Demo.mq5

145 lines
5.1 KiB
MQL5

feat: initial release of DearMQL5 v1.0.0 - Immediate-Mode GUI Engine for MetaTrader 5 DearMQL5 is an institutional-grade, immediate-mode graphical user interface (GUI) engine designed from the ground up for MetaTrader 5 (MQL5), inspired by the ergonomics and performance of Dear ImGui. ### Core Architecture & Execution Pipeline - Immediate-Mode Paradigm: Declarative frame-based rendering pipeline with zero memory allocation churn during render loops and deterministic execution. - Modular Per-Hub Canvas Surfaces (CUWindowSurface): Replaces full-screen canvas overlays with isolated, window-bounded CCanvas bitmaps, reducing memory usage by up to 93% (~570 KB vs 8.3 MB for 1080p) and preventing click-swallowing. - 0-Cost Hardware Texture Drag Engine: Absolute-anchor drag mechanics update native MT5 OBJPROP_XDISTANCE and OBJPROP_YDISTANCE properties directly on the compositor, bypassing CPU rasterization completely (0.00% CPU usage). - Chart Scroll Protection: Automatically suppresses chart history sliding (CHART_MOUSE_SCROLL) during active HUD dragging and safely restores it on release. - Two-Phase Hit-Testing: Interactive controls (buttons, sliders, toggles) receive first-priority click focus, cleanly separating widget actions from window drags. ### Subsystems & Components - Core State & Input (Core/): * UInputState: Mouse/keyboard tracking, deltas, and chart scroll lock. * UState: Retained state hash map in pure MQL5 with 64-bit FNV-1a hashing. * UIdStack: Hierarchical identifier scoping preventing widget ID collisions. * ULayout: Automatic cursor placement supporting SameLine, Indent, and Unindent. * UEventQueue: Non-blocking circular UI event queue for decoupled handling. * UTheme: Institutional OneDark/Nord dark color palette and typography metrics. - Rendering Backends (Render/): * URendererCanvas: Isolated per-hub surfaces with 32-bit ARGB alpha transparency. * URendererNative: Native MT5 object pool (OBJ_RECTANGLE_LABEL) with diff caching. * UFont: Anti-aliased font metrics and width cache. - Complete Widget Suite (Widgets/): * UButton: Click-on-release standard, accent, and emergency panic buttons. * UToggle: Smooth draggable/clickable iOS-style toggle switches. * USlider: Draggable horizontal float and integer sliders. * UProgressBar: Adaptive progress bars with dynamic green->amber->red color ramps. * UHeader: Collapsible section headers with immediate-mode state retention. * ULabel & USeparator: Formatted text, colored metrics, and horizontal dividers. - Window Management (Windows/): * UWindow: Window container with titlebar, collapse toggle, and coordinate clipping. * UWindowManager: Multi-window coordinator with GlobalVariable persistence across chart timeframe changes and backward-compatible key resolution. ### Tooling, Documentation & Reference Implementation - Examples/EA_DearMQL5_Demo.mq5: Complete reference Prop-Firm cockpit HUD. - README.md: Comprehensive institutional English documentation for GitHub and Algo Forge. - Docs/ARCHITECTURE.md: Technical architecture specification with Mermaid lifecycle.
2026-10-12 08:18:03 -03:00
//+------------------------------------------------------------------+
//| EA_DearMQL5_Demo.mq5 |
//| Copyright 2026, DearMQL5 GUI Engine |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, DearMQL5 GUI Engine"
#property link "https://www.mql5.com"
#property version "1.00"
#include "..\UI.mqh"
// State variables for Demo GUI
bool g_ui_open = true;
bool g_auto_trade = true;
bool g_news_filter = true;
bool g_trailing_active = false;
double g_risk_pct = 1.0;
double g_max_slippage_pts = 20.0;
int g_daily_loss_limit = 500;
int g_current_daily_loss = 160;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Enable mouse movement messages on chart
ChartSetInteger(0, CHART_EVENT_MOUSE_MOVE, true);
// Initialize DearMQL5 Canvas GUI
if(!UI::Init(0, 0))
{
Print("[DearMQL5] Falha ao inicializar o Canvas da GUI.");
return(INIT_FAILED);
}
Print("[DearMQL5] Inicializado com sucesso.");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
UI::Shutdown();
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
// Feed input events into DearMQL5 pipeline
UI::OnChartEvent(id, lparam, dparam, sparam);
// Re-render frame immediately on mouse interaction for zero-latency response
if(id == CHARTEVENT_MOUSE_MOVE || id == CHARTEVENT_CLICK)
{
RenderGUI();
}
}
//+------------------------------------------------------------------+
//| OnTick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Update HUD on incoming market ticks
RenderGUI();
}
//+------------------------------------------------------------------+
//| Renders the immediate-mode HUD |
//+------------------------------------------------------------------+
void RenderGUI()
{
UI::NewFrame();
if(UI::Begin("PROP FIRM TERMINAL — DearMQL5", g_ui_open, 20, 30, 280, 420))
{
// --- Section 1: Prop Firm Account Metrics ---
double equity = AccountInfoDouble(ACCOUNT_EQUITY);
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double pnl = AccountInfoDouble(ACCOUNT_PROFIT);
UI::Text(StringFormat("Balance: $%.2f", balance));
UI::SameLine();
UI::Text(StringFormat("Equity: $%.2f", equity));
uint pnl_color = (pnl >= 0) ? 0xFF98C379 : 0xFFE06C75;
UI::TextColored(StringFormat("Floating PnL: $%.2f", pnl), pnl_color);
// Daily Drawdown Progress Bar
double dd_fraction = (double)g_current_daily_loss / (double)g_daily_loss_limit;
string dd_overlay = StringFormat("Daily Loss: $%d / $%d (%.0f%%)", g_current_daily_loss, g_daily_loss_limit, dd_fraction * 100.0);
UI::ProgressBar(dd_fraction, 0, 16, dd_overlay, true);
UI::Separator();
// --- Section 2: Trading Actions ---
UI::Text("Execução Rápida:");
if(UI::ButtonColor("BUY MARKET", 0xFF00A389, 126, 26))
{
Print(">> BOTÃO CLICADO: BUY MARKET");
}
UI::SameLine();
if(UI::ButtonColor("SELL MARKET", 0xFFD19A66, 126, 26))
{
Print(">> BOTÃO CLICADO: SELL MARKET");
}
if(UI::ButtonColor("PANIC CLOSE ALL", 0xFFE06C75, 260, 26))
{
Print(">> ALERTA: PANIC CLOSE ALL PRESSIONADO!");
}
UI::Separator();
// --- Section 3: Risk Controls ---
UI::Text("Controles de Risco & Slippage:");
UI::SliderFloat("Risco %", g_risk_pct, 0.25, 3.0, "%.2f%%");
UI::SliderFloat("Max Slip", g_max_slippage_pts, 5.0, 50.0, "%.0f pts");
UI::Toggle("Auto-Trading Ativo", g_auto_trade);
UI::Toggle("Filtro de Notícias", g_news_filter);
UI::Toggle("Trailing Stop Dinâmico", g_trailing_active);
// --- Section 4: Diagnostics Header ---
if(UI::CollapsingHeader("Diagnóstico do Motor GUI", false))
{
UI::Text(StringFormat("Tempo de Render: %.3f ms", UDiagnostics::GetLastFrameMs()));
UI::Text(StringFormat("Draw Calls: %u", UDiagnostics::GetDrawCallsCount()));
UI::Text(StringFormat("Mouse: (%d, %d)", UInputState::MouseX(), UInputState::MouseY()));
}
}
UI::End();
UI::Render();
}