Nav apraksta

localHedgingMasterCopierMt5.mq5 16KB

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