This code is designed to close an open order at the end of the day. Note that at no time is there more then one open order at a time. The problem is the code when ran with the Strategy Tester will close the first order at 2300 hours but any other orders placed on the following days will not close at 2300 hours. Thoughts?
//Close order at days end.
int total = OrdersTotal();//Collects the total of open orders.
if(OrderSelect(total, SELECT_BY_TICKET)==true)//select the first Order
{
if(OrderCloseTime() == 0 && OrderType() == OP_SELL && TimeHour(TimeLocal()) >= 23)//Is there an open order, is it a buy, is hour 23
{
CloseLots = OrderLots();//Number of lots in the order.
ClosePrice = Ask;//Current ask price.
RefreshRates();//Refreshing the rates
//Print("total ",total,", CloseLots ",CloseLots,", ClosePrice (ask) ",ClosePrice,", UseSlippage ",UseSlippage);
OrderClose(total,CloseLots,ClosePrice,UseSlippage,Red);//Close the order using the above data.
}
if(OrderCloseTime() == 0 && OrderType() == OP_BUY && TimeHour(TimeLocal()) >= 23)//Is there an open order, is it a buy, is hour 23
{
CloseLots = OrderLots();//Number of lots in the order.
ClosePrice = Bid;//Current Bid price.
RefreshRates();//Refreshing the rates
//Print("total ",total,", CloseLots ",CloseLots,", ClosePrice (bid) ",ClosePrice,", UseSlippage ",UseSlippage);
OrderClose(total,CloseLots,ClosePrice,UseSlippage,Red);//Close the order using the above data.
}
}
//Close order at days end.
int total = OrdersTotal();//Collects the total of open orders.
if(OrderSelect(total, SELECT_BY_TICKET)==true)//select the first Order
{
if(OrderCloseTime() == 0 && OrderType() == OP_SELL && TimeHour(TimeLocal()) >= 23)//Is there an open order, is it a buy, is hour 23
{
CloseLots = OrderLots();//Number of lots in the order.
ClosePrice = Ask;//Current ask price.
RefreshRates();//Refreshing the rates
//Print("total ",total,", CloseLots ",CloseLots,", ClosePrice (ask) ",ClosePrice,", UseSlippage ",UseSlippage);
OrderClose(total,CloseLots,ClosePrice,UseSlippage,Red);//Close the order using the above data.
}
if(OrderCloseTime() == 0 && OrderType() == OP_BUY && TimeHour(TimeLocal()) >= 23)//Is there an open order, is it a buy, is hour 23
{
CloseLots = OrderLots();//Number of lots in the order.
ClosePrice = Bid;//Current Bid price.
RefreshRates();//Refreshing the rates
//Print("total ",total,", CloseLots ",CloseLots,", ClosePrice (bid) ",ClosePrice,", UseSlippage ",UseSlippage);
OrderClose(total,CloseLots,ClosePrice,UseSlippage,Red);//Close the order using the above data.
}
}