EA Trade closing problem

 

Hello Members,

Here is the code for testing a binary robot:

// Trade operation ----------------------------------//
   if(OrdersTotal() == 0 && D_B > 0 )
   BuyTicket = OrderSend(Symbol(),OP_BUY,lot,Ask,(int)CalcSlippage,0,0,"BUY",MagicNumber,0,clrGreen);
   if(OrdersTotal() == 0 && D_B < 0 )  
   SellTicket = OrderSend(Symbol(),OP_SELL,lot,Bid,(int)CalcSlippage,0,0,"Sell",MagicNumber,0,clrRed);
        
        
   //------closing Buy-----------
    for (int b = OrdersTotal()-1;b>=0;b--)
  {
     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol())
     {
       if (OrderType() == OP_BUY && TimeCurrent() == iTime(Symbol(),PERIOD_D1,0)+86399)
      bool closed = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),(int)CalcSlippage,clrBlue);
    
      
      
    }
  }

      
  //------closing Sell-----------
   for (int b = OrdersTotal()-1;b>=0;b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol())
     {
       if (OrderType() == OP_SELL && TimeCurrent() == iTime(Symbol(),PERIOD_D1,0)+86399)
       bool closed = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),(int)CalcSlippage,clrBlue);
  
      
       }
    }

 But when I backtest it, I get the followings:

trade is not closing on the same day, but many days after why is that? I backtesing on Hourly chart tick by tick.

Please help.

Thank you. 

 
if (OrderType() == OP_BUY && TimeCurrent() == iTime(Symbol(),PERIOD_D1,0)+86399)

You are trying to close in the very last second of the day. If a tick is not replicated for exactly that time, the trade will not be closed

if (OrderType() == OP_BUY && TimeCurrent() >= iTime(Symbol(),PERIOD_D1,0)+86340)

would close it in the last minute, but still not ideal as there is always a chance that a tick will not be generated in the last minute.

 

Why 2 loops when the checks are exactly the same?

 

  for (int b = OrdersTotal()-1;b>=0;b--)
  {
     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol())
     {
      if (TimeCurrent() >= iTime(Symbol(),PERIOD_D1,0)+86340)
      bool closed = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),(int)CalcSlippage,clrBlue);
    }
  }
You only need the one loop
 

To chek exectly time use construction like this. (use TimeLoca,not TimeCurrent)

bool first_tick_chaked=false;
datetime TimeLoca;
int time_shift;


////////////////////////////////////////////////////////////
void OnTick()
{
if(!first_tick_chaked)
{
first_tick_chaked=true;
EventSetTimer(1);
time_shift=int(TimeLocal()-TimeCurrent());
}

if(IsTesting()) All();
}
////////////////////////////////////////////////////////////
void OnTimer()
{  
All();

  }


///////////////////////MAIN////////////////////////
void All()
{
//loca time
TimeLoca=TimeLocal();

//your code

//if(TimeLoca-time_shift==D'23:59:59') Alert("Time!!!");


}


 


 

 
Fstrifoerr8:

To chek exectly time use construction like this. (use TimeLoca,not TimeCurrent)

Since the OP seems to be trying to synchronise the close with the end of the broker's day (probably, to prevent paying swap costs), it makes no sense to use Local Time.
 
Fernando Carreiro:
Since the OP seems to be trying to synchronise the close with the end of the broker's day (probably, to prevent paying swap costs), it makes no sense to use Local Time.

TimeLoca-time_shift=exectly time of terminal

be attentive. 

 
Fstrifoerr8:

TimeLoca-time_shift=exectly time of terminal

be attentive. 

That is not always reliable, because one needs to know the time difference, which may need "human" intervention to set-up, especial during Daylight Savings change-over, which a "human" may forgot to correct. Even if the PC does correct for DST, it may not be the same DST used by the broker.

It is best to always use the Broker's time, namely TimeCurrent(), and if need be, due to the lack of liquidity (no incoming ticks) as Keith correctly identified, the OP can always the use an OnTimer() event (irrespective of local time) to execute a close at a precise time, irrespective of local time difference or lack of ticks.

EDIT: You are correct! After analysing your code more closely, I see that you are in fact automatically correcting for changes! My apologies!

 
Fernando Carreiro:

That is not always reliable, because one needs to know the time difference, which may need "human" intervention to set-up, especial during Daylight Savings change-over, which a "human" may forgot to correct. Even if the PC does correct for DST, it may not be the same DST used by the broker.

It is best to always use the Broker's time, namely TimeCurrent(), and if need be, due to the lack of liquidity (no incoming ticks) as Keith correctly identified, the OP can always the use an OnTimer() event (irrespective of local time) to execute a close at a precise time, irrespective of local time difference or lack of ticks.

EDIT: You are correct! After analysing your code more closely, I see that you are in fact automatically correcting for changes! My apologies!

It is ok.

Happy that you understood my code. 

 
Keith Watford:

Why 2 loops when the checks are exactly the same?

 

  for (int b = OrdersTotal()-1;b>=0;b--)
  {
     if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES) == true &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol())
     {
      if (TimeCurrent() >= iTime(Symbol(),PERIOD_D1,0)+86340)
      bool closed = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),(int)CalcSlippage,clrBlue);
    }
  }
You only need the one loop

Thank you Keith Watford, Your coding idea worked like a charm.

Also thank you others for sharing your valuable input.

Regards

Reason: