Scan price range and add orders if missing.

 

Having trouble coding logic for checking Price Range and if any order(s) is missing on any price between range new order is sent.

 

Here I tried to make code to check range between buy order open price and buy order stoploss price and send buylimit orders to missing prices in that range. 

int      range;
double   price;
int      bMagic   = 111;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
int      tot=OrdersTotal();
double   bOpn;
double   bSL;

if(tot==0)
{
OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-25*Point,Ask+25*Point,0,bMagic,0,Green);
}

for(int data=tot-1;data>=0;data--)
{
   if(!OrderSelect(data,SELECT_BY_POS))continue;
   {
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==bMagic && OrderType()==OP_BUY)
      {
      bOpn  = OrderOpenPrice();
      bSL   = OrderStopLoss();
      }
   }
}

if(tot!=0 && tot<16 && bOpn!=0 && bSL!=0 && Ask-5>=bOpn)
{
range = (bOpn-bSL)*10000;

   for(int a=range-1;a>=0;a--)
   {
      if(!OrderSelect(a,SELECT_BY_POS))continue;
      {
         if(OrderSymbol()==Symbol() && OrderOpenPrice()!=bOpn-a*Point)
         {
         OrderSend(Symbol(),OP_BUYLIMIT,1,bOpn-a*Point,3,Ask-15*Point,Ask+15*Point,0,bMagic,0,Red);
         }
      }
   }
}
   
Comment("tot |",tot,
        "\nprice |",DoubleToStr(price,4));
        
   return(0);
  }
 
  • I'll recommend using functions to break your code into smaller parts
  • Create a function which checks for orders which lies within the range
  • Create a function which converts your points into integer pips for compare
  • Create a function which deals with 4/5 digits effectively
 
ubzen:
  • I'll recommend using functions to break your code into smaller parts
  • Create a function which checks for orders which lies within the range
  • Create a function which converts your points into integer pips for compare
  • Create a function which deals with 4/5 digits effectively


Good point!

I will try using break with this logic when order is missing.

Reason: