how to distinguish closed s/l and not exists orders on the same price

 
int AnalyseOrders(int Type, //OrderType { example: OP_BUYSTOP }
                  double Price)// Order price we are searching for  
{
int Ticket = -1;

   for(int i=1; i<=OrdersTotal(); i++)                  // Order searching cycle
         {
         if (OrderSelect(i-1,SELECT_BY_POS)==true)      // If the next is available
            {                                           // Order analysis:
            //----------------------------------------------------------------------- 4 --
            if(OrderType()== Type &&                    // search for OrderType
               OrderSymbol()==Symbol())                 // search for symbol   EURUSD
            Price = OrderOpenPrice();                   // Order price
            if( OrderOpenPrice() == Price  )            //long position is opened
               {
               Ticket = OrderTicket();                  // Order ticket
               }
            }                                           //End of order analysis
         }//for                                         //End of order searching
   return(Ticket);
}

I use this function for search for pending orders.

Example:

1. I have OP_BUYSTOP at Price = 1.45000.

2. Market price activate that order.

3. I search with function for OP_BUY at Price = 1.45000 and than I modify that order placing "stoploss" at 1.45100.

4. Market price close that order at 1.45100 and go bellow 1.45000.

5. Then I want to place again OP_BUYSTOP at Price = 1.45000 using the same function but it shows me that OP_BUYSTOP exists at that price.

For closed orders it shows me s/l.

What I need to include in my Function to be able to distinguish closed and not exists orders on the same price?

 

#3 is wrong. Once the order opens the orderOpenPrice may not be exactly the original price (slippage). Instead just scan all open orders OrderType() <= OP_SELL and OrderStopLoss() == 0 and then set the stops.

    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderType()         <= OP_SELL // opened
    &&  OrderStopLoss()     == 0.      // no sl yet
    &&  OrderSymbol()       == Symbol() ){              // and my pair.
        orderModify...

Always count DOWN in the presence of multiple orders (multiple charts)

#6 closed orders will not be selected by the loop (only a orderselect(... MODE_HISTORY) can.) Likewise non-existing orders do not exist and can not be selected.

 
WHRoeder:

#3 is wrong. Once the order opens the orderOpenPrice may not be exactly the original price (slippage). Instead just scan all open orders OrderType() <= OP_SELL and OrderStopLoss() == 0 and then set the stops.

Always count DOWN in the presence of multiple orders (multiple charts)

#6 closed orders will not be selected by the loop (only a orderselect(... MODE_HISTORY) can.) Likewise non-existing orders do not exist and can not be selected.


Thanks I did correction to the function and now is count DOWN.

Problem is #5

just to be clear I will repeat the process:

Market price is bellow 1.45000

0. I want to check if exists order OP_BUYSTOP at Price = 1.45000 and it doesn't exist.
EA place the order OP_BUYSTOP.

1. Order OP_BUYSTOP at Price = 1.45000 exists
2. Market price activate that order OP_BUYSTOP ==> OP_BUY (OP_BUYSTOP doesn't exist at Price 1.45000 anymore)
3. I search with function for OP_BUY at Price = 1.45000 and than I modify that order placing "stoploss" at 1.45100
4. Market price close that order at "stoploss" = 1.45100 and go bellow 1.45000

5. Then I want to place again OP_BUYSTOP at Price = 1.45000. Market price is bellow 1.45000. Using the same function I want to check if exists order OP_BUYSTOP at Price = 1.45000. We know that is closed at #2. But it shows me that OP_BUYSTOP exists at that Price = 1.45000

I'm confuse why it is happening and what I'm missing... ?

 

Once opened OP_BUYSTOP at price 1.45000 will shows that order exist even if it was closed by S/L and if price goes up for example because OP_SELLSTOP it didn't exit before it will open that order at 1.45000 and after that it will shows that order exist even if it was closed by S/L .

So once opened OP_BUYSTOP or OP_SELLSTOP will show after forever that OP_BUYSTOP or OP_SELLSTOP exists on exact same price even if it was closed by S/L.

Is it bug in MT4 Build:399 or my function needs to check more parameters ?

int AnalyseOrders(int Type,      // OrderType { example: OP_BUYSTOP }
                  double Price)  // Order price we are searching for   1.45000
{
int Ticket = -1;

   for(int i=OrdersTotal()-1; i>=0; i--)                 // Order searching cycle int i=1; i<=OrdersTotal(); i++
         {
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)     // If the next is available i-1
            {                                            // Order analysis:
            //----------------------------------------------------------------------- 4 --
            if(OrderType()== Type &&                     // search for OrderType
               OrderSymbol()==Symbol()&&                 // search for symbol  npr. EURUSD
               OrderStopLoss() == 0. )                   // OrderStopLoss()==0
            Price = OrderOpenPrice();                    // Order price
            if( OrderOpenPrice() == Price  )             // 
               {
               Ticket = OrderTicket();                   // Order ticket
               }
            }                                            // End of order analysis
         }//for                                          // End of order searching
   return(Ticket);
}