Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Expert Advisor | TrailingStop

This is a simple Expert Advisor with an implemented trailing stop.

It has only two parameters:

MetaTrader Experts, Indicators, Scripts and Libraries 

TrailingStop — the distance between the price and the Stop Loss level.

TrailingStep — protection against to frequent modification of position. 

How TrailingStop works — the example of the Buy position: 

Originally the StopLoss of a position is equal to zero, we wait for the following condition to be met

                  if(m_symbol.Bid()-ExtTrailingStop>m_position.PriceOpen())

— i.e. when the Bid minus TrailingStop is greater than the position open price PriceOpen:

MetaTrader Experts, Indicators, Scripts and Libraries

StopLoss is moved to the PriceOpen level (position open price):

MetaTrader Experts, Indicators, Scripts and Libraries

now, when the position has StopLoss (i.e. when StopLoss is not equal to zero), wait for the following condition to be met:

                  if(m_symbol.Bid()-ExtTrailingStop-ExtTrailingStep>m_position.StopLoss())

— i.e. when the Bid minus TrailingStop minus TrailingStep is greater than the StopLoss price: So here we need to additionally check TrailingStep — this prevent position modification on every tick:

MetaTrader Experts, Indicators, Scripts and Libraries

StopLoss is then moved to Bid minus TrailingStop:

MetaTrader Experts, Indicators, Scripts and Libraries 

Here is why we need a protection in the form of TrailingStep:

MetaTrader Experts, Indicators, Scripts and Libraries 

17263

Best MetaTrader Indicators + Profitable Expert Advisors