This is a trading tool that I use every single day. I could not live without it for both my live trading and especially my backtesting. It calculates your stop loss size (in pips) based on the current ATR.
How It Works
The first number in blue is the current ATR (pips). The second number in green is your stop loss size for Long trades, and the third number in green is your take profit size for Long trades. The fourth number in red is your stop loss size for Short trades, and the final number in red is your take profit size for Short trades.
For short trades, the stop loss value is calculated by adding the distance in pips between the most recent swing high’s price and the currently selected candle’s close price. The take profit is calculated by subtracting one ATR from your entry price.
For example – if the ATR is 10 pips, and the current candle-close of EUR/USD is 1.13, with the most recent swing high being 1.1325, and you want to enter short, then your stop loss would be at 1.1335 (a size of 35 pips). With a 2:1R, your take profit size would be 70 pips (at 1.1265).
Settings
R/R:
Your risk to reward profile.
ATR Length:
ATR period (how many candles to include in the calculation).
How Far To Look Back For High/Lows:
Candle lookback period for swing high/lows.
Stop Distance (In Pips Or ATR): This controls your ATR multiplier, or sets your S/L in pips.
Use ATR x ^ for stop loss?: If you uncheck this, then the Stop Distance setting will be treated as pips instead of an ATR multiplier.
Source Code
// Created by Matthew J. Slabosz // www.zenandtheartoftrading.com // Last Updated: 1st October, 2020 // @version=4 study("ATR+ (Stop Loss Indicator)", "ATR+", overlay=false, precision=1) // Get user input riskratio = input(title="R/R:", type=input.float, defval=1, minval=0.1) atrLength = input(title="ATR Length:", type=input.integer, defval=14, minval=1) lookback = input(title="How Far To Look Back For High/Lows:", type=input.integer, defval=3, minval=1) stopSizeInput = input(title="Stop Distance (in pips or ATR):", type=input.float, defval=1.0, minval=0.0) stopSizeUseATR = input(title="Use ATR Multiplier for stop loss?", type=input.bool, defval=true) // Custom function to convert pips into whole numbers toWhole(number) => return = atr(14) < 1.0 ? (number / syminfo.mintick) / (10 / syminfo.pointvalue) : number return := atr(14) >= 1.0 and atr(14) < 100.0 and syminfo.currency == "JPY" ? return * 100 : return // Custom function to convert whole numbers back into pips toPips(number) => return = atr(14) >= 1.0 ? number : (number * syminfo.mintick) * (10 / syminfo.pointvalue) return := atr(14) >= 1.0 and atr(14) < 100.0 and syminfo.currency == "JPY" ? return / 100 : return // Calculate ATR & convert it from points into a whole number atrValue = atr(atrLength) atrWhole = toWhole(atrValue) // Convert our fixed pip stop size into a whole number for use later if using a fixed stop size stopSize = toPips(stopSizeInput) // Multiply our stop size by our ATR multiplier (if using ATR multiplier) if stopSizeUseATR stopSize := atrValue * stopSizeInput // Get SL pips longEntrySize = toWhole((close - lowest(low, lookback) + stopSize)) shortEntrySize = toWhole((highest(high, lookback) - close + stopSize)) // Plot data plot(atrWhole, color=color.teal, style=plot.style_circles, title="ATR", transp=100) plot(longEntrySize, color=color.green, style=plot.style_circles, title="Long Entry Size", transp=100) plot(longEntrySize * riskratio, color=color.green, style=plot.style_circles, title="Long Target Size", transp=100) plot(shortEntrySize, color=color.red, style=plot.style_circles, title="Short Entry Size", transp=100) plot(shortEntrySize * riskratio, color=color.red, style=plot.style_circles, title="Short Target Size", transp=100)