RobotFX curates the best open-source MetaTrader code to inspire your trading automation.
//+------------------------------------------------------------------+ //| Matrix Manual.mq5 | //| Copyright 2026, MetaTrader User | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2026" #property link "https://www.mql5.com" #property version "1.08" #include <Trade\Trade.mqh> //--- Inputs input group "Trading Parameters" input double InpLotSize = 0.01; // Fixed Lot Size input ulong InpMagicNumber = 1; // Magic Number CTrade trade; // Virtual Key Codes for standard keyboards #define KEY_A 65 #define KEY_D 68 #define KEY_W 87 // Unique prefix for graphical objects to avoid conflicts string objPrefix = "MatrixPanel_"; // Forward declarations of functions used before their definition void DeleteMatrixPanel(); void CreateMatrixPanel(); void UpdateMatrixPanel(); void ApplyMatrixTheme(); void CloseAllPositions(); void CreateLabelObj(string name, int x, int y, int w, int h, color backColor, color borderColor, ENUM_BASE_CORNER corner); void CreateTextObj(string name, int x, int y, string text, color textCol, string font, int fontSize); //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { trade.SetExpertMagicNumber(InpMagicNumber); // Apply Matrix Chart Theme automatically ApplyMatrixTheme(); // Create Matrix Hacker Style Panel CreateMatrixPanel(); // Request a chart redraw to display the panel immediately ChartRedraw(); Print("Matrix Hotkey EA Initialized. Press A to Buy, D to Sell, W to Close All."); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Clean up graphical objects when EA is removed DeleteMatrixPanel(); Print("Matrix Hotkey EA Removed."); } //+------------------------------------------------------------------+ //| Expert tick function (Updates panel values live) | //+------------------------------------------------------------------+ void OnTick() { UpdateMatrixPanel(); } //+------------------------------------------------------------------+ //| Chart event function (Handles Hotkeys and UI interactions) | //+------------------------------------------------------------------+ void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { if(id == CHARTEVENT_KEYDOWN) { int keyCode = (int)lparam; // Hotkey: A -> BUY if(keyCode == KEY_A) { double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); if(trade.Buy(InpLotSize, _Symbol, ask, 0, 0, "Matrix Buy")) Print("Buy order executed successfully."); else Print("Buy order failed. Error: ", GetLastError()); UpdateMatrixPanel(); } // Hotkey: D -> SELL else if(keyCode == KEY_D) { double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); if(trade.Sell(InpLotSize, _Symbol, bid, 0, 0, "Matrix Sell")) Print("Sell order executed successfully."); else Print("Sell order failed. Error: ", GetLastError()); UpdateMatrixPanel(); } // Hotkey: W -> CLOSE ALL POSITIONS else if(keyCode == KEY_W) { CloseAllPositions(); UpdateMatrixPanel(); } } } //+------------------------------------------------------------------+ //| Function to close all open positions with matching Magic Number | //+------------------------------------------------------------------+ void CloseAllPositions() { int total = PositionsTotal(); for(int i = total - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(ticket > 0) { if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber) { trade.PositionClose(ticket); } } } Print("Close All executed for Magic Number: ", InpMagicNumber); } //+------------------------------------------------------------------+ //| Apply Matrix Theme to Chart Background and Candles | //+------------------------------------------------------------------+ void ApplyMatrixTheme() { // Background & Foreground ChartSetInteger(0, CHART_COLOR_BACKGROUND, clrBlack); // Pure Black background ChartSetInteger(0, CHART_COLOR_FOREGROUND, C'0,255,65'); // Axes, scales, and text in Matrix Green ChartSetInteger(0, CHART_COLOR_GRID, C'0,40,10'); // Subtle dark green grid lines // Candlesticks & Bars ChartSetInteger(0, CHART_COLOR_CHART_UP, C'0,255,65'); // Bullish candle outline/wick ChartSetInteger(0, CHART_COLOR_CHART_DOWN, C'0,100,30'); // Bearish candle outline/wick ChartSetInteger(0, CHART_COLOR_CANDLE_BULL, clrBlack); // Bullish candle body (hollow/black) ChartSetInteger(0, CHART_COLOR_CANDLE_BEAR, C'0,255,65'); // Bearish candle body (solid green) // Line charts & others ChartSetInteger(0, CHART_COLOR_CHART_LINE, C'0,255,65'); // Line chart color ChartSetInteger(0, CHART_COLOR_BID, C'0,255,65'); // Bid price line ChartSetInteger(0, CHART_COLOR_ASK, C'255,0,85'); // Ask price line (Matrix Crimson for contrast) } //+------------------------------------------------------------------+ //| Create Matrix Hacker Style Panel Objects (Centered Bar Layout) | //+------------------------------------------------------------------+ void CreateMatrixPanel() { DeleteMatrixPanel(); // Clear out any previous instances int w = 620; // Width of the horizontal bar int h = 45; // Height of the bar // Dynamically fetch chart width to center the bar perfectly long chartWidth = ChartGetInteger(0, CHART_WIDTH_IN_PIXELS); int x = (int)((chartWidth - w) / 2); if(x < 10) x = 10; // Fallback safety margin int y = 15; // Positioned neatly near the top center // 1. Background Bar (Dark Pitch Black with Text-Matching Green Border) CreateLabelObj("BG", x, y, w, h, clrBlack, C'0,180,50', CORNER_LEFT_UPPER); // 2. Matrix Outer Border Frame (Bright Neon Green Outline) CreateLabelObj("Border", x-2, y-2, w+4, h+4, clrNONE, C'0,255,65', CORNER_LEFT_UPPER); // 3. Layout elements horizontally inside the bar (Centered horizontally) // Terminal Label CreateTextObj("Title", x + 15, y + 14, "MATRIX //", C'0,180,50', "Consolas", 9); // Buy Stats Box & Texts (Centered inside dedicated green highlight frame) CreateLabelObj("Box_Buy", x + 90, y + 8, 55, 28, clrBlack, C'0,255,65', CORNER_LEFT_UPPER); CreateTextObj("Lbl_Buy", x + 96, y + 14, "B:", C'0,180,50', "Consolas", 9); CreateTextObj("Val_Buy", x + 114, y + 13, "0", C'0,255,65', "Consolas", 10); // Sell Stats Box & Texts (Centered inside dedicated green highlight frame) CreateLabelObj("Box_Sell", x + 155, y + 8, 55, 28, clrBlack, C'0,255,65', CORNER_LEFT_UPPER); CreateTextObj("Lbl_Sell", x + 161, y + 14, "S:", C'0,180,50', "Consolas", 9); CreateTextObj("Val_Sell", x + 179, y + 13, "0", C'0,255,65', "Consolas", 10); // Floating P/L Stats Box & Texts (Centered inside dedicated green highlight frame) CreateLabelObj("Box_PL", x + 220, y + 8, 95, 28, clrBlack, C'0,255,65', CORNER_LEFT_UPPER); CreateTextObj("Lbl_PL", x + 228, y + 14, "P/L:", C'0,180,50', "Consolas", 9); CreateTextObj("Val_PL", x + 258, y + 13, "0.00", C'0,255,65', "Consolas", 10); // Hotkey Legend Reminder CreateTextObj("Hotkeys", x + 335, y + 14, "[A]BUY [D]SELL [W]CLOSE", C'0,120,40', "Consolas", 8); } //+------------------------------------------------------------------+ //| Update Matrix Panel values dynamically | //+------------------------------------------------------------------+ void UpdateMatrixPanel() { int buyCount = 0; int sellCount = 0; double totalPL = 0.0; int total = PositionsTotal(); for(int i = 0; i < total; i++) { ulong ticket = PositionGetTicket(i); if(ticket > 0) { if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == InpMagicNumber) { long type = PositionGetInteger(POSITION_TYPE); double profit = PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP); totalPL += profit; if(type == POSITION_TYPE_BUY) buyCount++; else if(type == POSITION_TYPE_SELL) sellCount++; } } } // Update values on screen ObjectSetString(0, objPrefix + "Val_Buy", OBJPROP_TEXT, IntegerToString(buyCount)); ObjectSetString(0, objPrefix + "Val_Sell", OBJPROP_TEXT, IntegerToString(sellCount)); string plStr = DoubleToString(totalPL, 2); if(totalPL > 0) plStr = "+" + plStr; ObjectSetString(0, objPrefix + "Val_PL", OBJPROP_TEXT, plStr); // Hacker Style Color Feedback for P/L (Bright Green for Profit, Red for Loss) color plColor = C'0,255,65'; // Neon Green if(totalPL < 0) plColor = C'255,0,85'; // Matrix Crimson/Red else if(totalPL == 0) plColor = C'0,200,80'; ObjectSetInteger(0, objPrefix + "Val_PL", OBJPROP_COLOR, plColor); ChartRedraw(); } //+------------------------------------------------------------------+ //| Delete all panel objects from chart | //+------------------------------------------------------------------+ void DeleteMatrixPanel() { ObjectsDeleteAll(0, objPrefix); } //+------------------------------------------------------------------+ //| Helper: Create Rectangle / Background Object | //+------------------------------------------------------------------+ void CreateLabelObj(string name, int x, int y, int w, int h, color backColor, color borderColor, ENUM_BASE_CORNER corner) { string objName = objPrefix + name; ObjectCreate(0, objName, OBJ_RECTANGLE_LABEL, 0, 0, 0); ObjectSetInteger(0, objName, OBJPROP_XDISTANCE, x); ObjectSetInteger(0, objName, OBJPROP_YDISTANCE, y); ObjectSetInteger(0, objName, OBJPROP_XSIZE, w); ObjectSetInteger(0, objName, OBJPROP_YSIZE, h); ObjectSetInteger(0, objName, OBJPROP_BGCOLOR, backColor); ObjectSetInteger(0, objName, OBJPROP_BORDER_COLOR, borderColor); ObjectSetInteger(0, objName, OBJPROP_BORDER_TYPE, BORDER_FLAT); ObjectSetInteger(0, objName, OBJPROP_CORNER, corner); ObjectSetInteger(0, objName, OBJPROP_BACK, false); ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false); } //+------------------------------------------------------------------+ //| Helper: Create Text Label Object | //+------------------------------------------------------------------+ void CreateTextObj(string name, int x, int y, string text, color textCol, string font, int fontSize) { string objName = objPrefix + name; ObjectCreate(0, objName, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, objName, OBJPROP_XDISTANCE, x); ObjectSetInteger(0, objName, OBJPROP_YDISTANCE, y); ObjectSetString(0, objName, OBJPROP_TEXT, text); ObjectSetString(0, objName, OBJPROP_FONT, font); ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, fontSize); ObjectSetInteger(0, objName, OBJPROP_COLOR, textCol); ObjectSetInteger(0, objName, OBJPROP_CORNER, CORNER_LEFT_UPPER); ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false); ObjectSetInteger(0, objName, OBJPROP_BACK, false); }
Protect profits effectively with the smart Trailing Stop Expert Advisor. Advanced trailing options for MT4/MT5. See it in action.
Build better strategies with RobotFX professional tools – check them out.
75367