Parcourir la source

Ticket: 5360 Master Trade management, dashboard UI

Introduces the initial implementation of localHedgingMasterCopierMt5 for MetaTrader 5, including trade management, dashboard UI, and basic buy/sell trade placement logic. Adds both the source (.mq5) and compiled (.ex5) files.
Huzaifa-MQLDev il y a 7 mois
Parent
commit
d7a70b07b8
2 fichiers modifiés avec 470 ajouts et 0 suppressions
  1. BIN
      master/localHedgingMasterCopierMt5.ex5
  2. 470 0
      master/localHedgingMasterCopierMt5.mq5

BIN
master/localHedgingMasterCopierMt5.ex5


+ 470 - 0
master/localHedgingMasterCopierMt5.mq5

@@ -0,0 +1,470 @@
+//+------------------------------------------------------------------+
+//|                                  localHedgingMasterCopierMt5.mq5 |
+//|                                  Copyright 2025, MQL Development |
+//|                                  https://www.mqldevelopment.com/ |
+//+------------------------------------------------------------------+
+#property copyright "Copyright 2025, MQL Development"
+#property link      "https://www.mqldevelopment.com/"
+#property version   "1.00"
+#include <Trade\Trade.mqh>
+CTrade  trade;
+#define orderCount 2000
+#define count1 50
+#define testCounter 50
+#define MaxOrders 20000
+#define daysOfTradesToAdd 20
+
+#property description "Control Panels and Dialogs. Demonstration class CLabel"
+#include <Controls\Dialog.mqh>
+#include <Controls\Label.mqh>
+#include <Controls\Button.mqh>
+#include <Controls\RadioButton.mqh>
+#include <Controls\RadioGroup.mqh>
+#include <Controls\CheckBox.mqh>
+#include  <Controls\ComboBox.mqh>
+//+------------------------------------------------------------------+
+//| Expert initialization function                                   |
+//+------------------------------------------------------------------+
+struct historyTradesClientSide
+  {
+   ulong             ticket;
+   ulong             magic;
+                     historyTradesClientSide()
+     {
+      ticket = -1;
+      magic  = 0;
+     }
+  };
+historyTradesClientSide historyTradeStore[MaxOrders];
+
+struct masterOrder
+  {
+   datetime          opentime;
+   string            symbol;
+   ulong             ordertype;
+   double            price;
+   double            lots;
+   double            stoploss;
+   double            takeprofit;
+   ulong             ticket;
+
+   int               magic;
+   double            accountBalance;
+   int               contractSize;
+   string            comment;
+
+
+                     masterOrder()
+     {
+      ticket=-1;
+      price=-1;
+      symbol=Symbol();
+      ordertype=-1;
+      lots=0;
+      stoploss=0;
+      takeprofit=0;
+     }
+  };
+masterOrder mod1[orderCount];
+
+sinput       string                  string_0                  = "<><><><><><> General SETTINGS <><><><><><>";   //__
+input        int                     magic_no                  = 333;             // Magic no
+input        string                  suffix                    = "";              // Suffix
+input        string                  prefix                    = "";              // Prefix
+input        bool                    useMinimumLot             = true;            // Use Minimum Lot Threshold
+input        double                  takeprofit                = 10;              // Fixed Take Profit in Pips
+input        double                  stoploss                  = 10;              // Fixed Stop Loss in Pips
+input        double                  inpPips                   = 0.2;             // Pips To Start Copy
+input        double                  minimumLot                = 0.01;            // Minimum Lot
+input        bool                    useMaximumLot             = true;            // Use Maximum Lot Threshold
+input        double                  maximumLot                = 0.01;            // Maximum Lot
+
+// Heart Beat
+sinput       string                  string_1                  = "<><><><><><> HeartBeat Setting <><><><><><>";   //__
+input        string                  slaveServer               = "";              // Slave Server
+input        string                  slaveId                   = "";              // Slave Login ID
+input        string                  masterServer              = "";              // Master Server
+input        string                  slaveVersion              = "4";             // Slave Version
+input        string                  localApiKey               = "U2FsdGVkX18vBY4H1uzQiZwuh8B++8VBtCGJ3yGr2XPII0qCodmfuhjssKu5oug1J4e97bkwPtDQLi4py1OODQ=="; // Local API Key
+input        string                  localHB_req_link          = "http://localhost/api/mlastConnected"; // Request Link For LastConnected
+
+input        string                  dashboardSettings         = "<><><><><><> Dashboard Settings<><><><><><>";
+input        int                     DashHeight                = 115;             // Enter Height of Dashboard
+input        int                     DashWidth                 = 490;             // Enter Width  of Dashboard
+input        int                     FontSize                  = 10;              // Text Font Size
+
+// Global Variables
+int filehandle;
+int gmtDifference = 0;
+bool doReversal = true; // Reverse Trades
+bool auth = false;
+datetime last_check_expiry;
+ENUM_TIMEFRAMES check_expiry_timeframe = PERIOD_H1;
+int orderTotal=0;
+
+bool fileLastEmpty=false;
+bool fileHandlerIssue=false;
+int counter = 0, testCounterVar = 0;
+
+int yAxisGlobal = 5;
+int  dashHeight;                 //  Height of Dashboard
+int  dashWidth;                  //  Width  of Dashboard
+int OnInit()
+  {
+//---
+   trade.SetExpertMagicNumber(magic_no);
+   trade.SetDeviationInPoints(10);
+   trade.SetTypeFilling(ORDER_FILLING_IOC);
+   trade.LogLevel(LOG_LEVEL_ALL);
+   trade.SetAsyncMode(false);
+
+   dashHeight = DashHeight;
+   dashWidth  = DashWidth;
+   double width  = double(ChartGetInteger(0,CHART_WIDTH_IN_PIXELS));
+   double height = double(ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS));
+
+   double minHeight = (1.0/height) * (height);
+   double minWidth  = (400.0/width) * (width);
+
+   Print(" Minimum Required minHeight = ",minHeight," minWidth ",minWidth);
+
+   if(dashWidth < minWidth)
+     {
+      dashWidth = (int)minWidth;
+      Print(" Dashboard width is updated because user input is less than required width ");
+     }
+   if(dashHeight < minHeight)
+     {
+      dashHeight = (int)minHeight;
+      Print(" Dashboard Height is updated because user input is less than required Height ");
+     }
+
+   if(!ExtDialog.Create(0,"Local Hedge Master",10,10,40,dashWidth,dashHeight))      // the panel position
+      return(INIT_FAILED);
+   ExtDialog.Run();
+
+   EventSetMillisecondTimer(500);
+//---
+   return(INIT_SUCCEEDED);
+  }
+//+------------------------------------------------------------------+
+//| Expert deinitialization function                                 |
+//+------------------------------------------------------------------+
+void OnDeinit(const int reason)
+  {
+//---
+   ExtDialog.Destroy(reason);
+   EventKillTimer();
+  }
+//+------------------------------------------------------------------+
+//| Expert tick function                                             |
+//+------------------------------------------------------------------+
+void OnTimer()
+  {
+
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void OnTick()
+  {
+//---
+
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void OnChartEvent(const int id,
+                  const long &lparam,
+                  const double &dparam,
+                  const string &sparam)
+  {
+//---
+   ExtDialog.ChartEvent(id,lparam,dparam,sparam);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void placeBuyTrade()
+  {
+
+   double buySL = 0, buyTp=0;
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+
+   if(stoploss != 0)
+     {
+      buySL = Ask - (stoploss * 10 * Point());
+     }
+   if(takeprofit != 0)
+     {
+      buyTp = Ask + (takeprofit * 10 * Point());
+     }
+
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,1,Ask,buySL,buyTp,"Buy Trade Placed"))
+     {
+      Print("Buy Trade Placed: ",trade.ResultOrder());
+     }
+   else
+     {
+      Print("Error in placing Buy: "+Symbol()+"  ",GetLastError());
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void placeSellTrade()
+  {
+
+   double sellSL = 0, sellTp = 0;
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
+
+   if(stoploss != 0)
+     {
+      sellSL = Bid + (stoploss * 10 * Point());
+     }
+   if(takeprofit != 0)
+     {
+      sellTp = Bid - (takeprofit * 10 * Point());
+     }
+
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,1,Bid,sellSL,sellTp,"Sell Trade Placed"))
+     {
+      Print("Sell Trade PLaced: ",trade.ResultOrder());
+     }
+   else
+     {
+      Print("Error in placing Sell: "+Symbol()+"  ",GetLastError());
+     }
+
+  }
+//+------------------------------------------------------------------+
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+//================================================ Dashboard ====================================
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+class CControlsDialog : public CAppDialog
+  {
+public:
+
+   CLabel            m_label0;
+
+   CButton           m_button;
+   CButton           m_button1;
+
+                     CControlsDialog(void);
+                    ~CControlsDialog(void);
+   //--- create
+   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
+   virtual bool      OnEvent(const int id, const long &lparam,const double &dparam, const string &sparam);
+
+   bool              CreateLabel0(void);
+
+   bool              CreateButton(void);
+   bool              CreateButton1(void);
+
+   void              OnClickButton(void);
+   void              OnClickButton1(void);
+
+   bool              CreatePanel(void);
+
+  };
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+EVENT_MAP_BEGIN(CControlsDialog)
+ON_EVENT(ON_CLICK,m_button,OnClickButton)
+ON_EVENT(ON_CLICK,m_button1,OnClickButton1)
+EVENT_MAP_END(CAppDialog)
+//+------------------------------------------------------------------+
+//| Constructor                                                      |
+//+------------------------------------------------------------------+
+CControlsDialog::CControlsDialog(void)
+  {
+  }
+//+------------------------------------------------------------------+
+//| Destructor                                                       |
+//+------------------------------------------------------------------+
+CControlsDialog::~CControlsDialog(void)
+  {
+  }
+
+//+------------------------------------------------------------------+
+//| Create                                                           |
+//+------------------------------------------------------------------+
+bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
+  {
+   if(!CAppDialog::Create(chart,name,subwin,20,20,dashWidth,dashHeight))
+      return(false);
+//--- create dependent controls
+
+   if(!CreatePanel())
+      return(false);
+
+   if(!CreateLabel0())
+      return(false);
+
+   if(!CreateButton())
+      return(false);
+   if(!CreateButton1())
+      return(false);
+
+//--- succeed
+   return(true);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+bool CControlsDialog::CreateLabel0(void)  //Trend Info
+  {
+//--- coordinates
+   int x1 = (dashWidth/2) - 235;
+   int y1 = yAxisGlobal + 15;
+   int x2 = 0;
+   int y2 = 0;
+//--- create
+   if(!m_label0.Create(m_chart_id,m_name+"Label0",m_subwin,x1,y1,x2,y2))
+      return(false);
+   if(!m_label0.Text(" Master Trades Lots Calc: 0.00"))
+      return(false);
+   if(!m_label0.Color(clrBlack))
+      return(false);
+   if(!m_label0.Font("Calibri"))
+      return(false);
+   if(!m_label0.FontSize(FontSize))
+      return(false);
+   if(!Add(m_label0))
+      return(false);
+//--- succeed
+   return(true);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+bool CControlsDialog::CreateButton(void)
+  {
+//--- coordinates
+// Buy
+   int x1 = (dashWidth/2);
+   int y1 = yAxisGlobal + 10;
+   int x2 = x1 + 70;
+   int y2 = y1 + 30;
+//--- create
+   if(!m_button.Create(m_chart_id,"button",m_subwin,x1,y1,x2,y2))
+      return(false);
+   if(!m_button.ColorBackground(clrSlateGray))
+      return(false);
+   if(!m_button.Text("Sell"))
+      return(false);
+   if(!m_button.FontSize(FontSize))
+      return(false);
+   if(!m_button.ColorBackground(clrRed))
+      return(false);
+   if(!m_button.Font("Calibri Bold"))
+      return(false);
+   if(!m_button.Color(clrWhiteSmoke))
+      return(false);
+   if(!m_button.ColorBorder(clrRed))
+      return(false);
+   if(!Add(m_button))
+      return(false);
+   m_button.Locking(true);
+   m_button.BringToTop();
+//--- succeed
+   return(true);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+bool CControlsDialog::CreateButton1(void)
+  {
+//--- coordinates
+// Sell
+   int x1 = (dashWidth/2) + 84;
+   int y1 = yAxisGlobal + 10;
+   int x2 = x1 + 70;
+   int y2 = y1 + 30;
+//--- create
+   if(!m_button1.Create(m_chart_id,"button1",m_subwin,x1,y1,x2,y2))
+      return(false);
+   if(!m_button1.ColorBackground(clrBlue))
+      return(false);
+   if(!m_button1.Text("Buy"))
+      return(false);
+   if(!m_button1.FontSize(FontSize))
+      return(false);
+   if(!m_button1.Font("Calibri Bold"))
+      return(false);
+   if(!m_button1.Color(clrWhiteSmoke))
+      return(false);
+   if(!m_button1.ColorBorder(clrBlack))
+      return(false);
+   if(!Add(m_button1))
+      return(false);
+   m_button1.Locking(true);
+   m_button1.BringToTop();
+//--- succeed
+   return(true);
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void CControlsDialog::OnClickButton(void)
+  {
+   if(m_button.Pressed())
+     {
+      Print(" Clicked on Button");
+      m_button.Pressed(false);
+      placeSellTrade();
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+void CControlsDialog::OnClickButton1(void)
+  {
+   if(m_button1.Pressed())
+     {
+      Print(" Clicked on Button 1");
+      m_button1.Pressed(false);
+      placeBuyTrade();
+     }
+  }
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+
+bool CControlsDialog::CreatePanel(void)
+  {
+//--- coordinates
+   int x1=0;
+   int y1=0;
+   int x2=dashWidth;
+   int y2=dashHeight;
+//--- create
+   if(!backgroundPanel.Create(0,ExtDialog.Name()+"backPanel",m_subwin,x1,y1,x2,y2))
+      return(false);
+   if(!backgroundPanel.ColorBackground(clrWhiteSmoke))
+      return(false);
+   if(!backgroundPanel.ColorBorder(00128))
+      return(false);
+   if(!ExtDialog.Add(backgroundPanel))
+      return(false);
+   ExtDialog.Minimized(false);
+
+
+   backgroundPanel.Alignment(WND_ALIGN_CLIENT,0,0,0,0);
+//--- succeed
+   return(true);
+  }
+
+CControlsDialog ExtDialog;
+CPanel   backgroundPanel;
+//+------------------------------------------------------------------+
+//|                                                                  |
+//+------------------------------------------------------------------+