How to query if Stop Loss has been triggered

 
Hey guys,
im working on a Expert Advisor and need some help. How can i check if a position has been closed by a triggered Stop Loss? Currently i check if positions are open and if not, i want to know if the last position has been successful closed or as said, triggered by Stop Loss. I searched the docs but until now with no luck. 
It would be great if someone could point me into the right direction, thanks.
 
  1. Some (not all) brokers modify the comment, usually adding "[SL]"

  2. bool wasBySL = MathAbs( OrderClosePrice() - OrderStopLoss() ) < MathAbs( OrderClosePrice() - OrderTakeProfit() );
    Remember that stops become market orders, and there will be slippage. So OCP < OSL will not work.
 
whroeder1:
  1. Some (not all) brokers modify the comment, usually adding "[SL]"

  2. Remember that stops become market orders, and there will be slippage. So OCP < OSL will not work.

Thanks but i guess this will only work for MT4? Found no mql5 reference to commands like OrderClosePrice() ?

 
nowayfra: Thanks but i guess this will only work for MT4? Found no mql5 reference to commands like OrderClosePrice() ?

Please read the following thread for the MQL5 explanation: https://www.mql5.com/en/forum/130

Can't find the OrderClose Statement im MQL5
Can't find the OrderClose Statement im MQL5
  • 2009.12.09
  • www.mql5.com
It would be nice, if the HTML Help File would mention the OrderClose statement and its expression in MQL5 with a short examble...
 
Fernando Carreiro:

Please read the following thread for the MQL5 explanation: https://www.mql5.com/en/forum/130


Ok digged it out by myself, here some code for the records (a script)

void OnStart()
{

        datetime to = TimeCurrent(); 
        datetime from = to - PeriodSeconds(PERIOD_D1); // check deals for the current day
        ResetLastError(); 
        if(! HistorySelect(from, to))
                Print(__FUNCTION__, " HistorySelect=false. Error code=", GetLastError()); 
        
        unsigned int numOrders = HistoryOrdersTotal();
        unsigned long ticket = HistoryOrderGetTicket(numOrders - 1);
        
        ENUM_ORDER_REASON reason = 0;
        if(HistoryOrderSelect(ticket))
                reason = HistoryOrderGetInteger(ticket, ORDER_REASON);
        
        if(EnumToString(reason) == "ORDER_REASON_SL")
                Print("ticket ", ticket, "  triggered SL");
}
 
nowayfra: Thanks but i guess this will only work for MT4? Found no mql5 reference to commands like OrderClosePrice() ?

Why is this thread in the MQL4 section, (bottom of the Root page?) Instead of Root / MT5 EA section?
Next time post in the correct place. The moderators will likely move this thread there soon.

 
nowayfra:

Ok digged it out by myself, here some code for the records (a script)

Look nice
 

You can use this function:

void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result)
{
   
}


Documentation on MQL5: Event Handling / OnTradeTransaction
Documentation on MQL5: Event Handling / OnTradeTransaction
  • www.mql5.com
OnTradeTransaction - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Attention Moderators: Please move this thread into a MQL5 section!
Reason: