Trailing stop from hell, please help my trailing stop trails but doesn't stop loss

 
Guys i finally made a robot that buys whenever a bullish candle open but now the problem is my trailing stop, for example the robot place a buy order and i'm trailing in profit but when the market starts going against my direction the trailing stop doesn't close the locked profit ending up in a loss trade, so please help anyone

here is the code guys, please see what you can find mql5


//Create an instance of CTrade
#include<Trade\Trade.mqh>
CTrade trade;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   if(PositionsTotal()==0)
      trade.Buy(0.10,NULL,Ask,(Ask-1000*_Point),(Ask+20*_Point),NULL);

   CheckTrailingStop(Ask);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckTrailingStop(double Ask)
  {
   double SL=NormalizeDouble(Ask-150*_Point,_Digits);

   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      string symbol=PositionGetSymbol(i);
      if(_Symbol==symbol)
        {
         ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
         double CurrentStopLoss=PositionGetDouble(POSITION_SL);

         if(CurrentStopLoss<SL)
            trade.PositionModify(PositionTicket,(CurrentStopLoss+10*_Point),0);
        }
     }
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
Reason: