plz help

 
some one can slove it why TralingStop and BreakEven dont work with sell strades
    extern bool MON = true;
input string StartH1 ="03:00";
input string EndH1 ="21:00";

extern bool TUE = true;
input string StartH2 ="03:00";
input string EndH2 ="21:00";

extern bool WED = true;
input string StartH3 ="03:00";
input string EndH3 ="21:00";

extern bool THU = true;
input string StartH4 ="03:00";
input string EndH4 ="21:00";

extern bool FRI = true;
input string StartH5 ="03:00";
input string EndH5 ="21:00";


extern int Steps1=15;
extern int Steps=15;




input bool UseTimeFilter=true;
bool Trade =false;
 




extern double MaxDrawDown = 5 ;
extern int x = 3;

extern double MaxLot = 0.8;
enum Order{AUTO,BUY,SELL,MANUAL};

extern Order FirstOrder=AUTO;

extern double USD =0.5 ;



extern double Multiplier =2 ;



extern bool useBreakEven= true;
extern int BreakEven =5;
extern int BreakEvenStep = 1;

extern bool UseTrailingStop = true ;
extern int TrailingStop = 5;
extern int TrailingStep = 5;







extern double lot = 0.01 ;


extern int TP = 0 ;
extern int SL = 0 ;
extern int MagicNumber = 1917 ;
//input bool TimeFilter = false ;
input string EaName = "ATLAS" ;
double MyPoint, Buyt, Buys, Sellt, Sells,price,tp,sl,sLot,bLot,BuyLot,SellLot  ;
int Ticket ,cnt,Decimal;
bool zs ;
color N = clrNONE;





int OnInit()
  {

   if(Digits ==4 || Digits <=2)
      MyPoint=Point ;
   if(Digits ==3 || Digits ==5)
      MyPoint=Point*10 ;
   Comment(EaName,"\n\nalkosovi13@yahoo.fr") ;
if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)Decimal =2;else Decimal=1;


   return(INIT_SUCCEEDED);
  }


void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()

 {
  
    
    


        Trade = false;

        if ((UseTimeFilter == true) && (MON == true) && (DayOfWeek() == 1))
        {
                if (TimeFilter(StartH1, EndH1) == true) Trade = true;
        }
        
        if ((UseTimeFilter == true) && (TUE == true) && (DayOfWeek() == 2))
        {
                if (TimeFilter(StartH2, EndH2) == true) Trade = true;
        }
        
        if ((UseTimeFilter == true) && (WED == true) && (DayOfWeek() == 3))
        {
                if (TimeFilter(StartH3, EndH3) == true) Trade = true;
        }
        
        if ((UseTimeFilter == true) && (THU == true) && (DayOfWeek() == 4))
        {
                if (TimeFilter(StartH4, EndH4) == true) Trade = true;
        }
        
        if ((UseTimeFilter == true) && (FRI == true) && (DayOfWeek() == 5))
        {
                if (TimeFilter(StartH5, EndH5) == true) Trade = true;
        }
       





    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
 
        
        
        
        
        
        
        
        
        
        
        

 

 

 
     if(TP == 0)
      Buyt = 0;
   else
      Buyt= Ask+TP*MyPoint;
   if(SL == 0)
      Buys = 0;
   else
      Buys= Ask-SL*MyPoint;

   if(TP == 0)
      Sellt = 0;
   else
      Sellt= Bid-TP*MyPoint;
   if(SL == 0)
      Sells = 0;
   else
      Sells= Bid+SL*MyPoint;
   










if (((1-(AccountEquity()/AccountBalance()))*100)>MaxDrawDown){CB();CS();Delet ();  }


if (USD>0&& SymbolProfit()>=USD) {CB();CS();Delet ();  }


if (TB()<1 ){DeletType(OP_BUYLIMIT);}
if (TS()<1 ){DeletType(OP_SELLLIMIT);}  

 

      
      if(useBreakEven && BreakEven>0){
if(TB()<1000) { BEBuy();}   
if(TS()<1000)  {BESell();}   
      
  
    }  
      
      
      
      
      
 if (UseTrailingStop && TrailingStop>0 && TrailingStep>0)
 {

if(TB()<1000) {TSBUY();}
if(TS()<1000) {TSELL();}
}


 
         






 double arr[4] = {0.0,0.0,0.0,0.0 };
     
     for (int j = 0 ; j< 1000 ; j++)
     {
     
     double zigZag =  iCustom(Symbol(),0,"ZigZag",12,5,3,0,j);
     
     if ( zigZag !=EMPTY_VALUE && zigZag !=0)
     {
      
      if (arr[0]==0.0)
      {
      arr[0] =zigZag ;
      } 
      
      else if (arr[1]==0.0)
      {
       arr[1]= zigZag;
      }
      else if (arr[2] ==0.0)
      {
      arr[2] =zigZag;
      }
        else if (arr[3] ==0.0)
      {
      arr[3] =zigZag;
      
      break;
}}}






 

 static datetime LastClosed ;

if(Trade){
if (   Time[0] != LastClosed && TS()<1   &&Total(OP_SELLLIMIT)<1&&FirstOrder==SELL  && Open[1]> arr[2] && Close[1] < arr[2]   )

{

 
 

   
 Ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,3,Sells,Sellt,EaName, MagicNumber,0,clrRed);

 

   if(Ticket < 0) ;
   
   else LastClosed = Time[0]; 

          
  }  






 
 if ( Time[0] != LastClosed &&  TB()<1&&Total(OP_BUYLIMIT)<1 &&FirstOrder==BUY   && Open[1]< arr[2] && Close[1] > arr[2]     )
 
 {
  
  

 
Ticket =OrderSend(Symbol(),OP_BUY,lot,Ask,3,Buys,Buyt,EaName,MagicNumber,0,clrBlue);
 
 
   if(Ticket < 0) ;
   
   else LastClosed = Time[0]; 
  
  
  
    }    
     // auto
 
    if (TS()<1&&FirstOrder==AUTO){


if ( Time[0] != LastClosed &&   TS()<1&& Total(OP_SELLLIMIT)<1 && Open[1]> arr[2] && Close[1] < arr[2])
{

  

 
 

   Ticket = OrderSend(Symbol(),OP_SELL,lot,Bid,3,Sells,Sellt,EaName, MagicNumber,0,clrRed);



 

 if(Ticket < 0) ;
   
   else LastClosed = Time[0]; 
          
  } 
  
  
 if (  Time[0] != LastClosed && TB()<1 &&Total(OP_BUYLIMIT)<1 && Open[1]< arr[2] && Close[1] > arr[2]){
  
 


 Ticket =OrderSend(Symbol(),OP_BUY,lot,Ask,3,Buys,Buyt,EaName,MagicNumber,0,clrBlue);
  
   if(Ticket < 0) ;
   
   else LastClosed = Time[0]; 
  
  
  }}


}

if (Total()>0){


// buylimit

  if (LastOrderInfo("Type")==OP_BUY && Total(OP_BUYLIMIT)<1 &&(TS()+TB())<x  )

  {
  price=arr[1]-Steps*MyPoint;
   if(TP==0)tp=0;else tp = price  +TP*MyPoint;
   if(SL==0)sl=0;else sl =price  -SL*MyPoint;
   
   bLot = NormalizeDouble (LastOrderInfo("Lots",OP_BUY)*Multiplier, Decimal);
   if(bLot>MaxLot)BuyLot=MaxLot;else BuyLot=bLot;
   
   Ticket = OrderSend(Symbol(),OP_BUYLIMIT,BuyLot,price,3,sl,tp,EaName,MagicNumber,TimeCurrent()+60*60,N);
   if(Ticket<0){Print("EROR Open Buy Limit Code", GetLastError());}
 }
 
 //selllimit
if(LastOrderInfo("Type")==OP_SELL&&Total (OP_SELLLIMIT)<1 &&(TS()+TB())<x )
  {

price=arr[1]+Steps*MyPoint;
  
   if(TP==0)tp=0;else tp = price-TP*MyPoint;
   if(SL==0)sl=0;else sl =  price+SL*MyPoint;
   
   sLot =  NormalizeDouble (LastOrderInfo("Lots",OP_SELL)*Multiplier ,Decimal);
   if (sLot>MaxLot)SellLot = MaxLot;else SellLot=sLot;
   Ticket= OrderSend(Symbol(),OP_SELLLIMIT,SellLot, price,3,sl,tp,EaName,MagicNumber,TimeCurrent()+60*60,N);
   if(Ticket<0){Print("EROR Open Sell Limit Code", GetLastError());}
  
  
  
}

}


 
 
 
 
 
 
 }
   //**********************************************   
      
  



       
 


      
       
       
       
   //   last orderinfo    
       
       
double LastOrderInfo(string Info,int type =-1)

{
   for(int i =OrdersTotal () -1 ; i>=0;i--) {
     if (OrderSelect (i,SELECT_BY_POS,MODE_TRADES))
     if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && (OrderType()==type ||type==-1))
      {
             if(Info=="Type")           return (OrderType());
      else   if(Info=="SL")             return (OrderStopLoss());
      else   if(Info=="TP")             return (OrderTakeProfit());
      else   if(Info=="Lots")           return (OrderLots());
      else   if(Info=="Price")          return (OrderOpenPrice());           
      else   if(Info=="Profit")         return (OrderProfit());
      else   if(Info=="Time")           return (OrderOpenTime());
             
          
      }
      }return (0);
      
      
      } 
       
       
       
 // last closedinfo      
       
  double LastClosed(string S,int type =-1){
  
 for (int f = OrdersHistoryTotal() -1 ; f>=0;f--)
 {
 
    if(OrderSelect(f,SELECT_BY_POS,MODE_HISTORY)) 
      if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && (OrderType()== type || type ==-1))
      {
      
      if (S=="Lots")                          return(OrderLots());
else  if (S=="OpenPrice")                     return(OrderOpenPrice());       
else  if (S=="ClosePrice")                    return(OrderClosePrice());
else  if (S=="TP")                            return(OrderTakeProfit());
else  if (S=="SL")                            return(OrderStopLoss());
else  if (S=="Type")                          return(OrderType());
else  if (S=="Profit")                        return(OrderProfit());
else  if (S=="OpenTime")                      return(OrderOpenTime());
else  if (S=="CloseTime")                     return(OrderCloseTime());
else  if (S=="Com")                           return(OrderCommission());
else  if (S=="SWAP")                          return(OrderSwap());
else  if (S=="Magic")                         return(OrderMagicNumber());
       
     
       
   }
   }return(0);
   
   
   }
       
       
     
 // one tread at one time
 bool IsOkTrade()
 {
 int limit = OrdersTotal();
 for (int i = 0; i<limit;i++)
 {
 if(!OrderSelect(i,SELECT_BY_POS))
 {

 
 return false;
 
 }
 if(OrderMagicNumber()==MagicNumber)
  {     

  return false;
  }
  }     
     return true;
     }   
       
       
       
       
       
     int TS()
  {

   int s=0,i;
   for(i =0 ; i < OrdersTotal() ; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS, MODE_TRADES))
         if(Symbol()== OrderSymbol()&& OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL)
           {
            s++ ;

           }
     }
   return (s) ;
  }



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int TB()
  {
   int b = 0, i ;
   for(i=0; i < OrdersTotal() ; i++)
     {

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         if(Symbol() == OrderSymbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY)
           {
            b++;
           }
     }
   return (b);

  }  
       
       
   //tralingstop    
       
 void TSBUY() {
 
 for (cnt=OrdersTotal()-1;cnt>=0;cnt--){
 if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if (OrderSymbol() == Symbol() ) 
 if (OrderMagicNumber() ==MagicNumber)
 if (OrderType()==OP_BUY)
 if (Bid-OrderOpenPrice() >TrailingStop*MyPoint)
 if (OrderStopLoss() <Bid- TrailingStep*MyPoint)
 zs = OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStep * MyPoint,OrderTakeProfit(),0,N);
 
 }
 }
 
 void TSELL() {
 
 for (cnt = OrdersTotal()-1;cnt>=0;cnt--){
 if (OrderSelect (cnt ,SELECT_BY_POS,MODE_TRADES))
 if (OrderSymbol()==Symbol())
 if (OrderMagicNumber() == MagicNumber)
 if (OrderType()==OP_SELL)
 if (OrderOpenPrice()-Ask >TrailingStop*MyPoint)
 if (OrderStopLoss()>Ask+TrailingStep*MyPoint)
 zs = OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStep*MyPoint,OrderTakeProfit(),0,N);
 

 
 }}
 
 // pending order
 
 int Total(int type=-1){
 int total =0,i;
 for (i=0;i<OrdersTotal();i++){
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
 if(Symbol()==OrderSymbol()&& OrderMagicNumber()==MagicNumber && (OrderType()==type||type==-1))
 {
 total++;
 }
 }return (total);
 }
 
 
 
 
// close order

  void CS(){
 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType()==OP_SELL)
 {
 bool close_s=OrderClose(OrderTicket(),OrderLots(),Ask,3,clrRed);
 }}
 
 void CB(){
 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType()==OP_BUY)
 {
 bool close_b=OrderClose(OrderTicket(),OrderLots(),Bid,3,clrBlue);
 }} 
 
 
  void DeletType(int type){
 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber && OrderType()==type)
 {
 bool Delet_type = OrderDelete(OrderTicket(),clrNONE);
 }
 }
 
 
  void Delet () {
 for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if(OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber )
 {
 bool Delet = OrderDelete(OrderTicket(),clrNONE);
 }
 }

 
 
 //break even
 
 void BEBuy(){
 for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if(OrderSymbol()== Symbol())
 if(OrderMagicNumber()==MagicNumber)
 if(OrderType()==OP_BUY)
 if(Bid-OrderOpenPrice()> BreakEven*MyPoint)
 if(OrderOpenPrice()>OrderStopLoss())
 zs=OrderModify (OrderTicket(),OrderOpenPrice(), OrderOpenPrice()+ (BreakEvenStep* MyPoint),OrderTakeProfit(),0,N);
 }
 
 }
 void BESell(){
 
 for (cnt = OrdersTotal()-1;cnt>=0;cnt--){
 
 if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
 if(OrderSymbol()==Symbol())
 if(OrderMagicNumber()==MagicNumber)
 if(OrderType()==OP_SELL)
 if(OrderOpenPrice()-Ask>BreakEven*MyPoint)
 if(OrderOpenPrice()<OrderStopLoss())
 zs=OrderModify (OrderTicket(),OrderOpenPrice() , OrderOpenPrice()- (BreakEvenStep* MyPoint),OrderTakeProfit(),0,N);
 
 }}
 
 
 // info of market
 
 void info ( string NAME ,double CORNER, int Y,int X,string TEXT,int FONTSIZE , string FONT , color FONTCOLOR){
 
 
 ObjectCreate(NAME,OBJ_LABEL,0,0,0);
 ObjectSetText(NAME,TEXT,FONTSIZE,FONT,FONTCOLOR);
 ObjectSet(NAME,OBJPROP_CORNER,CORNER);
 ObjectSet(NAME,OBJPROP_XDISTANCE,X);
 ObjectSet(NAME,OBJPROP_YDISTANCE,Y);
 
 
  
 }
 
 
 
 double SymbolProfit() {
 
 
 double p=0;
  for (cnt =0 ;cnt<OrdersTotal() ; cnt ++){
     if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
      if(OrderSymbol() == Symbol()&& OrderMagicNumber () == MagicNumber){
      
      p=p+OrderProfit()+OrderCommission()+OrderSwap();
      }
 

 
 
 }return(p);
 }



  double SellProfit() {
 
 
 double p=0;
  for (cnt =0 ;cnt<OrdersTotal() ; cnt ++){
     if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
      if(OrderSymbol() == Symbol()&& OrderMagicNumber () == MagicNumber)
      if(OrderType()==OP_SELL){
      
      p=p+OrderProfit()+OrderCommission()+OrderSwap();
      }
 

 
 
 }return(p);
 }
 
  double BuyProfit() {
 
 
 double p=0;
  for (cnt =0 ;cnt<OrdersTotal() ; cnt ++){
     if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
      if(OrderSymbol() == Symbol()&& OrderMagicNumber () == MagicNumber)
      
     if(OrderType()==OP_BUY){
      
      p=p+OrderProfit()+OrderCommission()+OrderSwap();
      }
 

 
 
 }return(p);
 }
 
 
 
 
 
 bool TimeFilter(string Sh, string Eh)
{
        datetime Start = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " + Sh);
        datetime End   = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE) + " " + Eh);

        if ((Time[0] >= Start) && (Time[0] <= End))
        {
                return(true);
        }

        return(false);
}