Williams %R & RSI 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/
// © Nathan J. Hart (insideandup)
// @version=5
//
// DESCRIPTION
// 1. Calculates %R and RSI with multiple period lengths. 
//    1 period length value is defined by User. 
//    8 period length values follow User's selection of classic number sequences, e.g. Fibonacci, Leonardo, Lucas, Narayana, etc.
// 2. User selects which indicator and periods to display or hide.
//
// DEFAULTS
// %R default custom period: 10.
// RSI default custom period: 14.
// %R & RSI default number sequence periods: Lucas numbers 11, 18, 29, 47, 76, 123, 199, 322.
//
// CALCULATIONS
// %R = (period high - most recent period's close price)/(period high - period low)
// RSI = 100 - 1 / (100 + RS), where RS = SMMA(up, period) / SMMA(down, period)
//
// PURPOSE
// 1. Identify price trends.
//
// CREDITS
// 1. Williams %R technical analysis momentum oscillator by Larry Williams.
// 2. Wilder's Relative Strength Index technical analysis momentum oscillator by J. Welles Wilder.
// 3. "Solarized" color scheme by Ethan Schoonover.
//
// CREATED: 31 Dec 2021.
// LAST EDITED: 31 Dec 2021.


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

// VARIABLES

// Tillson's Moving Average
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

// numbers sequences

// Select the number sequence that defines period lengths for moving averages ("short" 11 periods 0-10, "mid" 14 periods 0-13, and "long" 16 periods 0-15 sets). 
//
// position:    0,  1,  2,  3,      4,      5,      6,      7,      8,      9,      10,     
//
// Fibonacci:   X,  13, 21, 34,     55,     89,     144,    233,    377,    610,    987.
// Leonardo:    X,  15, 25, 41,     67,     109,    177,    287,    465,    -,      753.
// Lucas:       X,  11, 18, 29,     47,     76,     123,    199,    322,    521,    843.
// Pell:        X,  12, 29, 70,     169,    408,    985,    2378,   -,      -,      -.
// Padovan:     X,  12, 16, 21,     28,     37,     49,     65,     86,     114,    151,    200,    265,    351,    465,    616.
// Perrin:      X,  12, 17, 22,     29,     39,     51,     68,     90,     119,    158,    209,    277,    367,    486,    644.
// Jacobsthal:  X,  11, 21, 43,     85,     171,    341,    683,    1365,   2731    -.
// Catalan:     X,  14, 42, 132,    429,    1430,   4862,   -,      -,      -,      -.
// Narayana:    X,  13, 19, 28,     41,     60,     88,     129,    189,    277,    406.
// Magic:       X,  15, 34, 65,     111,    175,    260,    369,    505,    671,    870.
// Tetrahedral: X,  20, 35, 56,     84,     120,    165,    220,    286,    364,    455.
// Mersenne:    X,  13, 17, 19,     31,     61,     89,     107,    127,    521,    607.
// 

// sub %R lengths defined by selected number sequence

wr_sequence = input.string("Lucas Numbers", "Period Lengths", inline = "Period Lengths", options = ["Fibonacci Numbers", "Leonardo Numbers", "Lucas Numbers", "Padovan Numbers", "Perrin Numbers", "Jacobsthal Numbers", "Narayana Numbers", "Magic Constants", "Tetrahedral Numbers", "Mersenne Exponents"], group = "Select the Number Sequence from which Calculations' Period Lengths are Defined.")

wr_1_length = 0
wr_2_length = 0
wr_3_length = 0
wr_4_length = 0
wr_5_length = 0
wr_6_length = 0
wr_7_length = 0
wr_8_length = 0

if (wr_sequence == "Fibonacci Numbers") 
    wr_1_length := 13
    wr_2_length := 21
    wr_3_length := 34
    wr_4_length := 55
    wr_5_length := 89
    wr_6_length := 144
    wr_7_length := 233
    wr_8_length := 377
else
    if (wr_sequence == "Leonardo Numbers")
        wr_1_length := 15
        wr_2_length := 25
        wr_3_length := 41
        wr_4_length := 67
        wr_5_length := 109
        wr_6_length := 177
        wr_7_length := 287
        wr_8_length := 465
    else
        if (wr_sequence == "Lucas Numbers")
            wr_1_length := 11
            wr_2_length := 18
            wr_3_length := 29
            wr_4_length := 47
            wr_5_length := 76
            wr_6_length := 123
            wr_7_length := 199
            wr_8_length := 322
        else
            if (wr_sequence == "Padovan Numbers")
                wr_1_length := 12
                wr_2_length := 16
                wr_3_length := 21
                wr_4_length := 28
                wr_5_length := 37
                wr_6_length := 49
                wr_7_length := 65
                wr_8_length := 86
            else
                if (wr_sequence == "Perrin Numbers")
                    wr_1_length := 12
                    wr_2_length := 17
                    wr_3_length := 22
                    wr_4_length := 29
                    wr_5_length := 39
                    wr_6_length := 51
                    wr_7_length := 68
                    wr_8_length := 90
                else
                    if (wr_sequence == "Jacobsthal Numbers")
                        wr_1_length := 11
                        wr_2_length := 21
                        wr_3_length := 43
                        wr_4_length := 85
                        wr_5_length := 171
                        wr_6_length := 341
                        wr_7_length := 683
                        wr_8_length := 1365
                    else
                        if (wr_sequence == "Narayana Numbers")
                            wr_1_length := 13
                            wr_2_length := 19
                            wr_3_length := 28
                            wr_4_length := 41
                            wr_5_length := 60
                            wr_6_length := 88
                            wr_7_length := 129
                            wr_8_length := 189
                        else
                            if (wr_sequence == "Magic Constants")
                                wr_1_length := 15
                                wr_2_length := 34
                                wr_3_length := 65
                                wr_4_length := 111
                                wr_5_length := 175
                                wr_6_length := 260
                                wr_7_length := 369
                                wr_8_length := 505
                            else
                                if (wr_sequence == "Tetrahedral Numbers")
                                    wr_1_length := 20
                                    wr_2_length := 35
                                    wr_3_length := 56
                                    wr_4_length := 84
                                    wr_5_length := 120
                                    wr_6_length := 165
                                    wr_7_length := 220
                                    wr_8_length := 286
                                else
                                    if (wr_sequence == "Mersenne Exponents")
                                        wr_1_length := 13
                                        wr_2_length := 17
                                        wr_3_length := 19
                                        wr_4_length := 31
                                        wr_5_length := 61
                                        wr_6_length := 89
                                        wr_7_length := 107
                                        wr_8_length := 127

// Williams %R

// define first/fastest %R length, default length = 10

show_wr0 = input(true, "", inline="WR0", group = "Display Williams %R and its Moving Average")
length0 = input(10, "Length", inline = "WR0", group = "Display Williams %R and its Moving Average")

wr_aveprice = input.string("Close", title = "", 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 (wr_aveprice == "Open")
    src0 := open
else
    if (wr_aveprice == "High")
        src0 := high
    else
        if (wr_aveprice == "Low")
            src0 := low
        else
            if (wr_aveprice == "Close")
                src0 := close
            else
                if (wr_aveprice == "Median HL/2")
                    src0 := hl2
                else
                    if (wr_aveprice == "Typical HLC/3")
                        src0 := hlc3
                    else
                        if (wr_aveprice == "OHLC/4")
                            src0 := ohlc4
                        else
                            if (wr_aveprice == "Body Median OC/2")
                                src0 := (open + close)/2
                            else
                                if (wr_aveprice == "Weighted Close HL2C/4")
                                    src0 := (high + low + 2 * close) / 4
                                else
                                    if (wr_aveprice == "Biased HC/2 if Close > Open, else LC/2")
                                        src0 := close > open ? (high + close) / 2 : (low + close) / 2
                                    else
                                        if (wr_aveprice == "Biased High if Close > HL/2, else Low")
                                            src0 := close > hl2 ? high : low
                                        else
                                            if (wr_aveprice == "Biased High if Close > Open, else Low")
                                                src0 := close > open ? high : low

_wr0(length0) =>
	max0 = ta.highest(length0)
	min0 = ta.lowest(length0)
	100 * (src0 - max0) / (max0 - min0)
percentR0 = _wr0(length0) + 50

show_wr0_ma = input(false, "", inline="MA", group = "Display Williams %R and its Moving Average")
wr0_ma_length = input.int(50, "Length", inline = "MA", group="Display Williams %R and its Moving Average")
wr0_ma_type = input.string("HMA", "", inline = "MA", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA"], group = "Display Williams %R and its Moving Average")
wr0_ma = ma(percentR0, wr0_ma_length, wr0_ma_type)
p_wr0 = plot(show_wr0_ma ? wr0_ma : na, title="%R0 MA", color=color.rgb(211, 54, 130, 60), linewidth=1) // magenta 211, 54, 130 or brwhite 253, 246, 227

// remaining %R

length1 = wr_1_length
src1 = src0
_wr1(length1) =>
	max1 = ta.highest(length1)
	min1 = ta.lowest(length1)
	100 * (src1 - max1) / (max1 - min1)
percentR1 = _wr1(length1) + 50

length2 = wr_2_length
src2 = src0
_wr2(length2) =>
	max2 = ta.highest(length2)
	min2 = ta.lowest(length2)
	100 * (src2 - max2) / (max2 - min2)
percentR2 = _wr2(length2) + 50

length3 = wr_3_length
src3 = src0
_wr3(length3) =>
	max3 = ta.highest(length3)
	min3 = ta.lowest(length3)
	100 * (src3 - max3) / (max3 - min3)
percentR3 = _wr3(length3) + 50

length4 = wr_4_length
src4 = src0
_wr4(length4) =>
	max4 = ta.highest(length4)
	min4 = ta.lowest(length4)
	100 * (src4 - max4) / (max4 - min4)
percentR4 = _wr4(length4) + 50

length5 = wr_5_length
src5 = src0
_wr5(length5) =>
	max5 = ta.highest(length5)
	min5 = ta.lowest(length5)
	100 * (src5 - max5) / (max5 - min5)
percentR5 = _wr5(length5) + 50

length6 = wr_6_length
src6 = src0
_wr6(length6) =>
	max6 = ta.highest(length6)
	min6 = ta.lowest(length6)
	100 * (src6 - max6) / (max6 - min6)
percentR6 = _wr6(length6) + 50

length7 = wr_7_length
src7 = src0
_wr7(length7) =>
	max7 = ta.highest(length7)
	min7 = ta.lowest(length7)
	100 * (src7 - max7) / (max7 - min7)
percentR7 = _wr7(length7) + 50

length8 = wr_8_length
src8 = src0
_wr8(length8) =>
	max8 = ta.highest(length8)
	min8 = ta.lowest(length8)
	100 * (src8 - max8) / (max8 - min8)
percentR8 = _wr8(length8) + 50

show_wr_all = input(true, "%R 01-08", inline="", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr1 = input(true, "%R 01", inline="WR1", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr2 = input(true, "%R 02", inline="WR1", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr3 = input(true, "%R 03", inline="WR1", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr4 = input(true, "%R 04", inline="WR1", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr5 = input(true, "%R 05", inline="WR2", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr6 = input(true, "%R 06", inline="WR2", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr7 = input(true, "%R 07", inline="WR2", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")
show_wr8 = input(true, "%R 08", inline="WR2", group = "Display %R calculated with the number sequence selected above, where %R 01 is calculated with the faster period and %R 08 the slower.")

p0 = plot(show_wr0 ? percentR0 : na, title="%R0", color = show_wr_all ? color.rgb(253, 246, 227, 20) : color.rgb(253, 246, 227, 70), linewidth=1) // brwhite
p1 = plot(show_wr1 ? show_wr_all ? percentR1 : na : na, title="%R1", color=color.rgb(253, 246, 227, 0), linewidth=1) // brwhite
p2 = plot(show_wr2 ? show_wr_all ? percentR2 : na : na, title="%R2", color=color.rgb(42, 161, 152, 20), linewidth=1) // cyan
p3 = plot(show_wr3 ? show_wr_all ? percentR3 : na : na, title="%R3", color=color.rgb(38, 139, 210, 30), linewidth=1) // blue
p4 = plot(show_wr4 ? show_wr_all ? percentR4 : na : na, title="%R4", color=color.rgb(108, 113, 196, 40), linewidth=1) // brmagenta
p5 = plot(show_wr5 ? show_wr_all ? percentR5 : na : na, title="%R5", color=color.rgb(211, 54, 130, 50), linewidth=1) // magenta
p6 = plot(show_wr6 ? show_wr_all ? percentR6 : na : na, title="%R6", color=color.rgb(220, 50, 47, 60), linewidth=1) // red
p7 = plot(show_wr7 ? show_wr_all ? percentR7 : na : na, title="%R7", color=color.rgb(181, 137, 0, 70), linewidth=1) // yellow
p8 = plot(show_wr8 ? show_wr_all ? percentR8 : na : na, title="%R8", color=color.rgb(253, 246, 227, 70), linewidth=1) // cyan 42, 161, 152 or brwhite 253, 246, 227

fill(p0, p1, title="R0 R1 Fill", color=color.rgb(42, 161, 152, 50)) // cyan
fill(p0, p_wr0, title="R0 R0MA Fill", color = show_wr_all ? na : percentR0 > wr0_ma ? color.rgb(253, 246, 227, 90) : color.rgb(211, 54, 130, 90)) // cyan
fill(p1, p2, title="R1 R2 Fill", color=color.rgb(38, 139, 210, 50)) // blue
fill(p2, p3, title="R2 R3 Fill", color=color.rgb(108, 113, 196, 55)) // brmagenta
fill(p3, p4, title="R3 R4 Fill", color=color.rgb(211, 54, 130, 60)) // magenta
fill(p4, p5, title="R4 R5 Fill", color=color.rgb(220, 50, 47, 65)) // red
fill(p5, p6, title="R5 R6 Fill", color=color.rgb(181, 137, 0, 70)) // yellow
fill(p6, p7, title="R6 R7 Fill", color=color.rgb(42, 161, 152, 80)) // cyan
fill(p7, p8, title="R7 R8 Fill", color=color.rgb(38, 139, 210, 80)) // blue

// RSI

// define first/fastest RSI length, default length = 14

show_rsi0 = input(true, "", inline = "RSI", group = "Display RSI and its Moving Average")
rsi_length0 = input.int(14, minval = 1, title = "Length", inline = "RSI", group = "Display RSI and its Moving Average")

rsi_aveprice = input.string("Close", title = "", 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 = "RSI", group="Display RSI and its Moving Average")

rsi_source0 = 1.0

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

up0 = ta.rma(math.max(ta.change(rsi_source0), 0), rsi_length0)
down0 = ta.rma(-math.min(ta.change(rsi_source0), 0), rsi_length0)
rsi0 = down0 == 0 ? 100 : up0 == 0 ? 0 : ((100 - (100 / (1 + up0 / down0))) - 50)

show_rsi0_ma = input(false, "", inline="MA", group = "Display RSI and its Moving Average")
rsi0_ma_length = input.int(50, "Length", inline = "MA", group="Display RSI and its Moving Average")
rsi0_ma_type = input.string("HMA", "", inline = "MA", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA"], group = "Display RSI and its Moving Average")
rsi0_ma = ma(rsi0, rsi0_ma_length, rsi0_ma_type)
r0_ma = plot(show_rsi0_ma ? rsi0_ma : na, title="RSI0 MA", color=color.rgb(42, 161, 152, 60), linewidth=1) // brwhite 253, 246, 227

// remaining RSI

rsi_length1 = wr_1_length
rsi_source1 = rsi_source0
up1 = ta.rma(math.max(ta.change(rsi_source1), 0), rsi_length1)
down1 = ta.rma(-math.min(ta.change(rsi_source1), 0), rsi_length1)
rsi1 = down1 == 0 ? 100 : up1 == 0 ? 0 : ((100 - (100 / (1 + up1 / down1))) - 50)

rsi_length2 = wr_2_length
rsi_source2 = rsi_source0
up2 = ta.rma(math.max(ta.change(rsi_source2), 0), rsi_length2)
down2 = ta.rma(-math.min(ta.change(rsi_source2), 0), rsi_length2)
rsi2 = down2 == 0 ? 100 : up2 == 0 ? 0 : ((100 - (100 / (1 + up2 / down2))) - 50)

rsi_length3 = wr_3_length
rsi_source3 = rsi_source0
up3 = ta.rma(math.max(ta.change(rsi_source3), 0), rsi_length3)
down3 = ta.rma(-math.min(ta.change(rsi_source3), 0), rsi_length3)
rsi3 = down3 == 0 ? 100 : up3 == 0 ? 0 : ((100 - (100 / (1 + up3 / down3))) - 50)

rsi_length4 = wr_4_length
rsi_source4 = rsi_source0
up4 = ta.rma(math.max(ta.change(rsi_source4), 0), rsi_length4)
down4 = ta.rma(-math.min(ta.change(rsi_source4), 0), rsi_length4)
rsi4 = down4 == 0 ? 100 : up4 == 0 ? 0 : ((100 - (100 / (1 + up4 / down4))) - 50)

rsi_length5 = wr_5_length
rsi_source5 = rsi_source0
up5 = ta.rma(math.max(ta.change(rsi_source5), 0), rsi_length5)
down5 = ta.rma(-math.min(ta.change(rsi_source5), 0), rsi_length5)
rsi5 = down5 == 0 ? 100 : up5 == 0 ? 0 : ((100 - (100 / (1 + up5 / down5))) - 50)

rsi_length6 = wr_6_length
rsi_source6 = rsi_source0
up6 = ta.rma(math.max(ta.change(rsi_source6), 0), rsi_length6)
down6 = ta.rma(-math.min(ta.change(rsi_source6), 0), rsi_length6)
rsi6 = down6 == 0 ? 100 : up6 == 0 ? 0 : ((100 - (100 / (1 + up6 / down6))) - 50)

rsi_length7 = wr_7_length
rsi_source7 = rsi_source0
up7 = ta.rma(math.max(ta.change(rsi_source7), 0), rsi_length7)
down7 = ta.rma(-math.min(ta.change(rsi_source7), 0), rsi_length7)
rsi7 = down7 == 0 ? 100 : up7 == 0 ? 0 : ((100 - (100 / (1 + up7 / down7))) - 50)

rsi_length8 = wr_8_length
rsi_source8 = rsi_source0
up8 = ta.rma(math.max(ta.change(rsi_source8), 0), rsi_length8)
down8 = ta.rma(-math.min(ta.change(rsi_source8), 0), rsi_length8)
rsi8 = down8 == 0 ? 100 : up8 == 0 ? 0 : ((100 - (100 / (1 + up8 / down8))) - 50)

show_rsi_all = input(false, "RSI 01-08", inline="", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi1 = input(true, "RSI 01", inline="WR1", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi2 = input(true, "RSI 02", inline="WR1", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi3 = input(true, "RSI 03", inline="WR1", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi4 = input(true, "RSI 04", inline="WR1", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi5 = input(true, "RSI 05", inline="WR2", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi6 = input(true, "RSI 06", inline="WR2", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi7 = input(true, "RSI 07", inline="WR2", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")
show_rsi8 = input(true, "RSI 08", inline="WR2", group = "Display RSI calculated with the number sequence selected above, where RSI 01 is calculated with the faster period and RSI 08 the slower.")

r0 = plot(show_rsi0 ? rsi0 : na, title="RSI", color = show_rsi_all ? color.rgb(253, 246, 227, 20) : color.rgb(253, 246, 227, 70), linewidth=1) // brwhite
r1 = plot(show_rsi1 ? show_rsi_all ? rsi1 : na : na, title="RSI1", color=color.rgb(253, 246, 227, 0), linewidth=1) // brwhite
r2 = plot(show_rsi2 ? show_rsi_all ? rsi2 : na : na, title="RSI2", color=color.rgb(42, 161, 152, 20), linewidth=1) // cyan
r3 = plot(show_rsi3 ? show_rsi_all ? rsi3 : na : na, title="RSI3", color=color.rgb(38, 139, 210, 30), linewidth=1) // blue
r4 = plot(show_rsi4 ? show_rsi_all ? rsi4 : na : na, title="RSI4", color=color.rgb(108, 113, 196, 40), linewidth=1) // brmagenta
r5 = plot(show_rsi5 ? show_rsi_all ? rsi5 : na : na, title="RSI5", color=color.rgb(211, 54, 130, 50), linewidth=1) // magenta
r6 = plot(show_rsi6 ? show_rsi_all ? rsi6 : na : na, title="RSI6", color=color.rgb(220, 50, 47, 60), linewidth=1) // red
r7 = plot(show_rsi7 ? show_rsi_all ? rsi7 : na : na, title="RSI7", color=color.rgb(181, 137, 0, 70), linewidth=1) // yellow
r8 = plot(show_rsi8 ? show_rsi_all ? rsi8 : na : na, title="RSI8", color=color.rgb(253, 246, 227, 70), linewidth=1) // cyan 42, 161, 152 or brwhite 253, 246, 227

fill(r0, r1, title="RSI0 RSI1 Fill", color=color.rgb(42, 161, 152, 50)) // cyan
fill(r0, r0_ma, title="RSI0 RSI0MA Fill", color = show_rsi_all ? na : rsi0 > rsi0_ma ? color.rgb(253, 246, 227, 90) : color.rgb(42, 161, 152, 90)) // cyan
fill(r1, r2, title="RSI1 RSI2 Fill", color=color.rgb(38, 139, 210, 50)) // blue
fill(r2, r3, title="RSI2 RSI3 Fill", color=color.rgb(108, 113, 196, 55)) // brmagenta
fill(r3, r4, title="RSI3 RSI4 Fill", color=color.rgb(211, 54, 130, 60)) // magenta
fill(r4, r5, title="RSI4 RSI5 Fill", color=color.rgb(220, 50, 47, 65)) // red
fill(r5, r6, title="RSI5 RSI6 Fill", color=color.rgb(181, 137, 0, 70)) // yellow
fill(r6, r7, title="RSI6 RSI7 Fill", color=color.rgb(42, 161, 152, 80)) // cyan
fill(r7, r8, title="RSI7 RSI8 Fill", color=color.rgb(38, 139, 210, 80)) // blue


// RMI by Roger Altman

// define first/fastest RMI length, default length = 14, momentum = 3

show_rmi0 = input(false, "", inline = "RMI", group = "Display RMI and its Moving Average")
rmi_length0 = input.int(title="Length", inline = "RMI", group = "Display RMI and its Moving Average", defval=14, minval=1, maxval=2000)

rmi_momentum0 = input(3, title="", inline = "RMI", group = "Display RMI and its Moving Average") // if momentum = 1, RMI = RSI

rmi_aveprice = input.string("Close", title = "", 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 = "RMI", group="Display RMI and its Moving Average")

rmi_source0 = 1.0

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

rmi_up0 = ta.rma(math.max(ta.change(rmi_source0, rmi_momentum0), 0), rmi_length0)
rmi_down0 = ta.rma(-math.min(ta.change(rmi_source0, rmi_momentum0), 0), rmi_length0)
rmi0 = (rmi_down0 == 0 ? 100 : rmi_up0 == 0 ? 0 : (100 - (100 / (1 + rmi_up0 / rmi_down0))) - 50)
rmi0_p = plot(show_rmi0 ? rmi0 : na, title="RMI", color = color.rgb(253, 246, 227, 70), linewidth=1) // brwhite

show_rmi0_ma = input(false, "", inline="MA", group = "Display RMI and its Moving Average")
rmi0_ma_length = input.int(50, "Length", inline = "MA", group="Display RMI and its Moving Average")
rmi0_ma_type = input.string("HMA", "", inline = "MA", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA"], group = "Display RMI and its Moving Average")
rmi0_ma = ma(rmi0, rmi0_ma_length, rmi0_ma_type)
rmi0_ma_p = plot(show_rmi0_ma ? rmi0_ma : na, title="RMI0 MA", color=color.rgb(42, 161, 152, 60), linewidth=1) // brwhite 253, 246, 227
fill(rmi0_p, rmi0_ma_p, title="RMI0 RMI0MA Fill", color = rmi0 > rmi0_ma ? color.rgb(253, 246, 227, 90) : color.rgb(42, 161, 152, 90)) // cyan

// MFI

show_mfi0 = input(false, "", inline = "MFI", group = "Display MFI and its Moving Average")
mfi_length0 = input.int(title="Length", inline = "MFI", group = "Display MFI and its Moving Average", defval=14, minval=1, maxval=2000)

mfi_aveprice = input.string("Typical HLC/3", title = "", 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 = "MFI", group="Display MFI and its Moving Average")

mfi_source0 = 1.0

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

mfi0 = ta.mfi(mfi_source0, mfi_length0) - 50
mfi0_p = plot(show_mfi0 ? mfi0 : na, title="MFI", color = color.rgb(253, 246, 227, 70), linewidth=1) // brwhite


show_mfi0_ma = input(false, "", inline="MA", group = "Display MFI and its Moving Average")
mfi0_ma_length = input.int(50, "Length", inline = "MA", group="Display MFI and its Moving Average")
mfi0_ma_type = input.string("HMA", "", inline = "MA", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA"], group = "Display MFI and its Moving Average")
mfi0_ma = ma(mfi0, mfi0_ma_length, mfi0_ma_type)
mfi0_ma_p = plot(show_mfi0_ma ? mfi0_ma : na, title="MFI0 MA", color=color.rgb(42, 161, 152, 60), linewidth=1) // brwhite 253, 246, 227
fill(mfi0_p, mfi0_ma_p, title="MFI0 MFI0MA Fill", color = mfi0 > mfi0_ma ? color.rgb(253, 246, 227, 90) : color.rgb(42, 161, 152, 90)) // cyan


// RSX by Mark Jurik

show_rsx = input(false, "", inline = "RSX", group = "Display RSX and its Moving Average")
rsx_length = input.int(title="Length", inline = "RSX", group = "Display RSX and its Moving Average", defval=14, minval=1, maxval=2000)

rsx_aveprice = input.string("Close", title = "", 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 = "RSX", group="Display RSX and its Moving Average")

rsx_source = 1.0

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


f8 = 100 * rsx_source
f10 = nz(f8[1])
v8 = f8 - f10

f18 = 3 / (rsx_length + 2)
f20 = 1 - f18

f28 = 0.0
f28 := f20 * nz(f28[1]) + f18 * v8
f30 = 0.0
f30 := f18 * f28 + f20 * nz(f30[1])
vC = f28 * 1.5 - f30 * 0.5

f38 = 0.0
f38 := f20 * nz(f38[1]) + f18 * vC
f40 = 0.0
f40 := f18 * f38 + f20 * nz(f40[1])
v10 = f38 * 1.5 - f40 * 0.5

f48 = 0.0
f48 := f20 * nz(f48[1]) + f18 * v10
f50 = 0.0
f50 := f18 * f48 + f20 * nz(f50[1])
v14 = f48 * 1.5 - f50 * 0.5

f58 = 0.0
f58 := f20 * nz(f58[1]) + f18 * math.abs(v8)
f60 = 0.0
f60 := f18 * f58 + f20 * nz(f60[1])
v18 = f58 * 1.5 - f60 * 0.5

f68 = 0.0
f68 := f20 * nz(f68[1]) + f18 * v18
f70 = 0.0
f70 := f18 * f68 + f20 * nz(f70[1])
v1C = f68 * 1.5 - f70 * 0.5

f78 = 0.0
f78 := f20 * nz(f78[1]) + f18 * v1C
f80 = 0.0
f80 := f18 * f78 + f20 * nz(f80[1])
v20 = f78 * 1.5 - f80 * 0.5

f88_ = 0.0
f90_ = 0.0
f88 = 0.0

f90_ := (nz(f90_[1]) == 0) ? 1 : (nz(f88[1]) <= nz(f90_[1])) ? (nz(f88[1]) + 1) : (nz(f90_[1]) + 1)
f88 := ((nz(f90_[1]) == 0) and (rsx_length - 1 >= 5)) ? (rsx_length - 1) : 5
f0 = ((f88 >= f90_) and (f8 != f10)) ? 1 : 0
f90 = ((f88 == f90_) and (f0 == 0)) ? 0 : f90_

v4_ = ((f88 < f90) and (v20 > 0)) ? ((v14 / v20 + 1) * 50) : 50

rsx = ((v4_ > 100) ? 100 : (v4_ < 0) ? 0 : v4_) - 50

rsx_p = plot(show_rsx ? rsx : na, title="RSX", color = color.rgb(253, 246, 227, 70), linewidth=1) // brwhite

show_rsx_ma = input(false, "", inline="MA", group = "Display RSX and its Moving Average")
rsx_ma_length = input.int(50, "Length", inline = "MA", group="Display RSX and its Moving Average")
rsx_ma_type = input.string("HMA", "", inline = "MA", options = ["SMA", "EMA", "DEMA", "TEMA", "QEMA", "PEMA", "ZLEMA", "T3", "HMA", "SMMA", "WMA", "VWMA"], group = "Display RSX and its Moving Average")
rsx_ma = ma(rsx, rsx_ma_length, rsx_ma_type)
rsx_ma_p = plot(show_rsx_ma ? rsx_ma : na, title="RSX MA", color=color.rgb(42, 161, 152, 60), linewidth=1) // brwhite 253, 246, 227
fill(rsx_p, rsx_ma_p, title="RSX RSXMA Fill", color = rsx > rsx_ma ? color.rgb(253, 246, 227, 90) : color.rgb(42, 161, 152, 90)) // cyan


// Upper and Lower Lines 

obPlot = hline(53, title="Upper Band", linestyle=hline.style_solid, color=color.rgb(253, 246, 227, 95), linewidth=1)//brwhite
hline(50, title="100% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 80), linewidth=1)//brwhite
hline(30, title="80% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 50), linewidth=1)//brwhite
hline(20, title="70% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 40), linewidth=1)//brwhite
hline(0, title="50% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 0), linewidth=1)//brwhite
hline(-20, title="30% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 40), linewidth=1)//brwhite
hline(-30, title="20% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 50), linewidth=1)//brwhite
hline(-50, title="0% Level", linestyle=hline.style_dotted, color=color.rgb(253, 246, 227, 80), linewidth=1)//brwhite
osPlot = hline(-53, title="Lower Band", linestyle=hline.style_solid, color=color.rgb(253, 246, 227, 95), linewidth=1)//brwhite
fill(obPlot, osPlot, title="Background", color=color.rgb(253, 246, 227, 99))//brwhite


//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
//
//END