OrderClose error

 
if (OrderSelect(0,SELECT_BY_POS, MODE_TRADES) == false && OrdersTotal() == 1)
{  Print ("failed Order Select");   }

if(OrderType()==OP_BUY && M < S)
{  OrderClose (OrderTicket(),Lots,Bid,2,Blue);  }
else if(OrderType()==OP_SELL && M > S)
{  OrderClose (OrderTicket(),Lots,Bid,2,Blue);  }
I am not getting an error message that the OrderSelect part failed, but I am getting OrderClose error 4051 - invalid ticket for OrderClose. I do not understand why.
 
jshumaker:
I am not getting an error message that the OrderSelect part failed, but I am getting OrderClose error 4051 - invalid ticket for OrderClose. I do not understand why.
You only get the message if the OrderSelect() fails AND OrdersTotal() == 1 what will happen if you have no orders ? the OrderSelect fails but OrdersTotal() = 0 so there is no message . . . . fix your code.
Reason: