i need help with my trailing stop loss, it does trail but doesn't stop loss

 

guys I made an EA that buys when a candle closes bullish, now my problem is the trailing stop loss, for example after a buy order is performed and the chart move in my direction the trailing stop does trail but when the chart changes direction the TSL doesn't close the locked profit, so please help me find the problem 

here is the code

//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.30,NULL,Ask,(Ask-1000*_Point),0,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...
Files:
 
Sechene.9604:

How many times have you been told to use the code button (Alt + S) when pasting code?

Please edit your post.

 
Keith Watford:

How many times have you been told to use the code button (Alt + S) when pasting code?

Please edit your post.

oh h,yeah i tend to forget some important stuff, Sorry  and sir I've tested the code with every tick and also 1 minute OHLC it does work nicely but on a demo the trailing stop just trail the chart and has no effect,    

thank you