What code can be used to close orders at the end of the next bar period?

 

I am trying to write an EA that will close open order at the end of the next bar and have tried the following for the 4hr period:

--------------------------------------------------------------------

datetime a = TimeHour(OrderOpenTimeA);
string closestringA;
datetime closetimeA;


if ( a >= 0 && a < 4 )
{
closestringA = "7:59";
closetimeA = StrToTime(closestringA) + TimeDay(1);
}

closetimeA = StrToTime(closestringA);

if ( TimeCurrent() >= closetimeA ) {CloseBasketA = True;}

----------------------------------------------------------------------

This code will return the date as a day before the order opens and does not work for any timeframe.

There must be an easier/simpler way to code this for any timeframe. I would appreciate any suggestions.

 
int TotalBars = 0; //global var

if (Bars > TotalBars)
  {
  // do.........
  }
TotalBars = Bars;
 
qjol:

Thanks a bunch, you saved me a lot of time.