How to check if there is an already open position near current market price?

I was trying to code on “is there any trade near by with in certain pips or not”

Does any body thinks that is it possible to check whether there is any opened position within the range of certain pips from current tick data.

I was trying for that and end up with nothing. If any one could suggest me the which one to use any function or operator or loop, I would be thank full.

Actually I am not asking for the code, Just the written suggestion of the way for coding in this case also works.

Of course it’s possible, show your code if you need coding help.


bool DTNAR() // this function name i gave randomly to try whether i could check that with in certain pip above and below the current price , is any order opened or not.
{

double DTNAiPtFc=100; // currently i gave this constant value, but later another function will return this value according to symbol,its in the pipae ( in case of 5 digit broker)
double buyprice,sellprice,pointDTNA,AskV,BidV,pacal;
buyprice=MarketInfo(Symbol(),MODE_ASK);
sellprice=MarketInfo(Symbol(),MODE_BID);
if(DTNAiPtFc>0) //i will be getting the value from another function so i added this condition to check whether the pipae id more than 0 or not
   {
      pointDTNA=MarketInfo(Symbol(),MODE_POINT);
      for (int z=0;z<=DTNAiPtFc;z++)
         {
         pical=pointDTNA*z;//when z= 2, the variable equals to Point of symbol * 2, if symbol with mini tick 0.00001, we get 0.00002
         AskV=buyprice+pical;// current ask + 0.00002
         BidV=sellprice-pical;//current bid - 0.00002
         if(AskV>0&&BidV>0)
            {
               for(int TrC=OrdersTotal()-1;TrC>=0;TrC--)
                  {
                     if(!OrderSelect(TrC,SELECT_BY_POS))continue;
                     if(OrderSymbol()!=Symbol())continue;
                     if(OrderType()!=OP_BUY||OrderType()!=OP_SELL)continue;
                     if(OrderOpenPrice()==AskV||OrderOpenPrice()==BidV) // check whether any opened order with price equal to AskV or BidV if yes returns true to whole function
                     return(true);
                                         
                  }
            }
         }
   }
   else return(false);
   
}



//I dont know whether it will work effeciently or not
//but i didnt get any other clue to code to check is any order opened within certain pips or not
//Previously i used limitation in opened order for same direction 
//for this case i was unable to imagine how to do, and end up with this

To checking for orders within a price range (inclusive) - use ‘>=’ && ‘<=’ rather than ‘==’.


 AskV=buyprice+pical;// current ask + 0.00002
         BidV=sellprice-pical;//current bid - 0.00002
         if(AskV>0&&BidV>0)
            {
               for(int TrC=OrdersTotal()-1;TrC>=0;TrC--)
                  {
                     if(!OrderSelect(TrC,SELECT_BY_POS))continue;
                     if(OrderSymbol()!=Symbol())continue;
                     if(OrderType()!=OP_BUY||OrderType()!=OP_SELL)continue;
                     if(OrderOpenPrice()<=AskV&&OrderOpenPrice()>=BidV) // check whether any opened order with price equal to AskV or BidV if yes returns true to whole function
                     return(true);
                                         
                  }
            }


bool DTNAR()
{

double DTNAiPtFc=100;
double buyprice,sellprice,pointDTNA,AskV,BidV,pacal;
buyprice=MarketInfo(Symbol(),MODE_ASK);
sellprice=MarketInfo(Symbol(),MODE_BID);
if(DTNAiPtFc>0)
   {       
      pointDTNA=MarketInfo(Symbol(),MODE_POINT);
      double z = DTNAiPtFc;
      pacal=pointDTNA*z;
      AskV=buyprice+pacal;
      BidV=sellprice-pacal;
      if(AskV>0&&BidV>0)
          {
               for(int TrC=OrdersTotal()-1;TrC>=0;TrC--)
                  {
                     if(!OrderSelect(TrC,SELECT_BY_POS))continue;Print ("success");
                     if(OrderSymbol()!=Symbol())continue;
                     if(OrderType()!=OP_BUY||OrderType()!=OP_SELL)continue;
                     if(OrderOpenPrice()<=AskV&&OrderOpenPrice()>=BidV)
                     return(true);
                  }
          }
      else 
      {
         return(false);
      }  
   }
else 
   {
   return(false);
   }
}

After compiling i got an error with message that “Not all control path returns a value”.

For functions with a return value (as opposed to ‘void’), it must return something every time it is called. So check through your codes - when you have ‘if’ and ‘else’, do they both contain a ‘return ()’ at the end? If not, does your function, right before it ends with a }, contains ‘return()’?

But anyway, I’ll give you the final answer :slight_smile: - you can remove highlighted parts:

                  }
          }
      else 
      {
         return(false);
      }  
   }
else 
   {
   return(false);
   }

Thanks a lot.

It really worked.

Hi, I see you still have this error:

if(OrderType()!=OP_BUY||OrderType()!=OP_SELL)continue;

needs to be:

if(OrderType()!=OP_BUY && OrderType()!=OP_SELL)continue;

Otherwise you will never get to the point where this function returns true.

 bool DTNAR() // this function name i gave randomly to try whether i could check that with in certain pip       pointDTNA=MarketInfo(Symbol(),MODE_POINT); ⋮          pical=pointDTNA*z;//when z= 2, the variable equals to Point of symbol * 2, if symbol with mini tick 0.00001, we get 0.00002

A PIP is not a Point.


if(OrderOpenPrice()==AskV||OrderOpenPrice()==BidV) // check whether any opened order with price equal to AskV or BidV if yes returns true to whole function

Doubles are rarely equal:

 double DTNAiPtFc=100; ⋮ if(DTNAiPtFc>0)

When will this ever be false?