Weird behavior on MQL5

 


I want a event that trigger always when a position is closed by SLTP. I found nothing native from MQL5 to do it, so I'm trying approaches to do myself an OnSLTP() function.

A piece from my code to do it uses the history:

    void ProcessClosedPositions(){
       datetime now = TimeCurrent();
       HistorySelect(lastDealProcessedDate, now);
       for(int i=0; i<HistoryDealsTotal(); i++){
          ulong dealTicket = HistoryDealGetTicket(i);
          if(magic != HistoryDealGetInteger(dealTicket, DEAL_MAGIC) || 
             symbol != HistoryDealGetString(dealTicket, DEAL_SYMBOL) || 
             DEAL_ENTRY_IN == (ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealTicket, DEAL_ENTRY) ||
             DEAL_ENTRY_INOUT == (ENUM_DEAL_ENTRY)HistoryDealGetInteger(dealTicket, DEAL_ENTRY)
          ) continue;
          
          int foundIdx = ArrayBsearch(alreadyProcessedDealTickets, dealTicket);
          if(foundIdx >= 0 && alreadyProcessedDealTickets[foundIdx] == dealTicket) continue;
          
          ulong orderTicket = HistoryDealGetInteger(dealTicket, DEAL_ORDER);
          ulong originalOrderTicket = HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID);
          
          HistoryOrderSelect(originalOrderTicket);

          PositionData data = {0};
          data.ticket = orderTicket;
          data.openPrice = HistoryOrderGetDouble(originalOrderTicket, ORDER_PRICE_OPEN);
          data.currentPrice = HistoryDealGetDouble(dealTicket, DEAL_PRICE);
          data.profit = HistoryDealGetDouble(dealTicket, DEAL_PROFIT);
          data.sl = HistoryOrderGetDouble(originalOrderTicket, ORDER_SL);
          data.tp = HistoryOrderGetDouble(originalOrderTicket, ORDER_TP);
          ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE) HistoryOrderGetInteger(originalOrderTicket, ORDER_TYPE);
          if(ORDER_TYPE_BUY == orderType){
             data.state = POSITION_TYPE_BUY;
          } else if(ORDER_TYPE_SELL == orderType){
             data.state = POSITION_TYPE_SELL;
          };
          data.volume = HistoryOrderGetDouble(orderTicket, ORDER_VOLUME_INITIAL)-HistoryOrderGetDouble(orderTicket, ORDER_VOLUME_CURRENT);
          ENUM_DEAL_REASON reason = (ENUM_DEAL_REASON) HistoryOrderGetInteger(orderTicket, ORDER_REASON);
          
          events.OnClosePosition(data, reason);
          
          lastDealProcessedDate = now;
          
          int totalProcessed = ArraySize(alreadyProcessedDealTickets);
          ArrayResize(alreadyProcessedDealTickets, totalProcessed+1);
          alreadyProcessedDealTickets[totalProcessed] = dealTicket;
          ArraySort(alreadyProcessedDealTickets);
       }
    }


I notice that if I use this sequence, the data will be returned right:

HistoryOrderSelect(originalOrderTicket);
ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE) HistoryOrderGetInteger(originalOrderTicket, ORDER_TYPE);
double volume = HistoryOrderGetDouble(orderTicket, ORDER_VOLUME_INITIAL)-HistoryOrderGetDouble(orderTicket, ORDER_VOLUME_CURRENT);


But if I invert the 2nd and 3nd lines, the orderType info will be wrong after some iterations.

HistoryOrderSelect(originalOrderTicket);
double volume = HistoryOrderGetDouble(orderTicket, ORDER_VOLUME_INITIAL)-HistoryOrderGetDouble(orderTicket, ORDER_VOLUME_CURRENT);
ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE) HistoryOrderGetInteger(originalOrderTicket, ORDER_TYPE);


Someone knows why this happen? I think that HistoryOrderGetDouble is setting some global reference that change the HistoryOrderGetInteger behavior...

 
Rafael Caetano Pinto:


I want a event that trigger always when a position is closed by SLTP. I found nothing native from MQL5 to do it, so I'm trying approaches to do myself an OnSLTP() function.

Use OnTradeTransaction() and check deal reason


A piece from my code to do it uses the history:


I notice that if I use this sequence, the data will be returned right:


But if I invert the 2nd and 3nd lines, the orderType info will be wrong after some iterations.


Someone knows why this happen? I think that HistoryOrderGetDouble is setting some global reference that change the HistoryOrderGetInteger behavior...

Please provide data and output log to demonstrate what you mean, as it doesn't make sense.

You NEED to check the returned value of functions : HistorySelect(), HistoryOrderSelect().