Williams %R with multiple periods
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © insideandup
// @version=5
//
// Description: Williams %R technical analysis oscillator by Larry Williams.
// Calculation: %R = (period high - most recent period's close price)/(period high - period low)
// Script feature: 9 customizable periods and fills.
// Default periods: 10, 13, 21, 34, 55, 89, 144, 233, 377. 
// Default colors: "Solarized" color scheme created by Ethan Schoonover at ethanschoonover.com.
// Created: Dec 2021
// Last Edited: 28 July 2023

indicator("Williams %R by 3iau", shorttitle="%R", format=format.price, precision=3, timeframe="", timeframe_gaps=true)

// VARIABLES

// Tillson's Moving Average (T3) variables

a = 0.618033989 // Tillson applied a = 0.7, here applying the Golden ratio conjugate = |(1-√5)/2| = 0.618033989
a1 = -1*math.pow(a, 3)
a2 = 3*math.pow(a, 2) + 3*math.pow(a, 3)
a3 = -6*math.pow(a, 2) - 3*a - 3*math.pow(a, 3)
a4 = 1 + 3*a + math.pow(a, 3) + 3*math.pow(a, 2)

// Moving average type selection

ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) :
     type == "EMA" ? ta.ema(source, length) :
     type == "DEMA" ? 2 * ta.ema(source, length) - ta.ema(ta.ema(source, length), length) :
     type == "TEMA" ? 3 * ta.ema(source, length) - 3 * ta.ema(ta.ema(source, length), length) + ta.ema(ta.ema(ta.ema(source, length), length), length) :
     type == "QEMA" ? 5 * ta.ema(source, length) - 10 * ta.ema(ta.ema(source, length), length) + 10 * ta.ema(ta.ema(ta.ema(source, length), length), length) - 5 * ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length) + ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length) :
     type == "PEMA" ? 8 * ta.ema(source, length) - 28 * ta.ema(ta.ema(source, length), length) + 56 * ta.ema(ta.ema(ta.ema(source, length), length), length) - 70 * ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length) + 56 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length) - 28 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length) + 8 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length), length) - ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length), length), length) :
     type == "ZLEMA" ? ta.ema(source + (source - source[math.round((length - 1) / 2)]), length) :
     type == "T3" ? a1 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length), length) + a2 * ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length), length) + a3 * ta.ema(ta.ema(ta.ema(ta.ema(source, length), length), length), length) + a4 * ta.ema(ta.ema(ta.ema(source, length), length), length) :
     type == "HMA" ? ta.hma(source, length) :
     type == "SMMA" ? ta.rma(source, length) :
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na


// WILLIAMS PERCENT R 00 AND ITS MA CALCULATIONS

// Source type selection

source0 = input.string("Close", title = "Source", options = ["Open", "High", "Low", "Close", "Median HL/2", "Typical HLC/3", "OHLC/4", "Body Median OC/2", "Weighted Close HL2C/4", "Biased HC/2 if Close > Open, else LC/2", "Biased High if Close > HL/2, else Low", "Biased High if Close > Open, else Low"], inline = "WR0", group = "Display Williams %R and its Moving Average")

src0 = 1.0

if (source0 == "Open")
    src0 := open
else
    if (source0 == "High")
        src0 := high
    else
        if (source0 == "Low")
            src0 := low
        else
            if (source0 == "Close")
                src0 := close
            else
                if (source0 == "Median HL/2")
                    src0 := hl2
                else
                    if (source0 == "Typical HLC/3")
                        src0 := hlc3
                    else
                        if (source0 == "OHLC/4")
                            src0 := ohlc4
                        else
                            if (source0 == "Body Median OC/2")
                                src0 := (open + close)/2
                            else
                                if (source0 == "Weighted Close HL2C/4")
                                    src0 := (high + low + 2 * close) / 4
                                else
                                    if (source0 == "Biased HC/2 if Close > Open, else LC/2")
                                        src0 := close > open ? (high + close) / 2 : (low + close) / 2
                                    else
                                        if (source0 == "Biased High if Close > HL/2, else Low")
                                            src0 := close > hl2 ? high : low
                                        else
                                            if (source0 == "Biased High if Close > Open, else Low")
                                                src0 := close > open ? high : low


show_wr0 = input(defval=true, title="", inline="wr0", group = "Display Williams %R and its Moving Average")
length0 = input.int(defval=10, minval=1, maxval=2000, title="%R 00", inline="wr0", group="Display Williams %R and its Moving Average")
price0(length0) =>
	max0 = ta.highest(length0)
	min0 = ta.lowest(length0)
	100 * (src0 - max0) / (max0 - min0)
percentR0 = price0(length0)

show_wr0_ma = input(defval=false, title="", inline="MA", group = "Display Williams %R and its Moving Average")
wr0_ma_length = input.int(defval=50, title="MA 00", inline = "MA", group="Display Williams %R and its Moving Average")
wr0_ma_type = input.string(defval="HMA", title="", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA"], inline = "MA", group = "Display Williams %R and its Moving Average")
wr0_ma = ma(percentR0, wr0_ma_length, wr0_ma_type)

// WILLIAMS PERCENT R 01-08 CALCULATIONS

source = input.string("Close", title = "Source", options = ["Open", "High", "Low", "Close", "Median HL/2", "Typical HLC/3", "OHLC/4", "Body Median OC/2", "Weighted Close HL2C/4", "Biased HC/2 if Close > Open, else LC/2", "Biased High if Close > HL/2, else Low", "Biased High if Close > Open, else Low"], inline = "WR0", group = "Display Specified Williams %R")

src = 1.0

if (source == "Open")
    src := open
else
    if (source == "High")
        src := high
    else
        if (source == "Low")
            src := low
        else
            if (source == "Close")
                src := close
            else
                if (source == "Median HL/2")
                    src := hl2
                else
                    if (source == "Typical HLC/3")
                        src := hlc3
                    else
                        if (source == "OHLC/4")
                            src := ohlc4
                        else
                            if (source == "Body Median OC/2")
                                src := (open + close)/2
                            else
                                if (source == "Weighted Close HL2C/4")
                                    src := (high + low + 2 * close) / 4
                                else
                                    if (source == "Biased HC/2 if Close > Open, else LC/2")
                                        src := close > open ? (high + close) / 2 : (low + close) / 2
                                    else
                                        if (source == "Biased High if Close > HL/2, else Low")
                                            src := close > hl2 ? high : low
                                        else
                                            if (source == "Biased High if Close > Open, else Low")
                                                src := close > open ? high : low


show_wr1 = input(defval=true, title="", inline="wr1", group = "Display Specified Williams %R")
length1 = input.int(defval=13, minval=1, maxval=2000, title="%R 01", inline="wr1", group="Display Specified Williams %R")
price1(length1) =>
	max1 = ta.highest(length1)
	min1 = ta.lowest(length1)
	100 * (src - max1) / (max1 - min1)
percentR1 = price1(length1)

show_wr2 = input(defval=true, title="", inline="wr2", group = "Display Specified Williams %R")
length2 = input.int(defval=21, minval=1, maxval=2000, title="%R 02", inline="wr2", group="Display Specified Williams %R")
price2(length2) =>
	max2 = ta.highest(length2)
	min2 = ta.lowest(length2)
	100 * (src - max2) / (max2 - min2)
percentR2 = price2(length2)

show_wr3 = input(defval=true, title="", inline="wr3", group = "Display Specified Williams %R")
length3 = input.int(defval=34, minval=1, maxval=2000, title="%R 03", inline="wr3", group="Display Specified Williams %R")
price3(length3) =>
	max3 = ta.highest(length3)
	min3 = ta.lowest(length3)
	100 * (src - max3) / (max3 - min3)
percentR3 = price3(length3)

show_wr4 = input(defval=true, title="", inline="wr4", group = "Display Specified Williams %R")
length4 = input.int(defval=55, minval=1, maxval=2000, title="%R 04", inline="wr4", group="Display Specified Williams %R")
price4(length4) =>
	max4 = ta.highest(length4)
	min4 = ta.lowest(length4)
	100 * (src - max4) / (max4 - min4)
percentR4 = price4(length4)

show_wr5 = input(defval=true, title="", inline="wr5", group = "Display Specified Williams %R")
length5 = input.int(defval=89, minval=1, maxval=2000, title="%R 05", inline="wr5", group="Display Specified Williams %R")
price5(length5) =>
	max5 = ta.highest(length5)
	min5 = ta.lowest(length5)
	100 * (src - max5) / (max5 - min5)
percentR5 = price5(length5)

show_wr6 = input(defval=true, title="", inline="wr6", group = "Display Specified Williams %R")
length6 = input.int(defval=144, minval=1, maxval=2000, title="%R 06", inline="wr6", group="Display Specified Williams %R")
price6(length6) =>
	max6 = ta.highest(length6)
	min6 = ta.lowest(length6)
	100 * (src - max6) / (max6 - min6)
percentR6 = price6(length6)

show_wr7 = input(defval=true, title="", inline="wr7", group = "Display Specified Williams %R")
length7 = input.int(defval=233, minval=1, maxval=2000, title="%R 07", inline="wr7", group="Display Specified Williams %R")
price7(length7) =>
	max7 = ta.highest(length7)
	min7 = ta.lowest(length7)
	100 * (src - max7) / (max7 - min7)
percentR7 = price7(length7)

show_wr8 = input(defval=true, title="", inline="wr8", group = "Display Specified Williams %R")
length8 = input.int(defval=377, minval=1, maxval=2000, title="%R 08", inline="wr8", group="Display Specified Williams %R")
price8(length8) =>
	max8 = ta.highest(length8)
	min8 = ta.lowest(length8)
	100 * (src - max8) / (max8 - min8)
percentR8 = price8(length8)

// PLOTS

// Percent R

p0=plot(show_wr0 ? percentR0 : na, title="%R0", color=color.rgb(42, 161, 152, 20), linewidth=1)//cyan
p0_ma = plot(show_wr0_ma ? wr0_ma : na, title="%R0 MA", color=color.rgb(42, 161, 152, 50), linewidth=1)

p1=plot(show_wr1 ? percentR1 : na, title="%R1", color=color.rgb(42, 161, 152, 0), linewidth=1)//cyan
p2=plot(show_wr2 ? percentR2 : na, title="%R2", color=color.rgb(38, 139, 210, 5), linewidth=1)//blue
p3=plot(show_wr3 ? percentR3 : na, title="%R3", color=color.rgb(108, 113, 196, 10), linewidth=1)//brmagenta
p4=plot(show_wr4 ? percentR4 : na, title="%R4", color=color.rgb(220, 50, 47, 20), linewidth=1)//red
p5=plot(show_wr5 ? percentR5 : na, title="%R5", color=color.rgb(203, 75, 22, 30), linewidth=1)//brred
p6=plot(show_wr6 ? percentR6 : na, title="%R6", color=color.rgb(181, 137, 0, 40), linewidth=1)//yellow
p7=plot(show_wr7 ? percentR7 : na, title="%R7", color=color.rgb(133, 153, 0, 50), linewidth=1)//green
p8=plot(show_wr8 ? percentR8 : na, title="%R8", color=color.rgb(42, 161, 152, 60), linewidth=1)//cyan

fill(p0, p0_ma, title="R0 R0MA Fill", color = show_wr0_ma ? percentR0 > wr0_ma ? color.rgb(238, 232, 213, 90) : color.rgb(220, 50, 47, 90) : na) // red
fill(p0, p1, title="R0R1 Fill", color=color.rgb(42, 161, 152, 50))//cyan
fill(p1, p2, title="R1R2 Fill", color=color.rgb(38, 139, 210, 50))//blue
fill(p2, p3, title="R2R3 Fill", color=color.rgb(108, 113, 196, 55))//brmagenta
fill(p3, p4, title="R3R4 Fill", color=color.rgb(220, 50, 47, 60))//red
fill(p4, p5, title="R4R5 Fill", color=color.rgb(203, 75, 22, 65))//brred
fill(p5, p6, title="R5R6 Fill", color=color.rgb(181, 137, 0, 70))//yellow
fill(p6, p7, title="R6R7 Fill", color=color.rgb(133, 153, 0, 75))//green
fill(p7, p8, title="R7R8 Fill", color=color.rgb(42, 161, 152, 80))//cyan

// Indicator horizontal lines

obPlot = hline(-20, title="Upper Band", linestyle=hline.style_solid, color=color.rgb(133, 153, 0, 90), linewidth=1)//green
hline(-35, title="Middle Level", linestyle=hline.style_dotted, color=color.rgb(133, 153, 0, 60), linewidth=1)//green
hline(-50, title="Center Level", linestyle=hline.style_dotted, color=color.rgb(133, 153, 0, 0), linewidth=1)//green
hline(-65, title="Middle Level", linestyle=hline.style_dotted, color=color.rgb(133, 153, 0, 60), linewidth=1)//green
osPlot = hline(-80, title="Lower Band", linestyle=hline.style_solid, color=color.rgb(133, 153, 0, 90), linewidth=1)//green
fill(obPlot, osPlot, title="Background", color=color.rgb(181, 137, 0, 97))//yellow


//NOTES
//
//Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584...
//
//COLOR SCHEMES
//
//SOLARIZED by Ethan Schoonover at ethanschoonover.com
//NAME       HEX      RGB
//brblack    #002b36  0, 43, 54
//black      #073642  7, 54, 66
//brgreen    #586e75  88, 110, 117
//bryellow   #657b83  101, 123, 131
//brblue     #839496  131, 148, 150
//brcyan     #93a1a1  147, 161, 161
//white      #eee8d5  238, 232, 213
//brwhite    #fdf6e3  253, 246, 227
//yellow     #b58900  181, 137, 0
//brred      #cb4b16  203, 75, 22
//red        #dc322f  220, 50, 47
//magenta    #d33682  211, 54, 130
//brmagenta  #6c71c4  108, 113, 196
//blue       #268bd2  38, 139, 210
//cyan       #2aa198  42, 161, 152
//green      #859900  133, 153, 0
//
// ONE HALF DARK by Son A. Pham at github.com/sonph/onehalf
//            HEX      RGB
// black      #282c34  40, 44, 52
// red        #e06c75  224, 108, 117
// green      #98c379  152, 195, 121
// yellow     #e5c07b  229, 192, 123
// blue       #61afef  97, 175, 239
// magenta    #c678dd  198, 120, 221
// cyan       #56b6c2  86, 182, 194
// white      #dcdfe4  220, 223, 228
// foreground #dcdfe4  220, 223, 228
// background #282c34  40, 44, 52
//
//END
