How do you test orderselect for true or false

 

Hi, Maybe someone could help me..

Trying to loop through open orders on every bar.

When there is one order open, my code does not see it, but when the orderstotal is one less -- then it sees the open order, but some reason there is a double execution of there being an order found and not.

For looping though orders total I have been counting down and from the MQL4 forum it seemed to count up

Have tried to create several different scenarios and search for what i am looking for from MQL4 but only got a headakeh

//----------------------------------------------------------------------------------------------------------
int start() 
//----------------------------------------------------------------------------------------------------------
   {
   if(Digits==3){SymPoint=0.010; SymSpread=(Ask-Bid)*100;}
   if(Digits==5){SymPoint=0.00010; SymSpread=(Ask-Bid)*10000;}
   if(TrailingStart>=0 && TrailingStop>0){RunTrail();}
   if(Bar1Time!=Time[1]) 
      {
      for(int i=OrdersTotal()-1; i>=0; i--)
         {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==True)
            {
            GetSignals(true);
            if(OrderType()==OP_BUY && Signal==SELL &&
               OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
               {
               CloseOrders(BUY);
               OpenOrders(SELL);
               }
            if(OrderType()==OP_SELL && Signal==BUY &&
               OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
               {
               CloseOrders(SELL);
               OpenOrders(BUY);
               } 
            Print("HELP!");
            }
         else
            {
            Print("Could not SELECT trade!");
            GetSignals(false);
            if(Signal==SELL){OpenOrders(SELL);}
            if(Signal==BUY){OpenOrders(BUY);}
            }
         }
      Bar1Time=Time[1];
      }
   Comments();
return(0);
   }

So when erasing the "-1" from OrdersTotal() it will process both event of the order loop but it will not process either with ordertotal()-1..

What I am trying to get at is that I may not be doing it right and I wonder about order looping since I have not seen too many versions on MQL4 and none have counted down.

 

Total = OrdersTotal()

position = 0 to total - 1.

 

This piece of code will not print anything unless I REMOVE the  "-1" after OrdersTotal().

   if(Bar1Time!=Time[1])
      {
      for(int i=OrdersTotal()-1; i>=0; i--)
         {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==True)
            {
            GetSignals(true);
            if(OrderType()==OP_BUY && Signal==SELL &&
               OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
               {
               CloseOrders(BUY);
               OpenOrders(SELL);
               }
            if(OrderType()==OP_SELL && Signal==BUY &&
               OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
               {
               CloseOrders(SELL);
               OpenOrders(BUY);
               } 
            Print("Help!");
            }
         else
            {
            GetSignals(false);
            if(Signal==SELL){OpenOrders(SELL);}
            if(Signal==BUY){OpenOrders(BUY);}
            Print("Could not SELECT a trade!");
            }
         }
      Bar1Time=Time[1];
      }
 

The orders pool is just an array like any other, its first index is zero so the zero index will hold the first order in the pool.

There might be something going on in your other functions thats causing a problem so take out all your criteria stuff and run it like this with just these print statements.

In your for loop, change back and forth between Orders otal() and OrdersTotal()-1 You will see why it works the way it does.

int start() 
//----------------------------------------------------------------------------------------------------------
   {
   if(Digits==3){SymPoint=0.010; SymSpread=(Ask-Bid)*100;}
   if(Digits==5){SymPoint=0.00010; SymSpread=(Ask-Bid)*10000;}
   if(TrailingStart>=0 && TrailingStop>0){RunTrail();}
   if(Bar1Time!=Time[1])
      {
      Print("There are ",OrdersTotal()," Open Orders");
      for(int i=OrdersTotal()-1; i>=0; i--)
         {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==True)
            {
            Print("Order Found at Index ",i);
            }
         else
            {
            Print("No Order Found At Index ",i);
            }
         }
       Bar1Time=Time[1];
      }
   Comments();
   return(0);
   }
 
Subgenius:

Hi, Maybe someone could help me..

Trying to loop through open orders on every bar.

When there is one order open, my code does not see it, but when the orderstotal is one less -- then it sees the open order, but some reason there is a double execution of there being an order found and not.

For looping though orders total I have been counting down and from the MQL4 forum it seemed to count up

Have tried to create several different scenarios and search for what i am looking for from MQL4 but only got a headakeh

So when erasing the "-1" from OrdersTotal() it will process both event of the order loop but it will not process either with ordertotal()-1..

What I am trying to get at is that I may not be doing it right and I wonder about order looping since I have not seen too many versions on MQL4 and none have counted down.

Read this: Loops and Closing or Deleting Orders
 
Subgenius: This piece of code will not print anything unless I REMOVE the  "-1" after OrdersTotal()
  1. Of course it doesn't. It only prints if a orderSelect fails and by removing the -1 it always fails.

    Take all that stuff out of the loop. You can't do the else portion until after the loop completes.

    int nBuy = 0, nSell = 0
    for(int i=OrdersTotal()-1; i>=0; i--)
             {
             if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==True)
                {
    //          GetSignals(true);
                if(//OrderType()==OP_BUY && Signal==SELL &&
                   OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
                if(OrderType()==OP_BUY)  nBuy++; 
                if(OrderType()==OP_SELL) nSell++; 
    } }
    if(nBuy+nSell > 0){
                GetSignals(true);
                if(Signal==SELL)
                   {
                   CloseOrders(BUY);
                   OpenOrders(SELL);
                   }
                if(Signal==BUY)
                   {
                   CloseOrders(SELL);
                   OpenOrders(BUY);
    
                   }
    }
    else
                {
                GetSignals(false);
                if(Signal==SELL){OpenOrders(SELL);}
                if(Signal==BUY){OpenOrders(BUY);}
    

  2. && OrderComment()==OC)
    brokers can change comments, including complete replacement.
 

RaptorUK your forum post is excellent ( https://forum.mql4.com/48352 )

But, instead of testing the order-status and just continuing when orders are not found open, trying to put a nested if for it doesn't seem possible.

Am I trying something that has never been done before? Starting over from scratch using your example, i have this for you to see what i mean.

1. if you run this in ST, all that is printed is "Orders Total: 0"

2. if you erase -1 after TotalNumberOfOrders, then everything from the forloop can be seen in the journal/expert.

It goes against your post, hey but it seems to work atleast one way.. What is your take?

static datetime Bar1Time;
//----------------------------------------------------------------------------------------------------------
int start() 
//----------------------------------------------------------------------------------------------------------
if(Bar1Time!=Time[1])
      {
      int PositionIndex;    //  <-- this variable is the index used for the loop
      int TotalNumberOfOrders=OrdersTotal(); // <-- we store the number of Orders in the variable
      
      Print("Orders Total: " + OrdersTotal());
      
      for(PositionIndex=TotalNumberOfOrders-1; PositionIndex >= 0 ; PositionIndex --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
         {
         if( OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) 
            {
            Print("Orders are open at index = " + PositionIndex);
            GetSignals(true);
            if(OrderType()==OP_BUY && Signal==SELL &&
               OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
               {
               Print("Selected a buy order on index = " + PositionIndex);
               CloseOrders(BUY);
               OpenOrders(SELL);
               }
            if(OrderType()==OP_SELL && Signal==BUY &&
               OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==OC)
               {
               Print("Selected a sell order on index = " + PositionIndex);
               CloseOrders(SELL);
               OpenOrders(BUY);
               }
            }
         else
            {
            Print("No open orders at index = " + PositionIndex);
            GetSignals(false);
            if(Signal==BUY){OpenOrders(BUY);}
            if(Signal==SELL){OpenOrders(SELL);}
            }
         } //  end of For loop
      Bar1Time=Time[1];
      }
 
WHRoeder:
  1. Of course it doesn't. It only prints if a orderSelect fails and by removing the -1 it always fails.

    Take all that stuff out of the loop. You can't do the else portion until after the loop completes.


  2. brokers can change comments, including complete replacement.

I read those forum posts about order comments and it seems to me like the broker does change the order comment but only when the order has been closed to show like stopout, takeprofit, stoploss etc.

It seems to me that a customer would have a valid argument with their broker if they changed the comments of open orders.. Did you see my last post to RAPTORuk?

for-loop cannot execute if position index is -1 and thats why when removing the -1 it works.

      Print("Orders-Total: " + OrdersTotal()); //output=0

      Print("Max Position-Index: " + (OrdersTotal()-1) ); //output=(-1)

 
Subgenius:

RaptorUK your forum post is excellent ( https://forum.mql4.com/48352 )

But, instead of testing the order-status and just continuing when orders are not found open, trying to put a nested if for it doesn't seem possible.

Am I trying something that has never been done before? Starting over from scratch using your example, i have this for you to see what i mean.

1. if you run this in ST, all that is printed is "Orders Total: 0"

How many Strategy Tester Orders do you have open at the time ?  not Demo or Live Orders,  Orders open in the Strategy Tester ?
 
RaptorUK:
How many Strategy Tester Orders do you have open at the time ?  not Demo or Live Orders,  Orders open in the Strategy Tester ?

Actually two orders because my signal function includes 3 and 4 bar confirmations with it being set to 4. I should have seen just one order at a time.

If there there is an open order then there will be one order at a time but because the order select proceedure is not working I am seeing two orders maximum.

check PM

Reason: