Scanning Existing Orders before Placing New Order

 

Hi, I have a function called CheckOpenOrders() as below. What its supposed to do is to check whether the current Bid price matches any existing orders in a 50-pip window before I open an order. However, somehow it doesn't seem to work and the code ends up opening many orders regardless whether there are existing ones that have already been opened around the price level.

Is there something wrong with my logic or the way I'm using the APIs?

int CheckOpenOrders()
  {
   for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderMagicNumber()==token){
         double norm_order_price = NormalizePrice(OrderOpenPrice());
         double upper_bound = norm_order_price + 0.005;
         if(Bid>norm_order_price && Bid<=upper_bound) return 0;  // If there are any existing orders, escape to main loop
      }
     }
    
    return 1;
  }

double NormalizePrice(double price)
{
   double norm_price = MathFloor(price/0.005)*0.005;
   return NormalizeDouble(norm_price,5);
}
 
jlyh:

Hi, I have a function called CheckOpenOrders() as below. What its supposed to do is to check whether the current Bid price matches any existing orders in a 50-pip window before I open an order. However, somehow it doesn't seem to work and the code ends up opening many orders regardless whether there are existing ones that have already been opened around the price level.

Is there something wrong with my logic or the way I'm using the APIs?

Hello, if you like my idea, you can use this function to check how pip by using Ask/Bid Mode

double TotalPips ()
{
   double totalpips,pips;
   totalpips=0;
   pips=0;
   for(int i=0;i<OrdersTotal();i++) 
   {

      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
       if(OrderSymbol()!=Symbol()) continue;
      double point=MarketInfo(OrderSymbol(),MODE_POINT);
      if(OrderType()==OP_BUY)
         pips=(MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/point;
      if(OrderType()==OP_SELL)
         pips=(OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/point;   

   }
   return (pips);
}
so if my function which I share you, can be connected to your function, I think not impossible for you to use on your logic... Happy Coding brother
 
Achmad Wijaya:

Hello, if you like my idea, you can use this function to check how pip by using Ask/Bid Mode

so if my function which I share you, can be connected to your function, I think not impossible for you to use on your logic... Happy Coding brother
Hi Achmad, not exactly what i was looking for but thanks anyway. Some errors i noticed in your code:
(1) if you do a value assignment to pips for every loop, i think the final value of pip will only be whatever is the last order and not the total number of pips earned.
(2) 1 pip is 10 points so by dividing by point you actually get 10 times the actual number of pips
 
jlyh: (2) 1 pip is 10 points so by dividing by point you actually get 10 times the actual number of pips

1 pip is 10 point only on a 5 digit broker. Code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.

 
whroeder1:

1 pip is 10 point only on a 5 digit broker. Code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points,) and metals. Compute what a PIP is and use it, not points.


Thanks for the tip whroeder1! Though, would be great if there's anything at all you would be able to advise on my original question :)

 
jlyh:

Thanks for the tip whroeder1! Though, would be great if there's anything at all you would be able to advise on my original question :)

This is for the forex majors, not CFD and exotics. (although it would work on most)

//+------------------------------------------------------------------+
//|                                                  50pipWindow.mq4 |
//|                                                      nicholishen |
//|                                   www.reddit.com/u/nicholishenFX |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "www.reddit.com/u/nicholishenFX"
#property version   "1.00"
#property strict

input int Magic = 0;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   string yes = "The Bid is currently in the pip window. No trades will be placed.";
   string no  = "The Bid is NOT in the window, ok to place trades.";
   Print(IsBidInPipWindow(50) ? yes : no);
}
//+------------------------------------------------------------------+

bool IsBidInPipWindow(const double num_pips_in_window)
{
   double pip= _Digits==3 || _Digits==5 ? _Point * 10 : _Point;
   for(int i=0;i<OrdersTotal();i++)
      if(OrderSelect(i,SELECT_BY_POS)&&(Magic==0||OrderMagicNumber()==Magic)&&OrderSymbol()==Symbol()) 
         if(NormalizeDouble(MathAbs(OrderOpenPrice()-Bid)/pip,1) <= num_pips_in_window)
            return true; 
   return false;
}
 

Works great, thanks @nicholishen!!