How to implement a trailing stop

 

Hi guys,

I've  been using this tutorial as a template for my EAs because it's easy to read and add indicators and conditions to it:  https://www.mql5.com/en/articles/100

But unfortunately, I couldn't add a trailing stop functionality to this code.  I've tried to copy some code from other tutorials for a fixed trailing stop but couldn't get it to work properly.  Can anybody show me a sample code or point me to any sample code or article that can be combined with this to add a trailing stop?  Thanks a lot! :D 

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • 2010.06.09
  • Samuel
  • www.mql5.com
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
 

I like to keep it simple, so I have made this two functions.

This snippet is just a simple trailing SL, for both a long and short order. The SL will follow your trade with a fixed length, set in an external input parameter. 

You need to declare this at the top of the EA, to make it an external parameter:

input double stoploss = 0.00020;

Then call TrailingSL(); in each iteration of your main/desired event (typically within OnTick).

void TrailingSL() {
   double StopLossTemp;
   
   for(i = 0; i < PositionsTotal(); i++)
   {      
      if(Symbol()==PositionGetSymbol(i))
      {
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            if(PositionGetDouble(POSITION_PRICE_CURRENT) > PositionGetDouble(POSITION_PRICE_OPEN)) {
               
               StopLossTemp = PositionGetDouble(POSITION_PRICE_CURRENT) - stoploss;
               ChangePosition(StopLossTemp);
            }
         }
   
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
            if(PositionGetDouble(POSITION_PRICE_CURRENT) < PositionGetDouble(POSITION_PRICE_OPEN)) {
               StopLossTemp = PositionGetDouble(POSITION_PRICE_CURRENT) + stoploss;
               ChangePosition(StopLossTemp);
            }
         }
      }
   }
}

void ChangePosition(string StopLossTemp) {
   request.action = TRADE_ACTION_SLTP;
   request.symbol = Symbol();
   request.sl = NormalizeDouble(StopLossTemp,Digits());
   
   OrderSend(request,result);
}

 

I hope this is to some use for you :-) 

 

Just a few corrections and suggestions for the code:

 

void ChangePosition(double StopLossTemp) { // change string to double

 

input double trailing_stop=100; // independent stoploss for trailing

...
  StopLossTemp = PositionGetDouble(POSITION_PRICE_CURRENT) - trailing_stop;
...

...
  StopLossTemp = PositionGetDouble(POSITION_PRICE_CURRENT) + trailing_stop;
...
 

its really help me a lots.

Thanks