Can not close open orders.. please help

 

hi, i would like to close open orders, what is missing in my code? especially closing orders part?


thank you

int period1 = 50;
int ticket;
double volume=0.1;
input int trailing = 250;


int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }

void OnTick()
  {
  double meksi3= iMA(NULL,0,period1,0,MODE_SMA,PRICE_CLOSE,3);
  double meksi2= iMA(NULL,0,period1,0,MODE_SMA,PRICE_CLOSE,2);
  double meksi1= iMA(NULL,0,period1,0,MODE_SMA,PRICE_CLOSE,1);
  int magicNBy =  500; 
  double ema5=   iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1);
  double ema3=   iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,1);
  double ema5o=  iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,2);
  double ema3o=  iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,2);
  double enyuksek = iHigh(Symbol(),0,iHighest(NULL,0,MODE_HIGH,3,0));
  double endusuk =   iLow (Symbol(),0,iLowest (NULL,0,MODE_LOW,3,0)); 
      
int i=0;
int hstTotal=OrdersHistoryTotal();
int counter = 0;
int ticketNumber = 0; 
 for(i=0;i<hstTotal;i++)
    {
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      if(OrderType() < 2)// this weeds out any pending orders.
       if(OrderSymbol()== Symbol())
        if(OrderCloseTime() > counter)
           {
            counter= (int) OrderCloseTime();    //each time it finds a greater closing time it
            ticketNumber = OrderTicket();//overwrites the counter and the ticket number.    
           }
    }
  
OrderSelect(ticketNumber,SELECT_BY_TICKET);//      
  if (OrderCloseTime()< Time[0])
  {
  int CountSymbolPositions=0;
  for(int trade=OrdersTotal()-1;trade>=0;trade--)
    {
      if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
         continue;
      if(OrderSymbol()==Symbol())
         {
         if((OrderType()==OP_SELL || OrderType()==OP_BUY) && OrderMagicNumber()==magicNBy)
         CountSymbolPositions++;
         }
    } 
  
  //buy
  if(Close[1]> Open[1] && Close[1]> meksi1 && CountSymbolPositions<1)
    ) 
  {
  ticket= OrderSend(Symbol(),OP_BUY,volume,Ask,50,0,Ask+800*_Point,NULL,magicNBy);
  if(ticket<0) Print("OrderSend failed with error #",GetLastError()); 
  } 
  
  //sell
  else if(Close[1]< Open[1] && Close[1]< meksi1 && CountSymbolPositions<1)
  {
  ticket = OrderSend(Symbol(),OP_SELL,volume,Bid,50,0,Bid-800*_Point,NULL,magicNBy);
  if(ticket<0) Print("OrderSend failed with error #",GetLastError());
  } ;
    
  }
 //below belongs to closing of open trades 

    if(OrderSelect(ticket ,SELECT_BY_TICKET)==true && OrderMagicNumber()== magicNBy && OrderCloseTime()==0 && OrderSymbol()=="EURUSD") 
      {
            int orderType = OrderType();// Short = 1, Long = 0
            
            if(orderType == 0)
            {   
            //check for c<mson- ters yon kapanısı
              if (Close[1]< Open[1] && Close[1]< meksi1)
              {bool orderbulma1= OrderSelect(ticket,SELECT_BY_TICKET);
              bool orderkapama1= OrderClose(OrderTicket(),volume,Bid,10);
              if (orderkapama1==false)
              {Print("trail profit kapama hatasi oldu- long");}
              else             
              Print("trail profit kapandi- long"); return;}    
              
              //check for profit trailing stop buy icin
              
              bool orderbulma2= OrderSelect(ticket,SELECT_BY_TICKET);
              if(enyuksek-Bid>trailing*_Point && OrderTakeProfit()>0)
              {bool orderkapama2= OrderClose(OrderTicket(),volume,Bid,10);
              if (orderkapama2==false)
              {Print("trail profit kapama hatasi oldu- long");}
              else             
              Print("trail profit kapandi- long"); return;}         
              
              //check for ema'ya gore buy kapama 
              if (ema3<ema5 && ema3o>ema5o)
              {bool orderbulma3= OrderSelect(ticket,SELECT_BY_TICKET);
               bool orderkapama3= OrderClose(OrderTicket(),volume,Bid,10);
              if (orderkapama3==false)
              {Print("kapama hatası oldu- long");}
              else             
              Print("ema ya gore kapandi- long"); return;}
                  
             }       
              
             else if(orderType == 1)
            {            
            //check for c>mson- ters yon kapanısı
              if(Close[1]> Open[1] && Close[1]> meksi1)
              {bool orderbulma4= OrderSelect(ticket,SELECT_BY_TICKET);
               bool orderkapama4= OrderClose(OrderTicket(),volume,Ask,10);
              if (orderkapama4==false)
              {Print("ters yon kapama hatasi oldu- short");}
              else             
              Print("ters yone kapandı- short"); return;}              
              
            //check for profit trailing stop sell icin
              bool orderbulma5= OrderSelect(ticket,SELECT_BY_TICKET);
              if(Ask-endusuk>trailing*_Point && OrderTakeProfit()>0)
              {bool orderkapama5= OrderClose(OrderTicket(),volume,Ask,10);
              if (orderkapama5==false)
              {Print("trail profit kapama hatasi oldu-short");}
              else             
              Print("trail profit kapandi- short"); return;}   
              
              //check for ema'ya gore sell kapama
              if (ema3>ema5 && ema3o<ema5o)
              {bool orderbulma6= OrderSelect(ticket,SELECT_BY_TICKET);
               bool orderkapama6= OrderClose(OrderTicket(),volume,Ask,10);
              if (orderkapama6==false)
              {Alert("kapama hatası oldu- short");}
              else             
              Alert("ema ya gore kapandi- short"); return;}   
            }
      }
    
 }
//+------------------------------------------------------------------+
Reason: