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

Library for maintaining positions with simple trailing stops - Library for MetaTrader 4 - MT4/MT5 Resources

author EAcpu | 2 reads | 0 comments |

With the help of this library, the function of maintaining positions can be achieved in any EA transaction through simple trailing stop loss. The library is universal and designed to be able to be connected to almost any EA using the #include compiler directive and gives the EA the ability to maintain positions through a simple trailing stop.


In order to connect the library to the EA, the following changes need to be made:

1. Insert a compilation directive before the function declaration:

 #include

2. Declare global variables:

 Color clModifyBuy = Aqua; // Color of the purchase modification icon
Color clModifySell = tomato; // Color of sell modification icon

3. Declare external parameters:

 // Use sound signals during trade execution
external boolean use sound = true ; external string name file sound = "expert.wav" ; // sound file name
external integer number of attempts = 3 ; // number of incorrect transaction attempts
external integer pause after error = 75 ; // Pause between trade attempts (in seconds)

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

 if (use trailing) 
    trailingPosition();

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

After connecting the library, the EA will have the following external parameters:

 // Use trailing stop. If "False", trailing stop is disabled. Use trailing= true ; // Only trail profits. If "False", the trailing stop will start
// Work in unprofitable areas. profit_tracking= true ; 
Trailing Stop Buy = 50 ; // Trailing stop size for buy orders. Trailing stop sell = 50 ; // Trailing stop size for sell orders. Number of trailing steps = 5 ; // Trailing steps.
The TrailingPositions() function can take the following parameters:

- Trading symbol name ("" - current trading symbol);
- Operation (-1 - any position, 0 - buy, 1 - sell);
- MagicNumber (-1 - any magic number).

This can narrow the scope of trailing stop operations. For example, to track only Euros:

 if (use trailing) 
    TrailingPosition( "EUR/USD" );

Or just track GBP selling:

 if (use trailing) 
    TrailingPosition( "GBP/USD" , OP_SELL );

If the EA uses magic numbers, it must be passed to the maintenance function:


Attachment download

📎 a-SimpleTrailing.mqh (5.94 KB)

Source: MQL5 #7069

Verification code Refresh