If in trade return(0)

 

Please help. I am trying to stop my robot from entering two trades at once. Below is a simplified block diagram of what I am trying to accomplish, as well as the code I am using. The code doesn’t work. What am I doing wrong? Is it the use of a return (0) that is wrong? If so, why? Do I need to do something different like put the “Place order” inside an if statement? E.g. if(OrderMagicNumber()!=MagicNumber) Place Order;

Brewmanz I know you tried to help the other day, but I did not understand . Thank you.

// Is this Robot currently in a trade
   for(i=0;i<OrdersTotal();i++)
     {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderMagicNumber()==MagicNumber ) return(0); 
     }
 
int orderCount(){
    int count=0;
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        count++;
    }
    return(count);
}
...
if (orderCount() == 0){ //open new
 
WHRoeder:

I understand your method. Thank yoy. If you know could you please explain why my code doesn't work? I use return(0) a lot to stop code from proceeding. But it doesn't seem to work in this case. Where does the logic break down? Let me give you an example. I was having trouble closing all pending orders. In my code I was counting up in my for() statement. Brewmanz point out that when order position 0 is deleted, the order in position 1 moved to position 0 and that I need to count down in my for() statement instead of up. So, if you (or any one else) can explain why it doesn't work I would be grateful. . Please don't get me wrong, I do appreciate your input and I will incorporate your change. But, I would still like to know why? Thanks for you insight.

 
post the rest of the code
 

tparimore, the problem with using return(0) to guide your logic is that it returns from the start() function when called.. therefore none of the code you have below the point where it is called will be executed.. its a very broad and indiscriminate approach to driving your algorithm and this may be part of your problem.

In regards to the code you posted:

   for(i=0;i<OrdersTotal();i++)
     {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderMagicNumber()==MagicNumber ) return(0); 
     }

This can work but it depends where it's placed but has some major drawbacks. for one, it's returning on the basis that an order has the specified magic number but what you really want is to return on the basis of there being an open order.. why have this degree of separation? Just state: if(OrdersTotal()>0) return(0);

This is still very ad-hoc and will lead to issues. If you place it towards the beginning of your code, the EA will return and not execute any of the code below.. perhaps you have some to monitor exit rules, etc.. if you are compelled to use this sort of method, you need to be very careful with the placing of this code. If you place it after all of your order closing/open order monitoring code but before the order open code, this should work. The code WHRoeder posted is much more robust however.

 
supertrade:

tparimore, the problem with using return(0) to guide your logic is that it returns from the start() function when called.. therefore none of the code you have below the point where it is called will be executed.. its a very broad and indiscriminate approach to driving your algorithm and this may be part of your problem.

In regards to the code you posted:

This can work but it depends where it's placed but has some major drawbacks. for one, it's returning on the basis that an order has the specified magic number but what you really want is to return on the basis of there being an open order.. why have this degree of separation? Just state: if(OrdersTotal()>0) return(0);

This is still very ad-hoc and will lead to issues. If you place it towards the beginning of your code, the EA will return and not execute any of the code below.. perhaps you have some to monitor exit rules, etc.. if you are compelled to use this sort of method, you need to be very careful with the placing of this code. If you place it after all of your order closing/open order monitoring code but before the order open code, this should work. The code WHRoeder posted is much more robust however.


Supertrade, thank you for your input. You are right it will also return (0) on pending orders. that is ok. I do want it to stop at this point if in a trade. I use a TradeOffered flag to stop multiple trades on a given bar. Pending orders are closed and TradeOffered flag is reset to no, on a new bar. This part seems to be working. Upon new bar, only actual trades should be open. And the "code above" is to keep from opening a second trade. However, it is getting pass the above code? very strange. In all my trades I use a SL and a TP, so exits are no problem. Seems as through it should work. However, i will use the if statement to set a InTrade flag an a if (InTrade != "yes") around the open order portion, as suggest by WHRoeder. thanks to everyone for their input.
 
tparimore:

Please help. I am trying to stop my robot from entering two trades at once. Below is a simplified block diagram of what I am trying to accomplish, as well as the code I am using. The code doesn’t work. What am I doing wrong? Is it the use of a return (0) that is wrong? If so, why? Do I need to do something different like put the “Place order” inside an if statement? E.g. if(OrderMagicNumber()!=MagicNumber) Place Order;

Brewmanz I know you tried to help the other day, but I did not understand . Thank you.


One last thought. Are there any limits to a magic number? I am using 22115 for the robot. 2 = second program, 2=EUR, 1=USD, 15=15 min chart. If I have exceeded the maximum allowable digits, maybe the order is returning a different number.
 
tparimore:

One last thought. Are there any limits to a magic number?
32 bit numbers = 2147483647 I'm using 20110202 with no problems
 
WHRoeder:
32 bit numbers = 2147483647 I'm using 20110202 with no problems

As I use the magic numbers to close pending order, I didn't think that was the issue. Thanks for confirming.