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

 
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

 
Dorian Baranes:

So what is wrong in my code ?

What do you want?

 
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 

 
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.

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

 
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.

 
HI Dorian. I'm Still NOT able 2 - Get the '' Double Top Tracker'' 2 - Work after Purchasing the tool..  I've Install It Several times Hoping that Would Work, But Still Nothing.. I've tried dorian75  Still NO response From U - I'm running OUT Of Ideas!!
 
HOW Can I - Make this tool Work. '' Double Top Tracker v3 ''
 
Alain Verleyen:
It may happens there is never a deal.
HI. Trying 2 - reach U..
Reason: