ATR with William %R Signal
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bobbystenly

//@version=5
indicator("ATR with William %R Signal", overlay=true, max_labels_count=500)

color textColor = color.white
color sellColor = color.red
color buyColor = color.green

// getting all the parameters needed
maLength = input.int(10, "MA Length")
wprLength = input.int(10, "William %R Length")
buyMul  = input.float(1.8, "Buy ATR Multiplier", step=0.1)
sellMul = input.float(1.8, "Sell ATR Multiplier", step=0.1)

// calculate William %R
wpr = ta.wpr(wprLength)

// calculate ATR
atr = ta.atr(maLength)

// calculate MA
ma = ta.sma(close, maLength)

plot(ma, color=color.blue)
plot(ma - (buyMul * atr), color=buyColor)
plot(ma + (sellMul * atr), color=sellColor)

// Buy If price currently lower than MA substracts by ATR (with some multiplier)
// To reduce the false signal, check the William %R value and should be on the oversold area and previously reach < -95
if ma - (buyMul * atr) > open and wpr < -80 and wpr[1] < -95 and close > open
    label.new(bar_index, low, text = "", color= buyColor, textcolor=textColor, style = label.style_label_up)
    
// Sell If price currently higher than MA add by ATR (with some multiplier)
// To reduce the false signal, check the William %R value and should be on the overbought area and previously reach > -5
if ma + (sellMul * atr) < close and wpr > -20 and wpr[1] > -5
    label.new(bar_index, high, text = "", color= sellColor, textcolor=textColor)

