Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2016.03.25 13:31
Function library for maintaining positions with a simple trailing stop

Function library for maintaining positions with a simple trailing stop - library for MetaTrader 4

| English Español Русский 中文 Deutsch 日本語 Português

votes: 8

With the help of this library it is possible to implement the ability to maintain positions with a simple trailing stop in any Expert Advisor. This library is universal and is designed to be able to connect to virtually any EA using the #include compiler directive and granting the EA the ability to maintain positions with a simple trailing stop.

In order to connect the library to an EA it is necessary to make the following changes:

1. Insert the compiler directive before the declaration of functions:

#include <a-SimpleTrailing.mqh>

2. Declare the global variables:

color clModifyBuy = Aqua;    // Color of Buy modification icon
color clModifySell = Tomato; // Color of Sell modification icon

3. Declare the external parameters:

// Use sound signals during the execution of trades
extern bool   UseSound        = True; 
extern string NameFileSound   = "expert.wav"; // Name of the sound file
extern int    NumberOfTry     = 3;  // Number of trade attempts on errors 
extern int    PauseAfterError = 75; // Pause between trade attempts in seconds

4. Insert the line at the end of the start() function:


5. Place the library file a-SimpleTrailing.mqh into the terminal folder ...\experts\include\

Once the library is connected, the EA will have the following external parameters:

// Use trailing stop. If 'False', the trailing stop is disabled.
UseTrailing = True;
// Trail only profit. If 'False', then the trailing stop will start 
// working in the unprofitable zone.
ProfitTrailing = True; 
TrailingStopBuy = 50;  // Trailing stop size for Buy orders.
TrailingStopSell = 50; // Trailing stop size for Sell orders.
TrailingStep = 5;      // Trailing step.
The TrailingPositions() function can take on the following parameters:

- symbol name ("" - current symbol);
- operation (-1 - any position, 0 - Buy, 1 - Sell);
- MagicNumber (-1 - any magic number).

This allows to narrow the scope of the trailing stop operation. For example, trail only Euro:


Or trail only Pound Sells:

    TrailingPositions("GBPUSD", OP_SELL);

And if the EA uses a magic number, then it must be passed to the maintenance function:

    TrailingPositions("", -1, MAGIC);

Translated from Russian by MetaQuotes Software Corp.
Original code:

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

Average Directional Movement Index (ADX) Average Directional Movement Index (ADX)

The Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.

LinesProfitLoss LinesProfitLoss

Calculates profit (loss) of the current orders on the symbol.

FFC - Forex Factory Calendar FFC - Forex Factory Calendar

Modified version of FF Calendar Indicator with new features.