Problems closing trade using ticket number

 

Hi

I'm confused about closing a trade.

I am working on a multi symbol EA. So I have an array of symbols that the code works through. I also have a corresponding array of tickets for any trades that are opened. I'm trying to close trades when exit conditions are met, using their ticket number.

Most of this is working except that I don't seem to be able to close a trade. Stepping through the code it seems that the CTrade::PositionClose(const ulong ticket,const ulong deviation) function is not seeing a ticket number, despite the watch list showing the ticket number.

This is my close trade function gets passed an index value for my array that corrisponds to the trade that needs to be closed. 

void ProcessTradeClose(int indx){
   //positionclose can have either symbol (netting account only i think) or ticket. Sometimes seems to be unable to select ticket
   trade.PositionClose(openTradeOrderTicket[indx],ULONG_MAX);//position needs to be closed ULONG_MAX means it gets closed at any price
   // test retcode, bool return from PositionClose is unreliable
   //if retcode good set trade array to 0 to allow future trades to be opened
   ulong ticketNumberToClear=openTradeOrderTicket[indx];
   uint result=trade.ResultRetcode();
   
   if (result==10009) //Request completed
      {
      openTradeOrderTicket[indx]=0;//clear this ticket
      Print("ProcessTradeClose - Succesfully closed Ticket number ",ticketNumberToClear," for symbol ",symbolArray[indx], " cleared" );   
      }
   else {
         Print("ProcessTradeClose- PositionClose method failed. Return code=",trade.ResultRetcode(),
         ". Code description: ",trade.ResultRetcodeDescription());
         }


   
   
   return;
}

So working though the debugging processes, I see for example a trade with a ticket number 11 on the chart display and the variable openTradeOrderTicket[indx]=11. So that is correct.

I then step into the trade.mqh module

/+------------------------------------------------------------------+
//| Close specified opened position                                  |
//+------------------------------------------------------------------+
bool CTrade::PositionClose(const ulong ticket,const ulong deviation)
  {
//--- check stopped
   if(IsStopped(__FUNCTION__))
      return(false);
//--- check position existence
   if(!PositionSelectByTicket(ticket))
      return(false);
   string symbol=PositionGetString(POSITION_SYMBOL);
//--- clean
   ClearStructures();
//--- check filling
   if(!FillingCheck(symbol))
      return(false);
//--- check
   if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
     {
      //--- prepare request for close BUY position
      m_request.type =ORDER_TYPE_SELL;
      m_request.price=SymbolInfoDouble(symbol,SYMBOL_BID);
     }
   else
     {
      //--- prepare request for close SELL position
      m_request.type =ORDER_TYPE_BUY;
      m_request.price=SymbolInfoDouble(symbol,SYMBOL_ASK);
     }
//--- setting request
   m_request.action   =TRADE_ACTION_DEAL;
   m_request.position =ticket;
   m_request.symbol   =symbol;
   m_request.volume   =PositionGetDouble(POSITION_VOLUME);
   m_request.magic    =m_magic;
   m_request.deviation=(deviation==ULONG_MAX) ? m_deviation : deviation;
//--- close position
   return(OrderSend(m_request,m_result));
  }

on entry the debugger's watch shows ticket is still 11. However:

if(!PositionSelectByTicket(ticket))
      return(false);

that PositionSelectByTicket function seems to return false and the PositionClose function ends without doing anything, the trade is left open, but trade.ResultRetcode returns 10009 that I thought meant TRADE_RETCODE_DONE, Request completed.

I'm obviously missing something, but I don't know what? 


Thank you for any help given.

 
pete sw:

Hi

I'm confused about closing a trade.

I am working on a multi symbol EA. So I have an array of symbols that the code works through. I also have a corresponding array of tickets for any trades that are opened. I'm trying to close trades when exit conditions are met, using their ticket number.

Most of this is working except that I don't seem to be able to close a trade. Stepping through the code it seems that the CTrade::PositionClose(const ulong ticket,const ulong deviation) function is not seeing a ticket number, despite the watch list showing the ticket number.

This is my close trade function gets passed an index value for my array that corrisponds to the trade that needs to be closed. 

So working though the debugging processes, I see for example a trade with a ticket number 11 on the chart display and the variable openTradeOrderTicket[indx]=11. So that is correct.

I then step into the trade.mqh module

on entry the debugger's watch shows ticket is still 11. However:

that PositionSelectByTicket function seems to return false and the PositionClose function ends without doing anything, the trade is left open, but trade.ResultRetcode returns 10009 that I thought meant TRADE_RETCODE_DONE, Request completed.

I'm obviously missing something, but I don't know what? 


Thank you for any help given.

Further investigation seems to suggest that I don't understand the ticketnumber. 

This is my code for opening the trade:

void ProcessTradeOpen(int indx, string tradeDirection, double sl)
   {
      string currentSymbol=symbolArray[indx];

   double tp=0;
   if (tradeDirection=="LONG")
      {
      //calc lots
      double lots;
      if(!CalcLots(lastTick.bid-sl,lots)){return;}
      if (trade.PositionOpen(currentSymbol,ORDER_TYPE_BUY,lots,lastTick.ask,sl,0,"Ichi Breakout Strat- long trade"))
         {
         openTradeOrderTicket[indx]=trade.ResultDeal();
         openTradeOrderDir[indx]="LONG";
         Print(currentSymbol," we have just created a, ",openTradeOrderDir[indx], " order with ticket no :",openTradeOrderTicket[indx] );
         }
      else 
         {
         Print("PositionOpen() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
         }
      }           
      
   else if (tradeDirection=="SHORT")
      {
   //calc lots
      double lots;
      if(!CalcLots(lastTick.bid-sl,lots)){return;}
      if (trade.PositionOpen(currentSymbol,ORDER_TYPE_SELL,lots,lastTick.ask,sl,0,"Ichi Breakout Strat - short trade"))
         {
         openTradeOrderDir[indx]="SHORT";   
         openTradeOrderTicket[indx]=trade.ResultDeal();
         Print(currentSymbol," we have just created a, ",openTradeOrderDir[indx], " order with ticket no :",openTradeOrderTicket[indx] );
         }
     else 
         {
         Print("PositionOpen() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
         }
 
      }


   return;

}

I thought trade.ResultDeal() returned the ticketnumber? but I get a different value when I step through the list of open trades and then retrieve the ticket number for my symbol. 

I thought trade.ResultDeal() returned the ticketnumber. https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctraderesultdeal says "Gets the deal ticket." I guess that is not the same? 

I assume I need to find a function that returns the same parameter as expected by the PositionSelectTicket() function?
Documentation on MQL5: Standard Library / Trade Classes / CTrade / ResultDeal
Documentation on MQL5: Standard Library / Trade Classes / CTrade / ResultDeal
  • www.mql5.com
ResultDeal - CTrade - Trade Classes - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5