extern int     TrailingStop   = 8; // Activate the classic trailing
extern int     TSstep         = 1; // step of the classic traling
extern int     StopLoss       = 5; // auto set stop loss after creat order
extern double  SoftTSPips     = 8; // Set the Stealth Trailing in pips
extern double  SoftTSNoLoss   = 7; // Close the order after the price come back (in pips)

//------------------------------------------------------------------------------------------------
double MaxPrice=0, 
    SoftNoloss=0, 
    CurentPrice=0;                                    
bool Ans;

void TrailingStop()
{
         
if(TSstep<1) TSstep=1;
   RefreshRates();
    for(int i=0;i<OrdersTotal();i++)
    {
       OrderSelect(i, SELECT_BY_POS,MODE_TRADES);
       if (OrderSymbol()==Symbol())
              {
         if(OrderType() == OP_BUY)
         {
         // BUY Order
            // auto set stop loss after creat order
            if (OrderStopLoss() == 0)
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(Point*StopLoss),OrderTakeProfit(),0);
               Print ("Set stop loss to price ", OrderOpenPrice(), " - (", Point, "*", StopLoss, ")) = ", OrderOpenPrice()-(Point*StopLoss));
            }
            
            if(Bid-OrderOpenPrice()>Point*TrailingStop && (OrderStopLoss()+(Point*TSstep)<Bid-(Point*TrailingStop)||OrderStopLoss()==0))
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(Point*TrailingStop),OrderTakeProfit(),0,Blue);
               
            CurentPrice = (Bid-OrderOpenPrice())/Point;
            
            if( CurentPrice > MaxPrice )
            {
               MaxPrice = CurentPrice;
               if (MaxPrice > SoftTSPips) SoftNoloss = MaxPrice - SoftTSNoLoss;
               
               Print (CurentPrice, "MaxPrice: ", MaxPrice, " SoftNoloss: ", SoftNoloss);
            }
            if ((CurentPrice <=  (SoftNoloss+1)) && (SoftNoloss > 0))
            {
               MaxPrice=0;
               SoftNoloss=0; 

               Ans=OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);      // Close Buy order
               if (Ans==true)                         // everything OK :)
                 {
                  Alert ("StealthTrailing closed the buy order ",OrderTicket());
                 }
               else Alert("Error of close ", GetLastError());

            }

         }
         else if(OrderType() == OP_SELL)
         { 
              
         //SELL order
            // auto set stop loss after creat order
            if (OrderStopLoss() == 0)
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(Point*StopLoss),OrderTakeProfit(),0);
               Print ("Set stop loss to price ",GetLastError(), " - ", OrderOpenPrice()+(Point*StopLoss));
            }
            ///////////
            
            if(OrderOpenPrice()-Ask>Point*TrailingStop && (OrderStopLoss()-(Point*TSstep)>Ask+(Point*TrailingStop)||OrderStopLoss()==0))
               OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(Point*TrailingStop),OrderTakeProfit(),0,Red);
               
            // Stealth Trailing

            CurentPrice = (OrderOpenPrice() - Ask)/Point;
            
            if( CurentPrice > MaxPrice )
            {
               MaxPrice = CurentPrice;
               if (MaxPrice > SoftTSPips) SoftNoloss = MaxPrice - SoftTSNoLoss;
               
               Print (CurentPrice, "MaxPrice: ", MaxPrice, " SoftNoloss: ", SoftNoloss);
            }
            if ((CurentPrice <=  (SoftNoloss+1)) && (SoftNoloss > 0))
            {
               MaxPrice=0;
               SoftNoloss=0; 

               Ans=OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);    
               if (Ans==true)                         //  everything OK
                 {
                  Alert ("StealthTrailing closed the sell order ",OrderTicket());
                 }
               else Alert("Error of close ", GetLastError());

            }          
         }
       }
    }
    return;
}
//------------------------------------------------------------------------------------------------

int start()
{
            
 if(TrailingStop>0) TrailingStop();
}

