Download MetaTrader 5

How to implement a trailing stop

To add comments, please log in or register
Find out how to use widgets at MQL5.community
polymath
182
polymath 2012.08.25 16:15 

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.
Christoffer Hjortlund
32
Christoffer Hjortlund 2012.09.28 01:24  

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 :-) 

Rogerio Figurelli
Moderator
39484
Rogerio Figurelli 2012.09.28 03:26  

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;
...
Moses Olawale  Ajayi
2398
Moses Olawale Ajayi 2012.10.19 10:59  

its really help me a lots.

Thanks

To add comments, please log in or register