# CurrentTime & OrderOpenTime Compare?

507

In this EA, It opens order on each hourly bar & closes with it. It closes in 60 mins.

if ( T11 ==0 && T1 == 0 && BuyTicket == 0 )
{
{
StopLoss = low - (SecondSL * UsePoint);
TakeProfit = NormalizeDouble(( MathRound((MathAbs(OpenPrice - StopLoss))* CalcPoint1))*2,6);
double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);

SellTicket = 0;

//-----close trade after 60 mins //----

{
datetime order_open_time = OrderOpenTime();
datetime k = order_open_time + 3600;

if(TimeCurrent()>=k)
{
double CloseLots = LotSize;
double ClosePrice = Bid;

}
}
}
}

In above code, it never close the trade after 60 mins, cause Timecurrent() is never getting above k or equal it.  How to solve this?

Any help will be appreciated.  Thank you

16096

Of course not. You just opened it, so they're just about equal.

On the next tick BuyTicket is not zero, and the first IF says to do nothing.

507

WHRoeder:

Of course not. You just opened it, so they're just about equal.

On the next tick BuyTicket is not zero, and the first IF says to do nothing.

Thank you for your reply. Then how can I close trade after 60 minutes. I tried doing it using following codes found from other thread. But that doesn't worked too.

int maxDuration = 60 * 60;
{
datetime order_open_time = OrderOpenTime();
int duration = (int)TimeCurrent() - (int)order_open_time;

if(duration >= maxDuration)
{
double CloseLots = LotSize;
double ClosePrice = Bid;

}
}

Lastly I don't understand about orderselect. If I want to open buy order or close buy order, then what should be the orderselect "BuyTicker or Sellticket" and what will be it "true" or "false" ?

if ( T11 ==0 && T1 == 0 && BuyTicket == 0 )

In above line if i changed it to SellTicket == 0 then will it be all right?

507

int duration = (int)TimeCurrent() - (int)order_open_time;

This particular code shows data as 1436767200. That's why its always greater than 3600.

16096

1. Simplify
int duration = int(TimeCurrent() - order_open_time);
2. 1436767200 = Mon, 13 Jul 2015 06:00:00 GMT Therefor the order had not yet opened (OOT == 0.) Selecting by ticket means you must test for open and/or for close.
3. EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a orderSelect loop to recovery, or persistent storage (GV/file) of ticket numbers required.
507

WHRoeder:
1. Simplify
2. 1436767200 = Mon, 13 Jul 2015 06:00:00 GMT Therefor the order had not yet opened (OOT == 0.) Selecting by ticket means you must test for open and/or for close.
3. EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a orderSelect loop to recovery, or persistent storage (GV/file) of ticket numbers required.

Thank you for the code. It worked...I understood my mistake.

//------WHRoeder CODE for Order Close -------
int maxDuration = 60 * 60; // 60 minutes
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--)
if (OrderSelect(pos, SELECT_BY_POS)  &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol() )
{
int duration = int(TimeCurrent() - OrderOpenTime());
if (duration >= maxDuration)
bool closed = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),UseSlippage,Blue);
}