OrderType Issue in a for loop

 
Hey, I would like to ask a question for the my issue I've been experiencing. It's so weird. I have a for loop to close my orders. As you see on the code, the if(OrderType()==OP_BUY) block doesn't work. And more interesting than that this, this block works on a X broker, but doesn't work on Y broker. Same codes, same things. Also I added Print("z") to check if it works or not. The first Print("z") get executed and works. But the one in the if(OrderType()==OP_BUY) block doesn't work.

Does anyone have an idea what reason cause this?
for(int c=OrdersTotal()-1; c>=0; c--)
        {
         if(!OrderSelect(c,SELECT_BY_POS,MODE_TRADES))
            continue;
         if(OrderMagicNumber()==magicNumber)
           {
            RefreshRates();
            /*if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
              {
               OrderDelete(OrderTicket());
               islemSayac--;
              }*/
            Print("z");
            if(OrderType()==OP_BUY)
              {
               Print("z");
               bool x = OrderClose(OrderTicket(),OrderLots(),Bid,slip,0);
               if(x==False)
                  Alert("OrderClose BUY Hatası: ",GetLastError());
               else
                  if(x==True)
                     sayac1--;
               islemSayac--;
              }
            else
               if(OrderType()==OP_SELL)
                 {
                  bool x = OrderClose(OrderTicket(),OrderLots(),Ask,slip,0);
                  if(x==False)
                     Alert("OrderClose SELL Hatası: ",GetLastError());
                  else
                     if(x==True)
                        sayac1--;
                  islemSayac--;
                 }
            return;
           }
        }
 
weyhub:
Hey, I would like to ask a question for the my issue I've been experiencing. It's so weird. I have a for loop to close my orders. As you see on the code, the if(OrderType()==OP_BUY) block doesn't work. And more interesting than that this, this block works on a X broker, but doesn't work on Y broker. Same codes, same things. Also I added Print("z") to check if it works or not. The first Print("z") get executed and works. But the one in the if(OrderType()==OP_BUY) block doesn't work.

Does anyone have an idea what reason cause this?


You want to close all Orders but open orders seem not to be the same parity. This code will work. If you only want to trade with the parity loaded by this script/EA, we can add the code below.

In general you can use the long code below.

if(OrderMagicNumber()==magicNumber && OrderSymbol()==Symbol())
for(int c=OrdersTotal()-1; c>=0; c--)
        {
           
         if(!OrderSelect(c,SELECT_BY_POS,MODE_TRADES))
            continue;
         if(OrderMagicNumber()==magicNumber)
           {
            RefreshRates();
            /*if(OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP || OrderType()==OP_BUYLIMIT || OrderType()==OP_SELLLIMIT)
              {
               OrderDelete(OrderTicket());
               islemSayac--;
              }*/
            Print("z");
            if(OrderType()==OP_BUY)
              {
               Print("z");
               bool x=OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
               //bool x = OrderClose(OrderTicket(),OrderLots(),Bid,slip,0);
               if(!x)
                  Alert("OrderClose BUY Hatası: ",GetLastError());
               else
                  if(x)
                     sayac1--;
               islemSayac--;
              }
            else
               if(OrderType()==OP_SELL)
                 {
                  bool x =OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                 // bool x = OrderClose(OrderTicket(),OrderLots(),Ask,slip,0);
                  if(!x)
                     Alert("OrderClose SELL Hatası: ",GetLastError());
                  else
                     if(x)
                        sayac1--;
                  islemSayac--;
                 }
            return;
           }
        } 
 
weyhub The first Print("z") get executed and works. But the one in the if(OrderType()==OP_BUY) block doesn't work.

Does anyone have an idea what reason cause this?
  1. “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. 2004
              When asking about code

    What does the log say?

  2. You don't have any open buy orders?  You are using a US broker and your loop is not FIFO compliant? What does the log say?

  3. Combine all your filters
    for(int c=OrdersTotal()-1; c>=0; c--)
    {
       if(!OrderSelect(c,SELECT_BY_POS,MODE_TRADES))
         continue;
       if(OrderMagicNumber()==magicNumber)
    and simplify.
    for(int c=OrdersTotal()-1; c>=0; c--) if(
       OrderSelect(c,SELECT_BY_POS,MODE_TRADES)
    && OrderMagicNumber()==magicNumber
    && OrderSymbol()==Symbol()
    ){
  4. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price.

Reason: