Disclaimer: I'm a retired software developer, NOT a pro trader.
I've been using this EA to exit trades on the M15 chart, as an alternative to a fixed pip trailing SL. Personally I like automated exits, as (obviously) they operate automatically and objectively, removing hunches and emotion from the equation. This EA suits my personal approach, obviously it's not going to be useful for everybody. I'm sharing it on a “take it or leave it” basis, as I don't intend spending hours answering questions, or offering a programming service to customize it to meet individual requirements. If it's useful to somebody in its existing form, that's cool; if not, well it's an unsupported freebee, so please move on. If you're going to use it on a live account, then you do so AT YOUR OWN RISK. I make no guarantees that it is bug-free, that it's fit for purpose, or that it will behave in exactly the way that you expect it to. I also accept no liability whatsoever for any computer damage or financial losses, irrespective of whether the EA is used correctly or not.
The key difference to simply using a trailing stop is that the price candle must CLOSE above/below the trailing pink line, for the EA to exit the trade, on a sell/buy position respectively. The EA ignores candle wicks, allowing for the trade to remain open for longer, following the “let profits run” maxim. Of course the trade-off is that the EA waits for each candle to close; hence if a large adverse candle suddenly occurs, its closing price might be well outside where a trailing SL would have got you out, causing an exit at a worse price.
Please note that when a trade is opened, the ATR exit can be on the ‘wrong side’ of price. The EA's trailing exit facility doesn't take effect until the trade is sufficiently in profit that the ATR exit jumps across to the other side. See the screenshot below.
The EA requires that the attached ATR Stops_v1.1 indicator is present in your ...\MQL4\Indicators folder.
I'm not posting the .mq4 source as the EA was written using MQL4 build 509, and hence if compiled using a MT4 version later than Feb 2014, it will likely compile with a zillion errors. Also, there's no MT5 version, as I have no experience coding MQL5.
Some notes on the parameter settings:
string EAname = "ATR-";
Any objects created by the EA will be named with this prefix
string AllowableOrders = "BS";
Include: B = allow buy orders; S = allow sell orders; BS = allow both types
string CandleTimeframe = "+0";
+0 = current chart's TF, +1 = next longer TF, -1 = next shorter TF, etc, allowing the EA to work relative to the chart's TF
Or you can simply enter M5, M15, H1, D1, etc, to lock the ‘pink line’ (trailing exit) to a fixed TF, independent of the chart's TF
int ATRS_Length = 1; // was 10 // if <=0, disable
int ATRS_ATRperiod = 3; // was 5
double ATRS_Kv = 1.5; // was 2.5
These are the parameters for the ATR Stops_v1.1 indicator
If the indy is also attached to the same chart as the EA, make sure that both use the same parameter settings, otherwise their outcomes will not match
bool AdjustForSpread = true;
If true, this will factor the instrument's spread into the calculation
bool TrailOnlyAfterBEreached = true; // true = trail SL only after BE reached; false = trail SL from outset
string ProfitPipsForSLtoBE = "-1"; // negative/zero value = don't move SL to BE; positive value = move SL to BE when order is N pips in profit
double BEpointPips = 0; // BE point relative to entry
The EA also provides an independent 'Move SL to breakeven' facility. To switch this facility off, leave ProfitPipsForSLtoBE as -1. Otherwise, if you want the EA to advance the SL to BE when the trade is X pips in profit, enter X (as a positive value) into ProfitPipsForSLtoBE. BEpointPips is the level that you want the SL moved to: 0 is BE, a negative value -Y means Y pips below BE (if stopped out, a loss of Y pips would occur), a positive value Y means Y pips beyond BE (if stopped out, a profit of Y pips would occur). Obviously BEpointPips must be < ProfitPipsForSLtoBE. Also, please note: I use this facility only on the 28 major currency pairs, because with exotics, metals, indices, cryptos etc, a 'pip' is not necessarily defined in the same way by different brokers.
string TextInComment = "atr_exit";
This is important, as only positions with this text somewhere in the Trade Comment will be operated on by the EA (leave blank to remove this restriction; that means that all orders for the chart's instrument will potentially be acted upon)
color ArrowColorBuy = Green;
color ArrowColorSell = Red;
int MagicNumberBuy = 1001;
int MagicNumberSell = 1002;
All of these are reserved for future use
int NumberOfAttempts = 12;
double SecondsBetweenAttempts = 0.5;
The EA operates on every price tick. This is the number of attempts, and how many seconds between each attempt, for it to check the criteria and potentially apply the exit. Of course after an order is closed, it can't be operated on, and these settings become irrelevant for this order. If multiple orders exist for the chart's instrument, and they all meet the TextInComment criteria, then each order is processed separately and independently
int ADRdays = 30;
The number of days for the ADR calculation
string SnapshotFile = "ATR exit screenshots\#atr—";
If non-blank, this will output a screenshot of the chart, at the moment that the exit occurred, into the specified folder, and with the specified name prefix
If blank, no screenshot will be output
int PostDiagnosticInfo = 3;
If set to 3, this will output diagnostic info every time the EA attempts to close an order, into the file named EAname + "debug_" + Symbol() + "," + timeframe + ".txt" in the ...\MQL4\Files folder
If set to 0, this output is not performed
bool DisplayComments = false;
Reserved for future use
Here's a screenshot of a buy trade of mine that unfortunately got stopped out ( at the yellow arrow; if only I'd had the initial SL > 0.6 pips further away!!)
If the trade had stuck, it would have generated 180 pips profit (see the green arrow) for 10.5 pips risk. This is a (cherrypicked, obviously) example to show how the pink line (created by the ATR Stops_v 1.1 indicator) exits can potentially work, and by ignoring candle wicks, keeps positions open for longer.
Here's another screenshot, showing how the EA/indicator more typically works:
EA (install in ...\MQL4\Experts folder):
Indicator (required by the EA; (install in ...\MQL4\Indicators folder):
I've been using this EA to exit trades on the M15 chart, as an alternative to a fixed pip trailing SL. Personally I like automated exits, as (obviously) they operate automatically and objectively, removing hunches and emotion from the equation. This EA suits my personal approach, obviously it's not going to be useful for everybody. I'm sharing it on a “take it or leave it” basis, as I don't intend spending hours answering questions, or offering a programming service to customize it to meet individual requirements. If it's useful to somebody in its existing form, that's cool; if not, well it's an unsupported freebee, so please move on. If you're going to use it on a live account, then you do so AT YOUR OWN RISK. I make no guarantees that it is bug-free, that it's fit for purpose, or that it will behave in exactly the way that you expect it to. I also accept no liability whatsoever for any computer damage or financial losses, irrespective of whether the EA is used correctly or not.
The key difference to simply using a trailing stop is that the price candle must CLOSE above/below the trailing pink line, for the EA to exit the trade, on a sell/buy position respectively. The EA ignores candle wicks, allowing for the trade to remain open for longer, following the “let profits run” maxim. Of course the trade-off is that the EA waits for each candle to close; hence if a large adverse candle suddenly occurs, its closing price might be well outside where a trailing SL would have got you out, causing an exit at a worse price.
Please note that when a trade is opened, the ATR exit can be on the ‘wrong side’ of price. The EA's trailing exit facility doesn't take effect until the trade is sufficiently in profit that the ATR exit jumps across to the other side. See the screenshot below.
The EA requires that the attached ATR Stops_v1.1 indicator is present in your ...\MQL4\Indicators folder.
I'm not posting the .mq4 source as the EA was written using MQL4 build 509, and hence if compiled using a MT4 version later than Feb 2014, it will likely compile with a zillion errors. Also, there's no MT5 version, as I have no experience coding MQL5.
Some notes on the parameter settings:
string EAname = "ATR-";
Any objects created by the EA will be named with this prefix
string AllowableOrders = "BS";
Include: B = allow buy orders; S = allow sell orders; BS = allow both types
string CandleTimeframe = "+0";
+0 = current chart's TF, +1 = next longer TF, -1 = next shorter TF, etc, allowing the EA to work relative to the chart's TF
Or you can simply enter M5, M15, H1, D1, etc, to lock the ‘pink line’ (trailing exit) to a fixed TF, independent of the chart's TF
int ATRS_Length = 1; // was 10 // if <=0, disable
int ATRS_ATRperiod = 3; // was 5
double ATRS_Kv = 1.5; // was 2.5
These are the parameters for the ATR Stops_v1.1 indicator
If the indy is also attached to the same chart as the EA, make sure that both use the same parameter settings, otherwise their outcomes will not match
bool AdjustForSpread = true;
If true, this will factor the instrument's spread into the calculation
bool TrailOnlyAfterBEreached = true; // true = trail SL only after BE reached; false = trail SL from outset
string ProfitPipsForSLtoBE = "-1"; // negative/zero value = don't move SL to BE; positive value = move SL to BE when order is N pips in profit
double BEpointPips = 0; // BE point relative to entry
The EA also provides an independent 'Move SL to breakeven' facility. To switch this facility off, leave ProfitPipsForSLtoBE as -1. Otherwise, if you want the EA to advance the SL to BE when the trade is X pips in profit, enter X (as a positive value) into ProfitPipsForSLtoBE. BEpointPips is the level that you want the SL moved to: 0 is BE, a negative value -Y means Y pips below BE (if stopped out, a loss of Y pips would occur), a positive value Y means Y pips beyond BE (if stopped out, a profit of Y pips would occur). Obviously BEpointPips must be < ProfitPipsForSLtoBE. Also, please note: I use this facility only on the 28 major currency pairs, because with exotics, metals, indices, cryptos etc, a 'pip' is not necessarily defined in the same way by different brokers.
string TextInComment = "atr_exit";
This is important, as only positions with this text somewhere in the Trade Comment will be operated on by the EA (leave blank to remove this restriction; that means that all orders for the chart's instrument will potentially be acted upon)
color ArrowColorBuy = Green;
color ArrowColorSell = Red;
int MagicNumberBuy = 1001;
int MagicNumberSell = 1002;
All of these are reserved for future use
int NumberOfAttempts = 12;
double SecondsBetweenAttempts = 0.5;
The EA operates on every price tick. This is the number of attempts, and how many seconds between each attempt, for it to check the criteria and potentially apply the exit. Of course after an order is closed, it can't be operated on, and these settings become irrelevant for this order. If multiple orders exist for the chart's instrument, and they all meet the TextInComment criteria, then each order is processed separately and independently
int ADRdays = 30;
The number of days for the ADR calculation
string SnapshotFile = "ATR exit screenshots\#atr—";
If non-blank, this will output a screenshot of the chart, at the moment that the exit occurred, into the specified folder, and with the specified name prefix
If blank, no screenshot will be output
int PostDiagnosticInfo = 3;
If set to 3, this will output diagnostic info every time the EA attempts to close an order, into the file named EAname + "debug_" + Symbol() + "," + timeframe + ".txt" in the ...\MQL4\Files folder
If set to 0, this output is not performed
bool DisplayComments = false;
Reserved for future use
Here's a screenshot of a buy trade of mine that unfortunately got stopped out ( at the yellow arrow; if only I'd had the initial SL > 0.6 pips further away!!)
If the trade had stuck, it would have generated 180 pips profit (see the green arrow) for 10.5 pips risk. This is a (cherrypicked, obviously) example to show how the pink line (created by the ATR Stops_v 1.1 indicator) exits can potentially work, and by ignoring candle wicks, keeps positions open for longer.
Here's another screenshot, showing how the EA/indicator more typically works:
EA (install in ...\MQL4\Experts folder):
Attached File(s)
Indicator (required by the EA; (install in ...\MQL4\Indicators folder):
Attached File(s)