Cómo comprobar si una orden ha sido cerrada por stop loss - página 7

 
Francesco Fava:

Estoy escribiendo un EA, modificando mi EA escrito para MT4. Abro una orden usando OrderSend.

Simplemente si necesito entender (perhasps del Historial), si la orden enviada (por cierto.... tengo que comprobar Orden, Operaciones o Posición?), se ha cerrado porque ha llegado al StopLoss.
He comprobado en los foros, pero no he encontrado lo que necesito....

Usando HistoryOrderGetDouble con el parámetroENUM_ORDER_PROPERTY_DOUBLE,

ORDER_PRICE_CURRENT

da sólo el precio actual del símbolo de la orden

que no se puede comparar conORDER_SL. Supongo que ORDER_PRICE_CURRENT es sólo el precio actual del símbolo y no el precio de cierre de la orden .

Gracias a todos por su apoyo.

#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!");      
    }
}


Resultado

#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...
 

Foro sobre el comercio, los sistemas automatizados de comercio y la prueba de estrategias de comercio

Las dos últimas órdenes

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 para 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));
}