Baffled

 

Hi all

This snippet of code is supposed to prevent opening a second order on this EA. When I test it on a single EA it works fine.

When I use it on a live demo with several other EA's on the same account it works for awhile and then fails.

if(NewBar()!= true){return(0);}                             //returns to start if this is not a new bar
    if(OrdersTotal() > 0)                                      // if there is an order Test for magic number
     {  for(int i=1; i<=OrdersTotal(); i++)
     OrderSelect(i-1,SELECT_BY_POS);
      if(OrderMagicNumber()==18){Print("got here");return(0);}         //only gets here if open order on this Ea
      }
     Print("OrdersTotal = ",OrdersTotal(), "i = ",i,"ordmagnum = ",OrderMagicNumber() );//should be no open order on this ea
     

If there is an open order for this EA (Magic Number = 18), then it should print "got here" and return to start.

Which it does sometimes, then stops and goes to the last line and outputs that print statement, which should only happen if there is no order or no order with magic number = to 18.

I have checked to make sure that there is no duplication of Magic numbers.

The for loop appears to work correctly.

Thoughts, suggestions, ideas, wild guesses all appreciated!!

Thanks

Keith

 
A suggestion. i=0; i<OrdersTotal(). Also, the if(NewBar() !=true) might be hanging it up. Take it out and see if it still gives you problems.
 
jmca wrote >>
A suggestion. i=0; i<OrdersTotal(). Also, the if(NewBar() !=true) might be hanging it up. Take it out and see if it still gives you problems.

Many thanks jmca, but no luck.

I tried your suggestions and also tried several variations on the iteration sequence, I did learn that in some cases not all of the open trades

will be selected. I suspect that the OrderSelect function is at the root of the problem.

I'm trying to figure out why in the tester with only one Ea and one currency it works, but with multiple EAs in the live demo it doesn't.

Alternately is there another way to limit an Ea to one open position, while not confusing it with open positions in other EAs?

Thanks again

keith

 

How many orders are you opening for this EA only at a time? If you're trying to get multiple orders, that for loop as it is will only find the 1st one it hits then break out of the loop. And I'm assuming you're putting the correct magic number in the OrderSend()? Try this, some of your syntax and brackets are iffy:


if(OrdersTotal() > 0){
   for(int i = 0; i < OrdersTotal(); i++){
      OrderSelect(i, SELECT_BY_POS);
   
      if(OrderMagicNumber() == 18){
         Print("got here");
         break;
      }
   }
   Print("OrdersTotal = ",OrdersTotal(), "i = ",i,"ordmagnum = ",OrderMagicNumber());
}
 
jmca wrote >>

How many orders are you opening for this EA only at a time? If you're trying to get multiple orders, that for loop as it is will only find the 1st one it hits then break out of the loop. And I'm assuming you're putting the correct magic number in the OrderSend()? Try this, some of your syntax and brackets are iffy:

Hey JMCA

Looks good. First to answer your questions, yes just one open position at a time. Yes I have checked (believe me!!!) that OrderSend has the correct magic number.

I ran your code in the tester and found that I had to change "break" to "return(0)", since break just stops the loop but allows control to pass through to the following code, whereas I need to return to start.

Having made the change I find that it works in the tester and so far in the live demo it has worked, only three bars so far and I have been that far before, so I'm keeping my fingers crossed.

thanks again, I know how difficult it is to puzzle out other peoples logic and code practices and I thank you for your valuable time.

keith

PS>I'll let you know how it worked overnight.

 

PS>I'll let you know how it worked overnight.

Yup!!

Works like a charm

thanks

Keith

Reason: