EA mixes BUY and SELL orders

 
Hi,

I'm trying to code an EA and at some place in my code I've to update the stoploss of opened and pending orders. I'm displaying an alert with the informations about the order.

When I run it, I receive a message like: "Count: 2 Stoploss 1.44705 for buy order: 46450421 orderType: 1".

orderType: 1 means that order 46450421 is a OP_SELL order, which it is, but why is it triggering the test "if ((orderType == OP_BUY || orderType == OP_BUYSTOP))" instead of "if ((orderType == OP_SELL || orderType == OP_SELLSTOP))"?

int orderType;
   for(cnt = total-1; cnt >= 0; cnt--)
   {
      while (!OrderSelect(cnt, SELECT_BY_POS)) {Sleep(500);}
      orderType = OrderType();
      if(/*OrderMagicNumber()==Magic &&*/ OrderSymbol()==Symbol())
      {      
         if ((orderType == OP_BUY || orderType == OP_BUYSTOP)) {
            if (!(isSLinTheZone && areBuyOrdersInProfitAt(NormalizeDouble(Low[1] - NbPipsTolerance*Point*10,Digits))))
               if (NormalizeDouble(OrderStopLoss(), Digits) != longSL) { 
                  if (Debug) {
                     Alert("LongSL: " + longSL);
                     Alert("Order SL: " + NormalizeDouble(OrderStopLoss(), Digits));
                     Alert("Modif required");
                     Alert("Count: " + cnt + " Stoploss " + longSL + " for buy order: " + OrderTicket() + " orderType: " +OrderType());
                  }
                  OrderModify(OrderTicket(), OrderOpenPrice(), longSL, OrderTakeProfit(),0, Yellow);
                  //cnt = total-1;
               }
         }
         
         if ((orderType == OP_SELL || orderType == OP_SELLSTOP)) { 
            if (!(isSLinTheZone && areSellOrdersInProfitAt(NormalizeDouble(High[1] + NbPipsTolerance*Point*10 + Spread*Point*10,Digits))))
               if (NormalizeDouble(OrderStopLoss(), Digits) != shortSL) { 
                  if (Debug) {
                     Alert("ShortSL: " + shortSL);
                     Alert("Order SL: " + NormalizeDouble(OrderStopLoss(), Digits));
                     Alert("Modif required");
                     Alert("Count: " + cnt + " Stoploss " + shortSL + " for sell order: " + OrderTicket() + " orderType: " +OrderType());
                  }
                  OrderModify(OrderTicket(), OrderOpenPrice(), shortSL, OrderTakeProfit(),0, Yellow);
                  //cnt = total-1; 
               }
         }   
      } // end of if
   } // end of for


Please help :-)

Daniel
 

 
ubzen:


I've updated the post.
 

For starters, try using the Integer representations and run the test again. See if that solves your problem then we'll talk about it some more. Edit: I think this has something to do with the fact that ordertype will start at 0. And orderselect will keep the last selected order in place.

 
ubzen:

For starters, try using the Integer representations and run the test again. See if that solves your problem then we'll talk about it some more.

It didn't change anything. What I don't understand is that in the alert message, OrderType() is equal to 1, which means it is a sell order. and it is really a sell order, but the code of the buy order is executed. I'm really lost. I tried to loop until OrderSelect(...) return true, as it appears in the code above. I thought perhaps it could not select the order. But with it or without it does exactly the same.
 
ubzen:

Edit: I think this has something to do with the fact that ordertype will start at 0. And orderselect will keep the last selected order in place.

Could be . . .

Try declaring orderType as something it can't be . . . e.g.

int orderType = 10;

. . . then see what happens, if you get no messages then your order selection isn't working.

 
RaptorUK:

Could be . . .

Try declaring orderType as something it can't be . . . e.g.

. . . then see what happens, if you get no messages then your order selection isn't working.


It works exactly the same if I first declare: int orderType = 10;

I also tried using OrderType() always instead of declaring a variable, but there is the same problem.

I also tried to call OrderSelect(...) just before modifying the order, but no change.

 
Are you testing this in the back-tester or in a demo-account?
 
ubzen:
Are you testing this in the back-tester or in a demo-account?

I'm testing live. I put the EA on a chart and see what it does and I'm using the "Ask for confirmation" check box.
 

Add in some Print(); statements so you can see what is going wrong . . . e.g.

if(/*OrderMagicNumber()==Magic &&*/ OrderSymbol()==Symbol())
      {      
         Print("orderType = ",orderType);
         if ((orderType == OP_BUY || orderType == OP_BUYSTOP)) {

. . . then take a look in the Experts tab.

 
Is it possible that the problem comes from the use of salaires manual confirmation?
Reason: