How to set stop loss based on the ATR indicator?

14 April 2024

We often get the question of how to set the stop loss (SL) to a value using the ATR indicator, instead of using a fixed SL value. In this blog, I'll explain how to achieve this using the EA Builder Pro tool.

Preconditions

If you're used to the standard Stop Loss module, you've probably set SL to a fixed value, which is always set right after opening an order.
You may want to delay setting the SL value. The Stop Loss Trailing module will give you that flexibility by setting the threshold, which has to be gained, before SL is set.
If you want more flexibility, the Stop Loss - Conditional module will allow you to set preconditions, by allowing the usage of indicators or functions, or both. An example of a precondition would be that you want to set SL when the price passes the previous candle's close price.

Setting the SL value

Previously mentioned modules give increasing flexibility for deciding when SL is set, hence the name 'precondition', but the value of SL has so far been a fixed value in pips relative to the current price.
If you want flexibility for all aspects of SL, you can use the Stop Loss - Conditional (Pro) module, which allows you to set the value of SL, in addition to the precondition, using indicators and functions.

The ATR example

In this example, we're going to use 2 indicators to set SL. The ATR indicator and the Bar Low indicator. Please add both indicators to your EA before proceding. By setting the SL based on ATR and Bar Low, we set SL depending on market conditions, rather than a fixed value. So we need to use the Stop Loss - Conditional (Pro) module.

Precondition of the ATR example

In this example, we want to set the value of SL the moment the order is opened, so we leave the precondition empty.

SL value of the ATR example

In this example, we'll set the SL to:
1,5 times ATR below the previous candle's low.

A more mathematical way to say this is:
previous candle's Low - current candle's ATR * 1.5.

In the language of MetaTrader this is:
Low (Shift 1) - ATR (Shift 0) * 1.5.

Finally, this is how to configure this module in the way we described:

stop loss conditional - pro module setup with ATR