From b5ae534a75ee09990745942ff6637673f45d957d Mon Sep 17 00:00:00 2001 From: yerden Date: Sat, 17 Mar 2018 14:34:08 +0600 Subject: [PATCH] trade: introduce partial close of market orders (#25) This patch adds capability to partially close market order by overloading close() and closeCurrent() in OrderManager class. --- Trade/OrderManager.mqh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Trade/OrderManager.mqh b/Trade/OrderManager.mqh index 457f785..12307e3 100644 --- a/Trade/OrderManager.mqh +++ b/Trade/OrderManager.mqh @@ -144,7 +144,9 @@ public: //--- Order closing bool closeCurrent(); + bool closeCurrent(double lots); bool close(int ticket); + bool close(int ticket,double lots); bool closeBy(int ticket,int other) { bool res=OrderCloseBy(ticket,other,m_closeColor); @@ -284,3 +286,34 @@ bool OrderManager::close(int ticket) return closeCurrent(); } //+------------------------------------------------------------------+ +//| Partially close current selected market order | +//+------------------------------------------------------------------+ +bool OrderManager::closeCurrent(double lots) + { + if(Order::IsPending()) + { + Alert(">>> Use close() on pending order #",Order::Ticket()); + return false; + } + if(!OrderClose(Order::Ticket(),lots,Order::E(),m_slippage,m_closeColor)) + { + int err=Mql::getLastError(); + m_lastError=err; + Alert(">>> Error OrderClose #",Order::Ticket(),": ",Mql::getErrorMessage(err)); + return false; + } + return true; + } +//+------------------------------------------------------------------+ +//| Partially close the market order with specified ticket | +//+------------------------------------------------------------------+ +bool OrderManager::close(int ticket,double lots) + { + if(!Order::Select(ticket)) + { + Alert(">>> Error closing order with invalid ticket #",Order::Ticket(),": ",Mql::getErrorMessage(Mql::getLastError())); + return false; + } + return closeCurrent(lots); + } +//+------------------------------------------------------------------+