Backtest Freeze , Orderdelet error 145

Back to topics list To post a new topic, please log in or register
avatar
3
noname2x 2015.12.30 00:01
 

I wrote a small code to delete all order . At first it looks like this :

void Close_Grid()
{
            for (int i = OrdersTotal()-1; i>=0;i--)
               {
                  if (OrderSelect(i,SELECT_BY_POS) == True)
                     {
                        if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magicnumber))
                           {  
                              if(OrderType()>1)
                                 Grid_Close = OrderDelete(OrderTicket());
                              else
                                 Grid_Close = OrderClose(OrderTicket(),OrderLots(),Ask,3);                     
                           }
                     }
               }   
}


Then when forward test , i saw that this code didn't close all the order . There always are 1-2 Orders that are still open. So i rewrote to this :

void Close_Grid()
{
   while(Order_Scan())
   for (int i = OrdersTotal()-1; i>=0;i--)
   {
      if (OrderSelect(i,SELECT_BY_POS)) 
         if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magicnumber))
         {
            if(OrderType()>1)
               Grid_Close = OrderDelete(OrderTicket());
            else
               Grid_Close = OrderClose(OrderTicket(),OrderLots(),Ask,3);        
         }
    }   
}
//+------------------------------------------------------------------+
//| Order_Scan                                                       |
//+------------------------------------------------------------------+
bool Order_Scan()
{
   int Order_Count = 0;
   bool Order_Exist =false;
   
   if (OrdersTotal() > 0)
      for (int i = OrdersTotal()-1; i>=0;i--)
      {
         if (OrderSelect(i,SELECT_BY_POS)) 
            if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magicnumber))
               Order_Count++;
      }
   else
      Order_Count = 0;
         
   if (Order_Count >0)
      Order_Exist = true;
   else
      Order_Exist =false;
      
   return(Order_Exist);   
}


So this time, the backtest freeze and show error 145. I knew what error 145 mean and why it happend. My question is how to rewrite the code so that if the price is so close to the market and it can't delete the order. It will have to wait or by pass and check again later for closing the order . Thanks



avatar
Moderator
3404
GumRai 2015.12.30 00:59 #
 

I think that in the strategy tester, there will be no new tick until the code is executed, so your code will create an endless loop.

In real time you may at least require a RefreshRates() between attempts to delete the orders.

It is probably better to wait for a new tick before a new attempt

static bool need_to_close_orders=false;
//Code that determines orders should be closed
  {
   need_to_close_orders=true;
  }
if(need_to_close_orders)
//Code to close orders
need_to_close_orders=Order_Scan();

 Your function can be simplified

bool Order_Scan()
  {
   int Order_Count=0;

   if(OrdersTotal()==0)
      return(false);
   else
   for(int i=OrdersTotal()-1; i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
         if((OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magicnumber))
            Order_Count++;
     }

   return(Order_Count>0);
  }

 

avatar
3
noname2x 2015.12.31 01:03 #
 
Thanks so much , i solved the problem with your solution , how didn't i figure it out sooner, so simple :D
Back to topics list  

To add comments, please log in or register