Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Experts

TrailingStopAndTake - expert for MetaTrader 5

Views:
13724
Rating:
(42)
Published:
2018.03.01 12:00
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

This non-trading Expert Advisor is designed for the simple trailing of stop loss and take profit orders of open positions.

The EA only works on hedging accounts.

Principle of operation: when a new position is opened, the EA checks its stop-orders. If a stop order is not found, the EA sets one at the distance specified in the EA parameters. Then the EA starts to move the position take profit and/or stop loss, if the price moves farther than the distance specified in the settings.

You can set position type - any/buy/sell, the position symbol (any or the specified one), the magic number (any or the specified one) or the position ticket (any or the specified one). It is also possible to set trailing in the losing area - if this mode is not selected, stop orders will not be trailed in the losing area. The breakeven value can be specified in the EA parameters.

To check the trailing operation in all modes, the EA can open positions on each new bar. This mode can only be applied in the Strategy Tester.

The EA has 12 input parameters:

  • Initial StopLoss size in points (0 - no stop) - if a position does not have a stop loss, then a stop loss order with the specified value will be added immediately after position opening. If the parameter is equal to 0, stop loss will not be placed;
  • Initial TakeProfit size in points (0 - no take) - if a position does not have a take profit, then a take profit order of the specified size will be added immediately after position opening. If the parameter is equal to 0, take profit will not be placed;
  • TrailingStop size in points (0 - no trail) - trailing parameter, stop loss distance from the price. If the parameter is equal to 0, stop loss will not be trailed;
  • TrailingTake size in points (0 - no trail) - trailing parameter, take profit distance from the price. If the parameter is equal to 0, take profit will not be trailed;
  • Positions type - the type of positions to trail;
  • Positions symbol ("" - any symbol) - the symbol of positions to trail (an empty value means all symbols);
  • Positions magic number (0 - any magic) - the magic number of positions to trail;
  • Position ticket (0 - all tickets) - the ticket of positions to trail;
  • Trailing Step - trailing step. If the price moves away from the stop order at a distance greater than the step, the stop order will be modified so as to have the size specified in appropriate settings TrailingStop size in points and/or TrailingTake size in points;
  • Trailing in the unprofitable zone - enable or disable trailing in the losing area. If disabled, stop orders will not be set in the loss zone;
  • Breakeven in points - breakeven value. A losing area starts below this value;
  • Spread multiplier - spread multiplier for calculating a correct distance for stop orders.
For Stop Loss and Take Profit orders (as well as pending orders), there is a minimum allowed distance for placing such orders - StopLevel. I.e. Stop Loss and Take Profit (as well as pending orders) cannot be placed closer than this distance to the price. If StopLevel is zero, it does not mean its absence, it only means that StopLevel is floating. In this case, the minimum distance is usually equal to spread*, but sometimes even the double spread is not enough. So, this parameter allows specifying a custom multiplier for the spread used to calculate the minimum distance for placing stop orders.

Fig.1 Initial StopLoss=600, initial TakeProfit=1200, TrailingStop=300, take profit trailing is disabled, trailing in the loss area is disabled.

Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/19963

Vector Vector

An oscillator of market movement, mood and trend.

Patterns Patterns

A set of thirty popular candlestick patterns.

Invest System 4.5 Invest System 4.5

A trading system based on the account balance and the result of the last trade.

Channels Channels

Trading in a channel based on a Moving Average with the period of 220 and three Envelopes with the periods of 220 and different heights. Position trailing.