ATR+ (Stop Loss Indicator)
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)
Last Updated: 1st October, 2020
Hi I am getting errors when compiling in mql5.
Please help.
Thanks
Sridhar
This is written in PineScript for Trading View, not MQL5.
Awesome work on the other indicators!! I’m having the following errors when testing this indicator:
Add to Chart operation failed, reason: line 15: Undeclared identifier ‘return’;
line 15: Variable ‘return’ is not found in scope ‘toWhole’, cannot register side effect;
line 33: Undeclared identifier ‘atrWhole’;
line 34: Undeclared identifier ‘longEntrySize’;
line 35: Undeclared identifier ‘longEntrySize’;
line 36: Undeclared identifier ‘shortEntrySize’;
line 37: Undeclared identifier ‘shortEntrySize’
it is not compiing in tradingview
True, there’s an error at line 44
line 44: Undeclared identifier ‘shortEntrySize’
Thanks Jeff & Antonio! My bad, I had a typo on line 16 which broke the script. I just fixed it so it should compile now :)
Hi man!
I’ve copied your ATR+ (Stop Loss Indicator) script to Pinescript, converted it to version 5, saved it and tried to apply it to the chart. In the console it was saying that it was applied, but I can see nothing on the chart.
Any idea on how I could fix this?
Only in currency not S&P
sir i am getting for problem in programing for stoploss and takeprofit in
custom indicator.please help me..