Error 4051 Invalid ticket for OrderClose

 

void OnTick()
{
int res;
double MA = iCustom(NULL,0,rsimatest,rsimatest_RSI,0);

  
//open sell  
   if(MA<50 && OrdersTotal() <1)
      {
       res = OrderSend(Symbol(),OP_SELL,1,Bid,3,0,0,"",BIGMACMATT,0,Red);
       return;
      }
//open buy
   if(MA>50 && OrdersTotal() <1)
      {
      res= OrderSend(Symbol(),OP_BUY,1,Ask,3,0,0,"",BIGMACMATT,0,Blue);
      return;
      }
      
//close buy
   if(OrdersTotal()>0 && MA>50)
   {
      if (!OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue)){
              Print("OrderModify error ",GetLastError());}
      return;
   }
//close sell
   if(OrdersTotal()>0 && MA<50)
   {
      if (!OrderClose(OrderTicket(),OrderLots(),Bid,3,Red)){
      Print("OrderModify error ",GetLastError());}
      return;
   }


return;

 
this is my first attempt at a working EA hopefully this is the last issue before it is fully functional, if anyone could help with this it'd be much appreciated
 
You have to select an order before using OrderTicket()
Reason: