Universal class library for implementing various StopLoss trailing strategies in trading robots. The library allows you to flexibly manage StopLoss of open positions by a fixed distance from the price, as well as by the values of popular indicators: Parabolic SAR, AMA, DEMA, FRAMA, MA, TEMA, VIDYA and arbitrary level.

The library provides a convenient way to add several types of different StopLoss trailing to your MQL5 Expert Advisor. Just plug in the required class, configure the parameters and call the Run() method in OnTick().

Header

#include <Trailings\Trailings.mqh>

Class Structure

Class

Purpose

Usage examples

CSimpleTrailing

base class of price trailing (simple trailing)

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CSimpleTrailing trail;

//--- In OnInit():
trail.SetSymbol(_Symbol);
trail.SetMagicNumber(123);
trail.SetStopLossOffset(100);
trail.SetActive(true);

//--- In OnTick():
trail.Run();

CTrailingByInd

base class of trailing by indicator

Used by inherited classes

CTrailingBySAR

trailing by Parabolic SAR indicator

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingBySAR trailSAR;

//--- In OnInit():
trailSAR.Initialize(_Symbol, PERIOD_M15, 0.02, 0.2);
trailSAR.SetActive(true);

//--- In OnTick():
trailSAR.Run();

CTrailingByAMA

Trailing by Adaptive Moving Average

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByAMA trailAMA;

//--- In OnInit():
trailAMA.Initialize(_Symbol, PERIOD_H1, 9, 2, 30, 0, PRICE_CLOSE);
trailAMA.SetActive(true);

//--- In OnTick():
trailAMA.Run();

CTrailingByDEMA

Trailing Double Exponential Moving Average

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByDEMA trailDEMA;

//--- In OnInit():
trailDEMA.Initialize(_Symbol, PERIOD_H1, 14, 0, PRICE_CLOSE);
trailDEMA.SetActive(true);

//--- In OnTick():
trailDEMA.Run();

CTrailingByFRAMA

Fractal Adaptive Moving Average trailing

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByFRAMA trailFRAMA;

//--- In OnInit():
trailFRAMA.Initialize(_Symbol, PERIOD_H1, 14, 0, PRICE_CLOSE);
trailFRAMA.SetActive(true);

//--- In OnTick():
trailFRAMA.Run();

CTrailingByMA

Moving Average trailing

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByMA trailMA;

//--- In OnInit():
trailMA.Initialize(_Symbol, PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE);
trailMA.SetActive(true);

//--- In OnTick():
trailMA.Run();

CTrailingByTEMA

Trailing Triple Exponential Moving Average

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByTEMA trailTEMA;

//--- In OnInit():
trailTEMA.Initialize(_Symbol, PERIOD_H1, 14, 0, PRICE_CLOSE);
trailTEMA.SetActive(true);

//--- In OnTick():
trailTEMA.Run();

CTrailingByVIDYA

Trailing Variable Index Dynamic Average

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByVIDYA trailVIDYA;

//--- In OnInit():
trailVIDYA.Initialize(_Symbol, PERIOD_H1, 9, 12, 0, PRICE_CLOSE);
trailVIDYA.SetActive(true);

//--- In OnTick():
trailVIDYA.Run();

CTrailingByValue

trailing on a specified level

//--- Connection 
#include <Trailings\Trailings.mqh>

//--- Globally
CTrailingByValue trailValue;

//--- In OnInit():
trailValue.SetSymbol(_Symbol);
trailValue.SetActive(true);

//--- In OnTick():
trailValue.Run(customSLBuy, customSLSell);

Setting parameters:

  • SetSymbol(symbol) - setting the trading symbol;
  • SetMagicNumber(magic) - set magic number;
  • SetStopLossOffset(offset) - setting the StopLoss offset from the price/indicator;
  • SetTrailingStart(start) - setting the minimum profit for trailing activation;
  • SetTrailingStep(step) - setting the step of StopLoss movement;
  • SetSpreadMultiplier(value) - setting the spread multiplier for StopLevel;
  • SetActive(flag) - enable/disable trailing.

For indicator classes - additional indicator parameters (periods, price types, methods, etc.).

Here is an example of how the Parabolic SAR trawl connected to the standard Expert Advisor \MQL5\Experts\Advisors\ExpertMACD.mq5 works:

More details on the topic of these trawls can be found in the articles

  1. How to add Trailing Stop on Parabolic SAR indicator
  2. How to make any type of Trailing Stop and connect it to an Expert Advisor

Trailings.mqh is an easy way to add a trailing StopLoss to your MQL5 Expert Advisor.
Connect the necessary class, configure the parameters and call *.Run() in OnTick.

If your EA has a cycle of enumerating its positions, you can call the Run() method of trailing with the ticket of the selected position:

Run(const ulong pos_ticket);

In the common simple case, this would be so in the OnTick() handler:

for(int i=PositionsTotal()-1; i>=0; i--) 
  { 
   trailing_simple.Run(PositionGetTicket(i)); 
  }