Skip to content

Commit

Permalink
trade: procure last error in OrderManager (#24)
Browse files Browse the repository at this point in the history
Provide similar semantics as in `GetLastError()`.
  • Loading branch information
yerden authored and dingmaotu committed Mar 17, 2018
1 parent 44790f9 commit a1d6c64
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Trade/OrderManager.mqh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Module: Trade/OrderManager.mqh |
//| This file is part of the mql4-lib project: |
//| https://github.com/dingmaotu/mql4-lib |
Expand Down Expand Up @@ -34,6 +34,12 @@ class OrderManager
ObjectAttr(int,slippage,Slippage);
ObjectAttr(color,closeColor,CloseColor);

// last error implementation
private:
int m_lastError;
public:
int getLastError() {int err=m_lastError;m_lastError=0;return err;}

// custom implementation of BuyColor/SellColor properties
private:
color m_color[2];
Expand Down Expand Up @@ -67,6 +73,7 @@ public:
STOPLEVEL((int)SymbolInfoInteger(symbol,SYMBOL_TRADE_STOPS_LEVEL)),
m_magic(0),
m_slippage(3),
m_lastError(0),
m_closeColor(clrWhite)
{
m_color[0]=clrBlue;
Expand Down Expand Up @@ -138,7 +145,12 @@ public:
//--- Order closing
bool closeCurrent();
bool close(int ticket);
bool closeBy(int ticket,int other) {return OrderCloseBy(ticket,other,m_closeColor);}
bool closeBy(int ticket,int other)
{
bool res=OrderCloseBy(ticket,other,m_closeColor);
m_lastError=Mql::getLastError();
return res;
}
};
//+------------------------------------------------------------------+
//| Determine the pending order command based on the price |
Expand Down Expand Up @@ -183,6 +195,7 @@ int OrderManager::send(int cmd,double lots,double price,double stoploss,double t
if(ticket<0)
{
int err=Mql::getLastError();
m_lastError=err;
Alert(StringFormat(">>> Error OrderSend[%d]: %s",err,Mql::getErrorMessage(err)));
}

Expand All @@ -196,7 +209,9 @@ bool OrderManager::modify(int ticket,double stoploss,double takeprofit)
bool success=OrderModify(ticket,0,OrderBase::N(s,stoploss),OrderBase::N(s,takeprofit),0);
if(!success)
{
Alert(">>> Error modifying #",ticket,": ",Mql::getErrorMessage(Mql::getLastError()));
int err=Mql::getLastError();
m_lastError=err;
Alert(">>> Error modifying #",ticket,": ",Mql::getErrorMessage(err));
}
return success;
}
Expand All @@ -223,6 +238,7 @@ bool OrderManager::modifyPending(int ticket,double price,datetime expiration)
if(!success)
{
int err=Mql::getLastError();
m_lastError=err;
Alert(StringFormat(">>> Error modify pending order #%d[%s]: %s",
ticket,err,Mql::getErrorMessage(err)));
}
Expand All @@ -237,15 +253,19 @@ bool OrderManager::closeCurrent(void)
{
if(!OrderDelete(Order::Ticket(),m_closeColor))
{
Alert(">>> Error OrderDelete #",Order::Ticket(),": ",Mql::getErrorMessage(Mql::getLastError()));
int err=Mql::getLastError();
m_lastError=err;
Alert(">>> Error OrderDelete #",Order::Ticket(),": ",Mql::getErrorMessage(err));
return false;
}
}
else
{
if(!OrderClose(Order::Ticket(),Order::Lots(),Order::E(),m_slippage,m_closeColor))
{
Alert(">>> Error OrderClose #",Order::Ticket(),": ",Mql::getErrorMessage(Mql::getLastError()));
int err=Mql::getLastError();
m_lastError=err;
Alert(">>> Error OrderClose #",Order::Ticket(),": ",Mql::getErrorMessage(err));
return false;
}
}
Expand Down

0 comments on commit a1d6c64

Please sign in to comment.