i am doing 1 buy stop and 1 sell stop order every 10am daily. and close both the next day whether open or pending. but when run run backtest, the pending one not opened cant close b4 the next 10pm. if there something wrong with my closing order code?
if (Hour()== StartHour)
{
if(FirstTick == true)
{
FirstTick = false;
Alert("Acc Bal is ", AccountBalance());
double HighestHigh=-1, LowestLow =999;
//check for highest high and lowest low
for(int i = 1; i<=NoOfBars; i++)
{
if(High[i] > HighestHigh)
HighestHigh = High[i];
if(Low[i] < LowestLow)
LowestLow = Low[i];
}
Alert("u r using ", TimeChart, " chart, checking for ", NoOfBars, " bars");
Alert("Highest high for the last ", NoOfBars," bars is ", HighestHigh);
Alert("lowest low for the last ", NoOfBars," bars is ", LowestLow);
//check for any opening order. if yes, close
int TotalOrders = OrdersTotal();
for(int j=TotalOrders-1; j>=0; j--)
{
bool OpenOrder = OrderSelect(j, SELECT_BY_POS); //select by order pool (open and pending order only)
int Type = OrderType();
bool CloseResult = false;
if(Type == OP_BUYLIMIT == OP_SELLLIMIT == OP_BUYSTOP == OP_SELLSTOP) CloseResult = OrderDelete(OrderTicket());
if(Type == OP_BUY) CloseResult = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 100, Red );
if(Type == OP_SELL) CloseResult = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 100, Red );
if(CloseResult==false) Alert("Order " , OrderTicket() , " failed to close. Error: " , GetLastError() );
if(CloseResult==true) Alert("Order " , OrderTicket() , " successfully closed! ");
}
int ticket1 = OrderSend(Symbol(), OP_BUYSTOP, Lots, HighestHigh + 10 * Point * 10, 100, LowestLow, HighestHigh + TakeProfit * Point * 10, "Buy Stop");
int ticket2 = OrderSend(Symbol(), OP_SELLSTOP, Lots, LowestLow - 10 * Point * 10, 100, HighestHigh, LowestLow - TakeProfit * Point * 10, "Sell Stop");
if(ticket1 < 0 && ticket2 < 0)
{
Alert("Error sending sell stop order ticket # ", ticket1);
Sleep(3000);
}
}
here is my journal entry