script to close orders once and only when new bar detected

 

Hi,

I am trying to create a simple script that closes all existing orders once and only when a new bar is detected.
But my code below always get executed even when new bar is same as old bar.
Thanks to help.


static int old_bars = 0; // remember the amount of bars already known 
if (old_bars != Bars) // if a new bar is received 
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)==tr ue)
{
int ticket=OrderTicket();
double lots=OrderLots();
OrderClose(ticket,lots,Ask,3);
}
}
} old_bars = Bars; // remember how many bars are known
 
static int old_bar = Time[0]
if (Time[0] != old_bar)
   {
   bla bla bla
   }
 
philtrader:

Hi,

I am trying to create a simple script that closes all existing orders once and only when a new bar is detected.
But my code below always get executed even when new bar is same as old bar.
Thanks to help.

OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3*VALUE);

MAKE    VALUE = 1    4 digit 

For 5 digit notation brokers VALUE = 10
 
qjol:

Hi, Thanks for your answers I got errors trying the script below, could you kindly explain. Thanks. 

 

static int old_bar = Time[0]

int start()
{

  while(true) 
  { 
   if (Time[0] != old_bar)
       {  
         for (int i = OrdersTotal() - 1; i >= 0; i--)
               {
                  if(OrderSelect(i,SELECT_BY_POS)==true)
                  {
                     int ticket=OrderTicket();
                     double lots=OrderLots();
                     OrderClose(ticket,lots,Ask,3);
                  }
               }
       } 
   }

}
 
philtrader:

Hi, Thanks for your answers I got errors trying the script below, could you kindly explain. Thanks. 

Time[]  returns a datetime not an int

if old_bar is globally declared it doesn't need to be static 

Once you have a new bar Time[0] will always  !=  old_bar because you never set   old_bar = Time[0]  

 
RaptorUK:

Time[]  returns a datetime not an int

if old_bar is globally declared it doesn't need to be static 

Once you have a new bar Time[0] will always  !=  old_bar because you never set   old_bar = Time[0]  


Ok, I think I'm getting closer, but still it gets inside the For the first time but would not close orders at the new coming bar. This what I do:

Again thanks for your help. 

datetime old_bar = 0;

int start()
{
  
  while(true) 
  { 
   
  
   if (Time[0] != old_bar)
       {  
         for (int i = OrdersTotal() - 1; i >= 0; i--)
               {
                  if(OrderSelect(i,SELECT_BY_POS)==true)
                  {
                     int ticket=OrderTicket();
                     double lots=OrderLots();
                     OrderClose(ticket,lots,Ask,3);
                  }
               }
       } old_bar = Time[0];  
   } 

}
 
philtrader:


Ok, I think I'm getting closer, but still it gets inside the For the first time but would not close orders at the new coming bar. This what I do:

Yes,  because you always set old_bar equal to Time[0],  you need to do this . . . 

datetime old_bar = 0;

int start()
{
  
  while(true) 
  { 
   
  
   if (Time[0] != old_bar)
       {  
         for (int i = OrdersTotal() - 1; i >= 0; i--)
               {
                  if(OrderSelect(i,SELECT_BY_POS)==true)
                  {
                     int ticket=OrderTicket();
                     double lots=OrderLots();
                     OrderClose(ticket,lots,Ask,3);
                  }
               }
       old_bar = Time[0];   //  <--- moved
       }  
   } 

}
 

Hi again,

Many thanks for your patience, I have copied the exact script and it does close the opened orders when I attach the script to my minute chart. But when I  place new orders before the end of the bar and when the current bar closes it would not close the newly opened orders. This is very strange as I understand your code is clear and simple enough. Anything I could have missed ?

 
philtrader:

Hi again,

Many thanks for your patience, I have copied the exact script and it does close the opened orders when I attach the script to my minute chart. But when I  place new orders before the end of the bar and when the current bar closes it would not close the newly opened orders. This is very strange as I understand your code is clear and simple enough. Anything I could have missed ?

Are they Pending orders ?
 
RaptorUK:
Are they Pending orders ?


No, they are just buy / sell orders executed at market. Could it be something with my charts or options settings ?
 
philtrader:

Hi again,

Many thanks for your patience, I have copied the exact script and it does close the opened orders when I attach the script to my minute chart. But when I  place new orders before the end of the bar and when the current bar closes it would not close the newly opened orders. This is very strange as I understand your code is clear and simple enough. Anything I could have missed ?


1.2. Scripts
Scripts are very similar to Expert Advisors, but their features are a bit different. Main feautres of scripts are listed below:
1. Scripts can use the functions of trades, too.
2. Parameters of external settings cannot be changed in scripts.
3. The main feature of scripts is the rule, according to which special function start() of a script will be launched only once, immediately after it has been attached to the chart and initialized.

 and why not do ...

OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3*VALUE);

MAKE    VALUE = 1    4 digit 

For 5 digit notation brokers VALUE = 10
Reason: