PositionsTotal() return 0 after a call of function trade.PositionOpen() - page 4

To add comments, please log in or register
Dorian Baranes
32674
Dorian Baranes  
Alain Verleyen:
It may happens there is never a deal, even when the order was accepted.

Here is the code I am using:

void OnTick()
{
  //Check an order is currently opened in progress
  if(tryToFinishOpenPosition==true)
  {
   finishOpenPosition();
   return;
  }
  openPosition(ORDER_TYPE_BUY, initial_lot);
}
//********************************************************************************
int openPosition(ENUM_ORDER_TYPE  cmd, double volume)
{
   ...
   if(!trade.PositionOpen(_Symbol,cmd,size,openPrice,0,0,RobotName+" "+timeframeToString(_Period)) || trade.ResultRetcode()!=TRADE_RETCODE_DONE)
   {

      lastErrorMessage="Position open failed. Return code="+IntegerToString(trade.ResultRetcode());
      lastDateErrorMessage=TimeCurrent();        
      Print("code retour du trade:",trade.ResultRetcode());
      return -1;
      
   }
   
   finishOpenPosition();
   return lastTicketNumber;
}
//**********************************************************************************
bool tryToFinishOpenPosition=false;
int finishOpenPosition()
{
   
   lastTicketNumber=(int) trade.ResultDeal();
   if(lastTicketNumber==0) 
   {
      Print("Result Deal: ",trade.ResultDeal());
      Print("PositionsTotal:",PositionsTotal());
      tryToFinishOpenPosition=true;
      return lastTicketNumber;
   }
   else
   {
      tryToFinishOpenPosition=false;
   }
}

Result:

ResulDeal keeps always the value 0 even though the position has been received by the server.

So what is wrong in my code ?

Regards,

Dorian

fxsaber
10579
fxsaber  
Dorian Baranes:

So what is wrong in my code ?

What do you want?

Dorian Baranes
32674
Dorian Baranes  
fxsaber:

What do you want?

As you can see on my example the ResultDeal is not a good indicator to check if my order has been executed since it always keeping the value 0. So which condition should I use to make sure transaction has been executed on the server side  ?

Regards,

Dorian 

Dorian Baranes
32674
Dorian Baranes  
Anthony Garot:

I monitor in the OnTradeTransaction() event. This is similar to @fxsaber 's solution.

Thanks Anthony. The event handler "OnTradeTransaction()" is a good indicator to check if my transaction has been executed.

Alain Verleyen
36534
Alain Verleyen  
Dorian Baranes:

Here is the code I am using:

Result:

ResulDeal keeps always the value 0 even though the position has been received by the server.

Of course trade.ResultDeal() will never be updated.

You are mixing deal, order and position.

So what is wrong in my code ?

int finishOpenPosition()
  {
   lastTicketNumber=(int) trade.ResultOrder();
   if(HistoryOrderSelect(lastTicketNumber))
     {     
      //Print("Result Deal: ",trade.ResultDeal());
      Print("PositionsTotal:",PositionsTotal());
      tryToFinishOpenPosition=true;
      return lastTicketNumber;
     }
   else
     {
      tryToFinishOpenPosition=false;
     }
  }
Something like that. There are several ways to do it, using OnTrade() or OnTradeTransaction() as suggested by Anthony is better than waiting a new tick.
Dorian Baranes
32674
Dorian Baranes  
Alain Verleyen:

Of course trade.ResultDeal() will never be updated.

You are mixing deal, order and position.

Something like that. There are several ways to do it, using OnTrade() or OnTradeTransaction() as suggested by Anthony is better than waiting a new tick.

Hi Alain,

Ok I understand that trade.ResultDeal will never be updated.

However does trade.ResultOrder will always contain an order number if no errors are found as showed in the example:

int lastTicketNumber;
int openPosition(ENUM_ORDER_TYPE  cmd, double volume)
{
   ...
   if(!trade.PositionOpen(_Symbol,cmd,size,openPrice,0,0,RobotName+" "+timeframeToString(_Period)) || trade.ResultRetcode()!=TRADE_RETCODE_DONE)
   {

      lastErrorMessage="Position open failed. Return code="+IntegerToString(trade.ResultRetcode());
      lastDateErrorMessage=TimeCurrent();        
      Print("code retour du trade:",trade.ResultRetcode());
      return -1;
      
   }
   
   lastTicketNumber=(int) trade.ResultOrder();  // Does result order will be always superior to 0
   return lastTicketNumber;
}


I am asking this question because I would like to use the variable lastTicketNumber to identified the execution of the transaction in the function OnTradeTransaction()

void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request,const MqlTradeResult& result)
{
    
    if ( trans.deal > 0 && trans.order==lastTicketNumber)
    {   ...

Regards,

Dorian

Alain Verleyen
36534
Alain Verleyen  
Dorian Baranes:

Hi Alain,

Ok I understand that trade.ResultDeal will never be updated.

However does trade.ResultOrder will always contain an order number if no errors are found as showed in the example:


I am asking this question because I would like to use the variable lastTicketNumber to identified the execution of the transaction in the function OnTradeTransaction()

Regards,

Dorian

Yes it should.

Please note that your way to filter "no errors" is incorrect. TRADE_RETCODE_DONE is not the only "no errors" answer.

To add comments, please log in or register