How to check if an order has been closed for stop loss - page 7

 
Francesco Fava:

I'm writing an EA, modifying my EA written for MT4. I open an order using OrderSend. 

Simply if I need to understand (perhasps from History), if the order sent (by the way.... I have to check Order, Deals or Position?), has been closed because it has reached StopLoss.
I checked on forums, but I have not found what I need.... 

Using HistoryOrderGetDouble with paramter ENUM_ORDER_PROPERTY_DOUBLE

ORDER_PRICE_CURRENT

gives only the current price of the order symbol

 that cannot be compared to ORDER_SL. I suppose ORDER_PRICE_CURRENT is only the current price of symbol and not the Order close price .

Thanks to all for your support.

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void OnStart()
{
  for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderType() <= OP_SELL))
    {
      OrderPrint();
      Print(EnumToString(OrderCloseReason()));
      
      if (OrderCloseReason() == DEAL_REASON_SL)
        Print("StopLoss!");      
    }
}


Result

#76922 2018.01.17 18:36:39 buy 1.00 EURUSD 1.22274 1.22273 0.00000 2018.01.17 18:36:47 1.22274 -12.22 0.00 0.00 [sl 1.22273] 0
DEAL_REASON_SL
StopLoss!
#76920 2018.01.17 18:27:29 sell 1.00 EURUSD 1.22376 0.00000 1.22375 2018.01.17 18:27:44 1.22375 -12.24 0.00 1.00 [tp 1.22375] 0
DEAL_REASON_TP
#875577 2018.01.17 18:27:43 buy 1.00 EURUSD 1.22375 0.00000 0.00000 2018.01.17 18:27:43 1.22375 0.00 0.00 0.00 0 expiration 2018.01.17 18:27:43
DEAL_REASON_TP
#76912 2018.01.17 16:49:24 sell 1.00 EURUSD 1.22233 0.00000 0.00000 2018.01.17 16:49:25 1.22237 -12.22 0.00 -4.00 0
DEAL_REASON_CLIENT
#76493 2018.01.11 08:35:00 buy 1.00 EURUSD 1.19462 0.00000 0.00000 2018.01.15 01:28:32 1.21962 -12.07 -8.48 2500.00 0
DEAL_REASON_CLIENT
#76492 2018.01.11 08:34:58 sell 1.00 EURUSD 1.19458 0.00000 0.00000 2018.01.15 01:28:32 1.21967 -12.07 2.04 -2509.00 0
DEAL_REASON_CLIENT
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Deal Properties
  • www.mql5.com
A deal is the reflection of the fact of a trade operation execution based on an order that contains a trade request. Each trade is described by properties that allow to obtain information about it. In order to read values of properties, functions of the Identifier of a position, in the opening, modification or closing of which this deal...
 

Forum on trading, automated trading systems and testing trading strategies

Last two orders

fxsaber, 2017.12.23 11:02

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

// Возврат тикетов последних Amount-сделок, закрытых по Reason-причине
int GetLastHistoryPositions( long &Tickets[], int Amount = INT_MAX, const ENUM_DEAL_REASON Reason = DEAL_REASON_SL )
{
  int Count = ArrayResize(Tickets, 0);
  
  for (int i = OrdersHistoryTotal() - 1; (i >= 0) && (Count < Amount); i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderCloseReason() == Reason))
      Tickets[ArrayResize(Tickets, ++Count) - 1] = OrderTicket();
  
  return(Count);
}

void OnStart()
{
  long Tickets[];
  
  // Последние две сделки, закрытые по SL
  for (int i = GetLastHistoryPositions(Tickets, 2) - 1; i >= 0; i--)
    Print(Tickets[i]);
}


Addon for MT4

enum ENUM_DEAL_REASON
{
  DEAL_REASON_CLIENT,
  DEAL_REASON_SL,
  DEAL_REASON_TP
};

ENUM_DEAL_REASON OrderCloseReason( void )
{
  return((StringFind(OrderComment(), "[sl]") != -1) ? DEAL_REASON_SL :
         ((StringFind(OrderComment(), "[tp]") != -1) ? DEAL_REASON_TP : DEAL_REASON_CLIENT));
}
Reason: