OrdersCount not working properly

 

Hi there, I'm testing my recent translated advisor but I suspect that orders count function is not working properly cause it isn't closing orders.

Here I count Buy and Sell orders, c ode:


   //---------------------------------------------- Internal loop ---!
   double BuyOrders=0,SellOrders=0;
   for(int i=0;i<PositionsTotal();i++){
      ulong iTicket=PositionGetTicket(i);
      if(PositionSelectByTicket(iTicket)&&
      PositionGetString(POSITION_SYMBOL)==_Symbol){
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){BuyOrders++;}
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){SellOrders++;}
         }
      }

Then I try to close first open order when there is an opposite signal, code:

   //-------------------------------------------- OrderClose loop ---!
   for(int i=0;i<OrdersTotal();i++){
      ulong iTicket=PositionGetTicket(i);
      if(PositionSelectByTicket(iTicket)&&
      PositionGetString(POSITION_SYMBOL)==_Symbol){
         if(PositionsTotal()>1){ // There is a second order (opposite), so I'm trying to close the first one.
            if(!trade.PositionClose(iTicket,ULONG_MAX)){
               Print("PositionClose error ",trade.ResultRetcode());
               return;
               }
            }
         }
      }

What expert does in backtest is open 1 buy and 1 sell closing no one, it works in MQ4. Need some help, thanks.

 
   //-------------------------------------------- OrderClose loop ---!
   for(int i=0;i<OrdersTotal();i++){
      ulong iTicket=PositionGetTicket(i);
      if(PositionSelectByTicket(iTicket)&&
      PositionGetString(POSITION_SYMBOL)==_Symbol){
         if(PositionsTotal()>1){ // There is a second order (opposite), so I'm trying to close the first one.
            if(!trade.PositionClose(iTicket,ULONG_MAX)){
               Print("PositionClose error ",trade.ResultRetcode());
               return;
               }
            }
         }
      }

You are using the wrong total.

I suggest to count down from total-1 to 0, specially when closing orders/positions.

You are also using PositionsTotal()>1, totally untrustable in case of EA running on several charts/pairs or several EA running at the same time or account mixed manual/automatic trading.

 
Fabio Cavalloni:

You are using the wrong total.

I suggest to count down from total-1 to 0, specially when closing orders/positions.

You are also using PositionsTotal()>1, totally untrustable in case of EA running on several charts/pairs or several EA running at the same time or account mixed manual/automatic trading.

Thank you I wasn't noticed that, first loop was correct but second one was wrong. Now it's working perfectly.

Count is forward cause I need to close first order when positionstotal>1. The problem about you're speaking is solved by MagicNumber but I still don't know how to place Magic.

 

Keep in mind that PositionsTotal() contains ALL positions (also manual and opened from other EA).

It can work well only in backtest.

 
Fabio Cavalloni:

Keep in mind that PositionsTotal() contains ALL positions (also manual and opened from other EA).

It can work well only in backtest.

Yes I know, I'm having to set a Magic Number. Is it an easy thing, how can I do?
 

I suggest to use trading classes objects (CTrade for positions opening/closign) to avoid errors and make better understandable code.

 
Example: the adviser calculates positions by the current symbol. Counts only those positions for which the Magic number matches the specified: