Download MetaTrader 5

EA Trade closing problem

To add comments, please log in or register
Add our channel to friends and follow the news!
Somsri Sarkar
2030
Somsri Sarkar 2016.11.03 05:11 

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. 

Keith Watford
Moderator
9601
Keith Watford 2016.11.03 09:37  
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.

Keith Watford
Moderator
9601
Keith Watford 2016.11.03 09:41  

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
Fstrifoerr8
36
Fstrifoerr8 2016.11.03 19:53  

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!!!");


}


 


 

Fernando Carreiro
2352
Fernando Carreiro 2016.11.03 19:59  
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.
Fstrifoerr8
36
Fstrifoerr8 2016.11.03 22:24  
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. 

Fernando Carreiro
2352
Fernando Carreiro 2016.11.03 22:54  
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!

Fstrifoerr8
36
Fstrifoerr8 2016.11.03 23:17  
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. 

Somsri Sarkar
2030
Somsri Sarkar 2016.11.05 05:24  
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

To add comments, please log in or register