How to prevent new order to open at same price as Existing open orders.?

 

Please,  can anyone help me solve this.
I do not want it to open a new trade, at the exact same price as an exisiting open order.

I tryed this, but it keeps open new orders at the same price.
I have read a lot in the documentation, book and forum.

There must be something that I am missing. 

 

if(TradeAllowed() &&  ........... )
{ Open Trade
bool TradeAllowed()
  {
   
   for(int k=OrdersTotal()-1;k>=0;k--)
   {
      if((OrderSelect(k,SELECT_BY_POS,MODE_TRADES))&&(OrderSymbol()==Symbol())&&(OrderMagicNumber()==Magic))
      {
         if(MathAbs(OrderOpenPrice() - Bid) < Dist_Pips*Point) 
      {
      return(false); 
     }
   else return(true);
   }  
   }
   }
 
bool TradeAllowed() {
   for(int k=OrdersTotal()-1;k>=0;k--) {
      if((OrderSelect(k,SELECT_BY_POS,MODE_TRADES)) && 
       (OrderSymbol()==Symbol())&&(OrderMagicNumber()==Magic)) {
         if(MathAbs(OrderOpenPrice() - Bid) < Dist_Pips*Point) {
                return(false); 
         }
       }  
    }
    return(true);
   }

 Hi, you don't want to execute the line: else return(true) because if there are any open trades (early in the list) that have a different price, it will return true even if there are orders that have the same price as the Bid. In other words, allow it to run through and check all the orders to see if any meet your false condition, then if none of the open trades have caused the function to return false, then (and only then) return true. Try the above:

 
FXEZ:

 Hi, you don't want to execute the line: else return(true) because if there are any open trades (early in the list) that have a different price, it will return true even if there are orders that have the same price as the Bid. In other words, allow it to run through and check all the orders to see if any meet your false condition, then if none of the open trades have caused the function to return false, then (and only then) return true. Try the above:

Thank you very much.

So all my problems has been the "else"   I am so greatfull to you.

 
FXEZ:

 Hi, you don't want to execute the line: else return(true) because if there are any open trades (early in the list) that have a different price, it will return true even if there are orders that have the same price as the Bid. In other words, allow it to run through and check all the orders to see if any meet your false condition, then if none of the open trades have caused the function to return false, then (and only then) return true. Try the above:


Hi

You where so nice to help me with this issue.

Can you also give me a hint, how to only count the last 2 open positions. ?
I would be very greatfull.
 

Reason: