Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

StopLoss tracking library for MQL5 - Library for MetaTrader 5 - MT4/MT5 resources

author EAcpu | 6 reads | 0 comments |

StopLoss trailing classes library for MQL5 - library for MetaTrader 5

Universal class library for implementing various stop loss tracking strategies in trading robots. The library allows you to flexibly manage the stop loss of open positions through a fixed distance from the price and the value of popular indicators: Parabolic SAR, AMA, DEMA, FRAMA, MA, TEMA, VIDYA and arbitrary levels.

This library provides a convenient way to add many types of different Stop Loss Trails to your MQL5 Expert Advisor. Just insert the required classes, configure the parameters and call the Run() method in OnTick().

 #include <trailing\trailing.mqh>

Class structure

 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- global CSimpleTrailing trail; //--- in OnInit(): Trail.SetSymbol( _symbol );
Trail.SetMagicNumber( 123 );
Trail.SetStopLossOffset( 100 );
Track.SetActive( true ); //--- In OnTick(): Track.Run();

CT Tracking by Industry Base class for trailing indicators Used by inherited classes CT tracking SAR Parabolic SAR Indicator Trailing
 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- global CTrailingBySAR TrailSAR; //--- in OnInit(): TrailSAR.Initialize( _symbol , PERIOD_M15 , 0.02 , 0.2 );
TrailSAR.SetActive( true ); //--- In OnTick(): TrailSAR.Run();

CTrailingByAMA Adaptive Moving Average Tracking
 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- Global CTrailingByAMA TrailAMA; //--- In OnInit(): TrailAMA.Initialize( _SYMBOL , PERIOD_H1 , 9 , 2 , 30 , 0 , PRICE_CLOSE );
TrailAMA.SetActive( true ); //--- In OnTick(): TrailAMA.Run();

CTrailingByDEMA Tracking the Double Exponential Moving Average
 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- global 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
 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- global CTrailingByFRAMA TrailFRAMA; //--- in OnInit(): TrailFRAMA.Initialize( _SYMBOL , PERIOD_H1 , 14 , 0 , PRICE_CLOSE );
TrailFRAMA.SetActive( true ); //--- In OnTick(): TrailFRAMA.Run();

CT tracking MA moving average trailing
 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- Global CTrailingByMA TrailMA; //--- In OnInit(): TrailMA.Initialization( _SYMBOL , PERIOD_H1 , 20 , 0 , MODE_EMA , PRICE_CLOSE );
TrailMA.SetActive( true ); //--- In OnTick(): TrailMA.Run();

CTrailingByTEMA Tracking the Triple Exponential Moving Average
// - - connect
#includes <TRAILING\TRAILING.mqh> //--- global CTrailingByTEMA TrailTEMA; //--- in OnInit(): TrailTEMA.Initialize( _TOM , PERIOD_H1 , 14 , 0 , PRICE_CLOSE );
TrailTEMA.SetActive( true ); //--- In OnTick(): TrailTEMA.Run();

CTrailingByVIDYA Tracking Variable Exponential Dynamic Average
 // - - connect
#includes <TRAILING\TRAILING.mqh> //--- global CTrailingByVIDYA TrailVIDYA; //--- in OnInit(): TrailVIDYA.Initialize( _symbol , PERIOD_H1 , 9 , 12 , 0 , PRICE_CLOSE );
TrailVIDYA.SetActive( true ); //--- In OnTick(): TrailVIDYA.Run();

CT trailing value falling behind specified levels
 // - - connect
#includes <trailing\trailing.mqh> //--- global CTrailingByValue TrailValue; //--- in OnInit(): TrailValue.SetSymbol( _symbol );
TrailValue.SetActive( true ); //--- In OnTick(): TrailValue.Run(customSLBuy, customSLSell);


Set parameters:

For indicator categories - additional indicator parameters (period, price type, method, etc.).

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

StopLoss trailing classes library for MQL5 - library for MetaTrader 5

More details on these trawl topics can be found in the article

Trailing.mqh is a simple way to add trailing stops to your MQL5 Expert Advisors.
Connect the necessary classes, configure parameters and call *.Run() in OnTick.

If your EA has a loop that enumerates its positions, you can call the Run() method to keep track of the tickets for the selected position:

 running( const pos_ticket );

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

 For ( integer i = total number of positions () - 1 ; i >= 0 ; i --) 
  { 
  trailingsimple.run( locationGetTickets (i)); 
  }


Another zigzag correction. zigzag

Function to convert server time from one broker's time zone to another.

It signals the end of a period of low market volatility and heralds large price swings.


Attachment download

📎 Trailings.mqh (107.4 KB)

📎 MovingAverageWithSimpleTrail.mq5 (18.02 KB)

Source: MQL5 #63926

Verification code Refresh