You are on page 1of 1

// ATR+ Stop Loss Indicator v2.

0
// Created by Matthew J. Slabosz
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=4
study("ATR Trailing Stop Indicator [Chart]", "S/L+", overlay=true)
// Get ATR inputs
atrLength = input(title="ATR Length", type=input.integer, defval=14, minval=1,
group="ATR Settings")
lookback = input(title="Bars To Look Back For Highs/Lows", type=input.integer,
defval=7, minval=1, group="ATR Settings")
multiplier = input(title="ATR Multiplier", type=input.float, defval=1.0,
minval=0.1, group="ATR Settings")
trailType = input(title="Trailing Stop Type", type=input.string,
defval="High/Low", options=["High/Low", "Close", "Open"], group="ATR Settings")
// Get display table inputs
showATR = input(title="Show ATR?", type=input.bool, defval=true, group="Display
Settings")
atrColor = input(title="ATR Color", type=input.color,
defval=color.new(color.blue, 100), group="Display Settings")
atrLocation = input(title="ATR Location", type=input.string, defval="BR",
options=["TR", "TC", "BR", "BC"], group="Display Settings")
// Calculate data
atr = atr(atrLength)
longStop = (trailType == "High/Low" ? lowest(low, lookback) : trailType ==
"Close" ? close : open) - atr * multiplier
shortStop = (trailType == "High/Low" ? highest(high, lookback) : trailType ==
"Close" ? close : open) + atr * multiplier
// Plot data
plot(longStop, color=color.new(color.green, 0), style=plot.style_linebr,
title="Long Trailing Stop")
plot(shortStop, color=color.new(color.red, 0), style=plot.style_linebr,
title="Short Trailing Stop")
// Create an output table to display average pip change
var table resultTable = table.new(atrLocation == "BR" ? position.bottom_right :
atrLocation == "TR" ? position.top_right : atrLocation == "TC" ?
position.top_center : position.bottom_center, 1, 1, border_width=3)
f_fillCell(_table, _column, _row, _value, _text) =>
_cellText = tostring(_value, "#.#####") + "\n" + _text
table.cell(_table, _column, _row, _cellText, bgcolor=atrColor,
text_color=color.new(atrColor, 0))
// Only fill the cell data on the last bar on the chart (for optimization)
if barstate.islast and showATR
f_fillCell(resultTable, 0, 0, atr, "ATR")

You might also like