Williams %R w/ Bollinger Bands [Loxx]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © loxx

//@version=5
indicator("Williams %R w/ Bollinger Bands [Loxx]", 
     overlay = false, 
     shorttitle="WPRBB [Loxx]",
     timeframe="", 
     timeframe_gaps=true, 
     max_bars_back = 3000)

greencolor = #2DD204
redcolor = #D2042D 

_pr(src, len) =>
	max = ta.highest(len)
	min = ta.lowest(len)
	temp = 100 * (src - max) / (max - min)
    temp

variant(type, src, len) =>
    sig = 0.0
    if type == "SMA"
        sig := ta.sma(src, len) 
    else if type == "EMA"
        sig := ta.ema(src, len) 
    else if type == "WMA"
        sig := ta.wma(src, len)   
    else if type == "RMA"
        sig := ta.rma(src, len)  
    sig
    
f_bb(type, src, length, mult) =>
    float basis = variant(type, src, length)
    float dev = mult * ta.stdev(src, length)
    [basis, basis + dev, basis - dev]
 
src = input.source(close, "WPR Source", group = "Williams %R Settings")
per = input.int(25, "WPR Period", group = "Williams %R Settings")
MA_Period= input.int(3, "WPR Smoothing Period", group = "Williams %R Settings")
type = input.string("SMA", "WPR Smoothing Type", options = ["EMA", "WMA", "RMA", "SMA"], group = "Williams %R Settings")

BB_Period= input.int(89, "BB Period", group = "Bollinger Bands Settings")
BB_Div  = input.float(1., "BB Multiplier", group = "Bollinger Bands Settings")
bbtype  = input.string("SMA", "BB MA Type", options = ["EMA", "WMA", "RMA", "SMA"], group = "Bollinger Bands Settings")

colorbars = input.bool(false, "Color bars?", group = "UI Options")
showsignals = input.bool(false, "Show signals?", group = "UI Options")

out = (_pr(src, per) + 50.0) * 2.0
out := variant(type, out, MA_Period)

[middle, upper, lower] = f_bb(bbtype, out, BB_Period, BB_Div)

colorout = out > upper ? greencolor : out < lower ? redcolor : color.gray

outpl = plot(out, color = colorout, linewidth = 2)
plot(middle, color = color.white)

uppl = plot(upper, color = greencolor)
dnpl = plot(lower, color = redcolor)

fill(outpl, uppl, color = out > upper ? greencolor : na)
fill(outpl, dnpl, color = out < lower ? redcolor : na)

plot(60, color = bar_index % 2 ? color.gray : na)
plot(0, color = bar_index % 2 ? color.gray : na)
plot(-60, color = bar_index % 2 ? color.gray : na)

goLong =  ta.crossover(out, upper)  
goShort =  ta.crossunder(out, lower)  

plotshape(goLong and showsignals, title = "Long", color = greencolor, textcolor = greencolor, text = "L", style = shape.triangleup, location = location.bottom, size = size.auto)
plotshape(goShort and showsignals, title = "Short", color = redcolor, textcolor = redcolor, text = "S", style = shape.triangledown, location = location.top, size = size.auto)

alertcondition(goLong, title="Long", message="Williams %R w/ Bollinger Bands [Loxx]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(goShort, title="Short", message="Williams %R w/ Bollinger Bands [Loxx]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")

barcolor(colorbars ? colorout : na)














