Help to auto close order when new bar opens

 

Hi, I want to close old order when new bar is opened. I tried to code it, but it doesn't work. Please tell me what its wrong. Thank you.

double last_bar;

int init()
{
//----
   last_bar = Open[1];
//----
   return(0);
}
//------------------------------------------------------------------

int start()
{
if(order_total < 1)
   {
      if(last_bar != Open[1])       
         {
            last_bar = Open[1];
            Close_order();            
         }
      else
         {
            //Code to open orders
         }
      
   }
return(0);
}
//------------------------------------------------------------------

void Close_order()
{

   for (int i=OrdersTotal()-1; i>=0; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
      if (OrderType()==OP_BUY && Close[0] < Open[0]) OrderClose (OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage);
      if (OrderType()==OP_SELL && Close[0] > Open[0]) OrderClose (OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage);
   }
   
}
 

If you have an order open, nothing will happen (assuming that order_total is OrdersTotal() )

if(order_total < 1)
 
if(order_total < 1)
   {
      if(last_bar != Open[1])       
         {
            last_bar = Open[1];
            Close_order();            
         }
you can't close trades if there isn't one .....
 
Can anyone help me?
 
tvh168:
Can anyone help me?

This is my new code. I want order close when price reverse. Ex: I put order Buy. On new bar, if price reverse (Close[0] < Open[0]), order is auto close.
double time_bar_curr;

int init()
{
//----
   time_bar_curr = Time[1];
//----
   return(0);
}
//------------------------------------------------------------------

int start()
{
Close_order();
if(order_total < 1)
   {
      
     //Code to open orders
      
   }
return(0);
}
//------------------------------------------------------------------

void Close_order()
{
   int i;
   if(time_bar_curr != Time[1])
   {
      if(Close[0] < Open[0])
      {
         for (i=OrdersTotal()-1; i>=0; i--)
         {
            if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
            if (OrderType()==OP_BUY) OrderClose (OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage);
         }
      }
      if(Close[0] > Open[0])
      {
         for (i=OrdersTotal()-1; i>=0; i--)
         {
            if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
            if (OrderType()==OP_SELL) OrderClose (OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage);
         }
      }
      time_bar_curr = Time[1];
   }
}
 
tvh168:

This is my new code. I want order close when price reverse. Ex: I put order Buy. On new bar, if price reverse (Close[0] < Open[0]), order is auto close.
At the start of a new bar Open == Close == High == Low for Bar 0 . . . and by the way, Close[0] == Bid
 
RaptorUK:
At the start of a new bar Open == Close == High == Low for Bar 0 . . . and by the way, Close[0] == Bid


Hi, can you explain more?
 
tvh168:

Hi, can you explain more?
What more is there to explain ? try thinking a little what is the Open price of a new bar for the first tick ? what is the close for the first tick ? etc, etc
 
Perhaps when you say "new bar, if price reverse" you mean Close[1] < Open[1]
Reason: