Come controllare se un ordine è stato chiuso per stop loss - pagina 7

 
Francesco Fava:

Sto scrivendo un EA, modificando il mio EA scritto per MT4. Apro un ordine usando OrderSend.

Semplicemente se ho bisogno di capire (perhasps da History), se l'ordine inviato (a proposito.... devo controllare Order, Deals o Position?), è stato chiuso perchè ha raggiunto lo StopLoss.
Ho controllato sui forum, ma non ho trovato quello che mi serve....

Uso HistoryOrderGetDouble con il parametroENUM_ORDER_PROPERTY_DOUBLE,

PREZZO_ORDINE_CORRENTE

dà solo il prezzo corrente del simbolo dell'ordine

che non può essere confrontato conORDER_SL. Suppongo che ORDER_PRICE_CURRENT sia solo il prezzo corrente del simbolo e non il prezzo di chiusura dell'ordine.

Grazie a tutti per il vostro supporto.

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


Risultato

#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 sul trading, sistemi di trading automatizzati e test di strategie di trading

Ultimi due ordini

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