Procházet zdrojové kódy

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 před 1 měsícem
rodič
revize
d7a70b07b8

binární
master/localHedgingMasterCopierMt5.ex5


+ 470 - 0
master/localHedgingMasterCopierMt5.mq5

@@ -0,0 +1,470 @@
1
+//+------------------------------------------------------------------+
2
+//|                                  localHedgingMasterCopierMt5.mq5 |
3
+//|                                  Copyright 2025, MQL Development |
4
+//|                                  https://www.mqldevelopment.com/ |
5
+//+------------------------------------------------------------------+
6
+#property copyright "Copyright 2025, MQL Development"
7
+#property link      "https://www.mqldevelopment.com/"
8
+#property version   "1.00"
9
+#include <Trade\Trade.mqh>
10
+CTrade  trade;
11
+#define orderCount 2000
12
+#define count1 50
13
+#define testCounter 50
14
+#define MaxOrders 20000
15
+#define daysOfTradesToAdd 20
16
+
17
+#property description "Control Panels and Dialogs. Demonstration class CLabel"
18
+#include <Controls\Dialog.mqh>
19
+#include <Controls\Label.mqh>
20
+#include <Controls\Button.mqh>
21
+#include <Controls\RadioButton.mqh>
22
+#include <Controls\RadioGroup.mqh>
23
+#include <Controls\CheckBox.mqh>
24
+#include  <Controls\ComboBox.mqh>
25
+//+------------------------------------------------------------------+
26
+//| Expert initialization function                                   |
27
+//+------------------------------------------------------------------+
28
+struct historyTradesClientSide
29
+  {
30
+   ulong             ticket;
31
+   ulong             magic;
32
+                     historyTradesClientSide()
33
+     {
34
+      ticket = -1;
35
+      magic  = 0;
36
+     }
37
+  };
38
+historyTradesClientSide historyTradeStore[MaxOrders];
39
+
40
+struct masterOrder
41
+  {
42
+   datetime          opentime;
43
+   string            symbol;
44
+   ulong             ordertype;
45
+   double            price;
46
+   double            lots;
47
+   double            stoploss;
48
+   double            takeprofit;
49
+   ulong             ticket;
50
+
51
+   int               magic;
52
+   double            accountBalance;
53
+   int               contractSize;
54
+   string            comment;
55
+
56
+
57
+                     masterOrder()
58
+     {
59
+      ticket=-1;
60
+      price=-1;
61
+      symbol=Symbol();
62
+      ordertype=-1;
63
+      lots=0;
64
+      stoploss=0;
65
+      takeprofit=0;
66
+     }
67
+  };
68
+masterOrder mod1[orderCount];
69
+
70
+sinput       string                  string_0                  = "<><><><><><> General SETTINGS <><><><><><>";   //__
71
+input        int                     magic_no                  = 333;             // Magic no
72
+input        string                  suffix                    = "";              // Suffix
73
+input        string                  prefix                    = "";              // Prefix
74
+input        bool                    useMinimumLot             = true;            // Use Minimum Lot Threshold
75
+input        double                  takeprofit                = 10;              // Fixed Take Profit in Pips
76
+input        double                  stoploss                  = 10;              // Fixed Stop Loss in Pips
77
+input        double                  inpPips                   = 0.2;             // Pips To Start Copy
78
+input        double                  minimumLot                = 0.01;            // Minimum Lot
79
+input        bool                    useMaximumLot             = true;            // Use Maximum Lot Threshold
80
+input        double                  maximumLot                = 0.01;            // Maximum Lot
81
+
82
+// Heart Beat
83
+sinput       string                  string_1                  = "<><><><><><> HeartBeat Setting <><><><><><>";   //__
84
+input        string                  slaveServer               = "";              // Slave Server
85
+input        string                  slaveId                   = "";              // Slave Login ID
86
+input        string                  masterServer              = "";              // Master Server
87
+input        string                  slaveVersion              = "4";             // Slave Version
88
+input        string                  localApiKey               = "U2FsdGVkX18vBY4H1uzQiZwuh8B++8VBtCGJ3yGr2XPII0qCodmfuhjssKu5oug1J4e97bkwPtDQLi4py1OODQ=="; // Local API Key
89
+input        string                  localHB_req_link          = "http://localhost/api/mlastConnected"; // Request Link For LastConnected
90
+
91
+input        string                  dashboardSettings         = "<><><><><><> Dashboard Settings<><><><><><>";
92
+input        int                     DashHeight                = 115;             // Enter Height of Dashboard
93
+input        int                     DashWidth                 = 490;             // Enter Width  of Dashboard
94
+input        int                     FontSize                  = 10;              // Text Font Size
95
+
96
+// Global Variables
97
+int filehandle;
98
+int gmtDifference = 0;
99
+bool doReversal = true; // Reverse Trades
100
+bool auth = false;
101
+datetime last_check_expiry;
102
+ENUM_TIMEFRAMES check_expiry_timeframe = PERIOD_H1;
103
+int orderTotal=0;
104
+
105
+bool fileLastEmpty=false;
106
+bool fileHandlerIssue=false;
107
+int counter = 0, testCounterVar = 0;
108
+
109
+int yAxisGlobal = 5;
110
+int  dashHeight;                 //  Height of Dashboard
111
+int  dashWidth;                  //  Width  of Dashboard
112
+int OnInit()
113
+  {
114
+//---
115
+   trade.SetExpertMagicNumber(magic_no);
116
+   trade.SetDeviationInPoints(10);
117
+   trade.SetTypeFilling(ORDER_FILLING_IOC);
118
+   trade.LogLevel(LOG_LEVEL_ALL);
119
+   trade.SetAsyncMode(false);
120
+
121
+   dashHeight = DashHeight;
122
+   dashWidth  = DashWidth;
123
+   double width  = double(ChartGetInteger(0,CHART_WIDTH_IN_PIXELS));
124
+   double height = double(ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS));
125
+
126
+   double minHeight = (1.0/height) * (height);
127
+   double minWidth  = (400.0/width) * (width);
128
+
129
+   Print(" Minimum Required minHeight = ",minHeight," minWidth ",minWidth);
130
+
131
+   if(dashWidth < minWidth)
132
+     {
133
+      dashWidth = (int)minWidth;
134
+      Print(" Dashboard width is updated because user input is less than required width ");
135
+     }
136
+   if(dashHeight < minHeight)
137
+     {
138
+      dashHeight = (int)minHeight;
139
+      Print(" Dashboard Height is updated because user input is less than required Height ");
140
+     }
141
+
142
+   if(!ExtDialog.Create(0,"Local Hedge Master",10,10,40,dashWidth,dashHeight))      // the panel position
143
+      return(INIT_FAILED);
144
+   ExtDialog.Run();
145
+
146
+   EventSetMillisecondTimer(500);
147
+//---
148
+   return(INIT_SUCCEEDED);
149
+  }
150
+//+------------------------------------------------------------------+
151
+//| Expert deinitialization function                                 |
152
+//+------------------------------------------------------------------+
153
+void OnDeinit(const int reason)
154
+  {
155
+//---
156
+   ExtDialog.Destroy(reason);
157
+   EventKillTimer();
158
+  }
159
+//+------------------------------------------------------------------+
160
+//| Expert tick function                                             |
161
+//+------------------------------------------------------------------+
162
+void OnTimer()
163
+  {
164
+
165
+  }
166
+//+------------------------------------------------------------------+
167
+//|                                                                  |
168
+//+------------------------------------------------------------------+
169
+void OnTick()
170
+  {
171
+//---
172
+
173
+  }
174
+//+------------------------------------------------------------------+
175
+//|                                                                  |
176
+//+------------------------------------------------------------------+
177
+void OnChartEvent(const int id,
178
+                  const long &lparam,
179
+                  const double &dparam,
180
+                  const string &sparam)
181
+  {
182
+//---
183
+   ExtDialog.ChartEvent(id,lparam,dparam,sparam);
184
+  }
185
+//+------------------------------------------------------------------+
186
+//|                                                                  |
187
+//+------------------------------------------------------------------+
188
+void placeBuyTrade()
189
+  {
190
+
191
+   double buySL = 0, buyTp=0;
192
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
193
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
194
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
195
+
196
+   if(stoploss != 0)
197
+     {
198
+      buySL = Ask - (stoploss * 10 * Point());
199
+     }
200
+   if(takeprofit != 0)
201
+     {
202
+      buyTp = Ask + (takeprofit * 10 * Point());
203
+     }
204
+
205
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,1,Ask,buySL,buyTp,"Buy Trade Placed"))
206
+     {
207
+      Print("Buy Trade Placed: ",trade.ResultOrder());
208
+     }
209
+   else
210
+     {
211
+      Print("Error in placing Buy: "+Symbol()+"  ",GetLastError());
212
+     }
213
+  }
214
+//+------------------------------------------------------------------+
215
+//|                                                                  |
216
+//+------------------------------------------------------------------+
217
+void placeSellTrade()
218
+  {
219
+
220
+   double sellSL = 0, sellTp = 0;
221
+//openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
222
+   double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
223
+   double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
224
+
225
+   if(stoploss != 0)
226
+     {
227
+      sellSL = Bid + (stoploss * 10 * Point());
228
+     }
229
+   if(takeprofit != 0)
230
+     {
231
+      sellTp = Bid - (takeprofit * 10 * Point());
232
+     }
233
+
234
+   if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,1,Bid,sellSL,sellTp,"Sell Trade Placed"))
235
+     {
236
+      Print("Sell Trade PLaced: ",trade.ResultOrder());
237
+     }
238
+   else
239
+     {
240
+      Print("Error in placing Sell: "+Symbol()+"  ",GetLastError());
241
+     }
242
+
243
+  }
244
+//+------------------------------------------------------------------+
245
+//+------------------------------------------------------------------+
246
+//|                                                                  |
247
+//+------------------------------------------------------------------+
248
+//================================================ Dashboard ====================================
249
+//+------------------------------------------------------------------+
250
+//|                                                                  |
251
+//+------------------------------------------------------------------+
252
+class CControlsDialog : public CAppDialog
253
+  {
254
+public:
255
+
256
+   CLabel            m_label0;
257
+
258
+   CButton           m_button;
259
+   CButton           m_button1;
260
+
261
+                     CControlsDialog(void);
262
+                    ~CControlsDialog(void);
263
+   //--- create
264
+   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
265
+   virtual bool      OnEvent(const int id, const long &lparam,const double &dparam, const string &sparam);
266
+
267
+   bool              CreateLabel0(void);
268
+
269
+   bool              CreateButton(void);
270
+   bool              CreateButton1(void);
271
+
272
+   void              OnClickButton(void);
273
+   void              OnClickButton1(void);
274
+
275
+   bool              CreatePanel(void);
276
+
277
+  };
278
+//+------------------------------------------------------------------+
279
+//|                                                                  |
280
+//+------------------------------------------------------------------+
281
+EVENT_MAP_BEGIN(CControlsDialog)
282
+ON_EVENT(ON_CLICK,m_button,OnClickButton)
283
+ON_EVENT(ON_CLICK,m_button1,OnClickButton1)
284
+EVENT_MAP_END(CAppDialog)
285
+//+------------------------------------------------------------------+
286
+//| Constructor                                                      |
287
+//+------------------------------------------------------------------+
288
+CControlsDialog::CControlsDialog(void)
289
+  {
290
+  }
291
+//+------------------------------------------------------------------+
292
+//| Destructor                                                       |
293
+//+------------------------------------------------------------------+
294
+CControlsDialog::~CControlsDialog(void)
295
+  {
296
+  }
297
+
298
+//+------------------------------------------------------------------+
299
+//| Create                                                           |
300
+//+------------------------------------------------------------------+
301
+bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
302
+  {
303
+   if(!CAppDialog::Create(chart,name,subwin,20,20,dashWidth,dashHeight))
304
+      return(false);
305
+//--- create dependent controls
306
+
307
+   if(!CreatePanel())
308
+      return(false);
309
+
310
+   if(!CreateLabel0())
311
+      return(false);
312
+
313
+   if(!CreateButton())
314
+      return(false);
315
+   if(!CreateButton1())
316
+      return(false);
317
+
318
+//--- succeed
319
+   return(true);
320
+  }
321
+//+------------------------------------------------------------------+
322
+//|                                                                  |
323
+//+------------------------------------------------------------------+
324
+bool CControlsDialog::CreateLabel0(void)  //Trend Info
325
+  {
326
+//--- coordinates
327
+   int x1 = (dashWidth/2) - 235;
328
+   int y1 = yAxisGlobal + 15;
329
+   int x2 = 0;
330
+   int y2 = 0;
331
+//--- create
332
+   if(!m_label0.Create(m_chart_id,m_name+"Label0",m_subwin,x1,y1,x2,y2))
333
+      return(false);
334
+   if(!m_label0.Text(" Master Trades Lots Calc: 0.00"))
335
+      return(false);
336
+   if(!m_label0.Color(clrBlack))
337
+      return(false);
338
+   if(!m_label0.Font("Calibri"))
339
+      return(false);
340
+   if(!m_label0.FontSize(FontSize))
341
+      return(false);
342
+   if(!Add(m_label0))
343
+      return(false);
344
+//--- succeed
345
+   return(true);
346
+  }
347
+//+------------------------------------------------------------------+
348
+//|                                                                  |
349
+//+------------------------------------------------------------------+
350
+bool CControlsDialog::CreateButton(void)
351
+  {
352
+//--- coordinates
353
+// Buy
354
+   int x1 = (dashWidth/2);
355
+   int y1 = yAxisGlobal + 10;
356
+   int x2 = x1 + 70;
357
+   int y2 = y1 + 30;
358
+//--- create
359
+   if(!m_button.Create(m_chart_id,"button",m_subwin,x1,y1,x2,y2))
360
+      return(false);
361
+   if(!m_button.ColorBackground(clrSlateGray))
362
+      return(false);
363
+   if(!m_button.Text("Sell"))
364
+      return(false);
365
+   if(!m_button.FontSize(FontSize))
366
+      return(false);
367
+   if(!m_button.ColorBackground(clrRed))
368
+      return(false);
369
+   if(!m_button.Font("Calibri Bold"))
370
+      return(false);
371
+   if(!m_button.Color(clrWhiteSmoke))
372
+      return(false);
373
+   if(!m_button.ColorBorder(clrRed))
374
+      return(false);
375
+   if(!Add(m_button))
376
+      return(false);
377
+   m_button.Locking(true);
378
+   m_button.BringToTop();
379
+//--- succeed
380
+   return(true);
381
+  }
382
+//+------------------------------------------------------------------+
383
+//|                                                                  |
384
+//+------------------------------------------------------------------+
385
+bool CControlsDialog::CreateButton1(void)
386
+  {
387
+//--- coordinates
388
+// Sell
389
+   int x1 = (dashWidth/2) + 84;
390
+   int y1 = yAxisGlobal + 10;
391
+   int x2 = x1 + 70;
392
+   int y2 = y1 + 30;
393
+//--- create
394
+   if(!m_button1.Create(m_chart_id,"button1",m_subwin,x1,y1,x2,y2))
395
+      return(false);
396
+   if(!m_button1.ColorBackground(clrBlue))
397
+      return(false);
398
+   if(!m_button1.Text("Buy"))
399
+      return(false);
400
+   if(!m_button1.FontSize(FontSize))
401
+      return(false);
402
+   if(!m_button1.Font("Calibri Bold"))
403
+      return(false);
404
+   if(!m_button1.Color(clrWhiteSmoke))
405
+      return(false);
406
+   if(!m_button1.ColorBorder(clrBlack))
407
+      return(false);
408
+   if(!Add(m_button1))
409
+      return(false);
410
+   m_button1.Locking(true);
411
+   m_button1.BringToTop();
412
+//--- succeed
413
+   return(true);
414
+  }
415
+//+------------------------------------------------------------------+
416
+//|                                                                  |
417
+//+------------------------------------------------------------------+
418
+void CControlsDialog::OnClickButton(void)
419
+  {
420
+   if(m_button.Pressed())
421
+     {
422
+      Print(" Clicked on Button");
423
+      m_button.Pressed(false);
424
+      placeSellTrade();
425
+     }
426
+  }
427
+//+------------------------------------------------------------------+
428
+//|                                                                  |
429
+//+------------------------------------------------------------------+
430
+void CControlsDialog::OnClickButton1(void)
431
+  {
432
+   if(m_button1.Pressed())
433
+     {
434
+      Print(" Clicked on Button 1");
435
+      m_button1.Pressed(false);
436
+      placeBuyTrade();
437
+     }
438
+  }
439
+//+------------------------------------------------------------------+
440
+//|                                                                  |
441
+//+------------------------------------------------------------------+
442
+bool CControlsDialog::CreatePanel(void)
443
+  {
444
+//--- coordinates
445
+   int x1=0;
446
+   int y1=0;
447
+   int x2=dashWidth;
448
+   int y2=dashHeight;
449
+//--- create
450
+   if(!backgroundPanel.Create(0,ExtDialog.Name()+"backPanel",m_subwin,x1,y1,x2,y2))
451
+      return(false);
452
+   if(!backgroundPanel.ColorBackground(clrWhiteSmoke))
453
+      return(false);
454
+   if(!backgroundPanel.ColorBorder(00128))
455
+      return(false);
456
+   if(!ExtDialog.Add(backgroundPanel))
457
+      return(false);
458
+   ExtDialog.Minimized(false);
459
+
460
+
461
+   backgroundPanel.Alignment(WND_ALIGN_CLIENT,0,0,0,0);
462
+//--- succeed
463
+   return(true);
464
+  }
465
+
466
+CControlsDialog ExtDialog;
467
+CPanel   backgroundPanel;
468
+//+------------------------------------------------------------------+
469
+//|                                                                  |
470
+//+------------------------------------------------------------------+