Trailing Stop remains fixed (MQL5)

LeGrandKent
12
LeGrandKent  

Hello,

I recently coded a trailing stop to complete my simple strategy.

When I compile my code, it shows no error. However, when I run the code to test it, the stop loss does not follow my position but remains fixed.

Can you tell me what does not work in my code? Thanks a lot for your help. LGK


#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"


#include <Ichimoku.mqh>
#include <Trade\Trade.mqh>

CTrade trade;

input double LOTS=0.1;

//+------------------------------------------------------------------+
//|   On tick                                                        |
//+------------------------------------------------------------------+
void OnTick()
  {

   int signal=SignalCross();
   int trend=Trend();
   
   

      if(signal==SIGNAL_UP && trend==TREND_UP)
        
        
        {
        
        double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
         

         trade.Buy(LOTS,Symbol(),Ask,(Ask-30*_Point),0,NULL);
         
         CheckTrailingStopBUY(Ask); 
         
        }
       
     
      else if(signal==SIGNAL_DOWN && trend==TREND_DOWN)
       
        {
         
         
         double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
         
 
          trade.Sell(LOTS,Symbol(),Bid,(Bid+30*_Point),0,NULL);
         
       CheckTrailingStopSELL(Bid);  
         
         
        }
     
 

       
}

     
  
//+------------------------------------------------------------------+
//|  Trailing Stop                                            |
//+------------------------------------------------------------------+
  
  void CheckTrailingStopBUY(double Ask)
  {


   double SL=NormalizeDouble(Ask-20*_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);
           }
}
}
}



void CheckTrailingStopSELL(double Bid)
  {


   double SL=NormalizeDouble(Bid+20*_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);
           }
}
}
}  
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is...
lippmaje
1578
lippmaje  

You need to check the trailing stops on each tick (not just only after buy/sell):

MqlTick tick;
SymbolInfoTick(_Symbol,tick);

if(signal==SIGNAL_UP   && trend==TREND_UP)   trade.Buy (LOTS,_Symbol,tick.ask,tick.ask-30*_Point,0,NULL);
if(signal==SIGNAL_DOWN && trend==TREND_DOWN) trade.Sell(LOTS,_Symbol,tick.bid,tick.bid+30*_Point,0,NULL);

if(trade.SelectPosition(_Symbol)) {
   long postype=PositionGetInteger(POSITION_TYPE);
   if(postype==POSITION_TYPE_BUY)  CheckTrailingStopBUY (tick.ask);
   if(postype==POSITION_TYPE_SELL) CheckTrailingStopSELL(tick.bid);
}
LeGrandKent
12
LeGrandKent  
lippmaje:

You need to check the trailing stops on each tick (not just only after buy/sell):

Thank you very much !