MACD - STOCH - RSI 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Clancy3gbh

//@version=4
study(title="MACD - STOCH - RSI", shorttitle = "M.S.R v1", overlay = false, resolution = "")

//  ▄▄▄▄▄
//  ▐       MACD - STOCHSTIC - RSI      all in one.
//  ▐                   Hope you enjoy.
//  ▐           All feedback is good feedback.
//  ▀▀▀▀▀

//══════════════════════════════════════════════════════════════════════════════

[macdline, signalline, macdhist] = macd(close, 12, 26, 9)

i_vol       = input(false, 'Volume Bars', group = 'MACD Settings', tooltip = 'Changes histogram color if volume is above average.')

i_rsiSwitch = input(false, 'Overlay RSI     | ----- |', group = 'RSI Settings', inline = '0')
i_switch    = input(false, 'Overlay Stoch', group = "Stochastic Settings", inline = '0')

c_green     = macdhist > macdhist [1] ? color.new(#03B108, 20) : color.new(#6DFF71, 20)
c_red       = macdhist < macdhist [1] ? color.new(#890000, 20) : color.new(#FF6D6D, 20)

c_macd      = macdhist > 0 ? c_green : c_red

avg         = sum(volume, 10)/10
def         = iff(volume > (avg * 2), 1, iff(volume > (avg * 1.5), 2, na))

c_defblue   = def == 1 ? color.new(#0068B8, 20) : def == 2 ? color.new(#5FB9FF, 20) : c_macd
c_defyellow = def == 1 ? color.new(#550093, 20) : def == 2 ? color.new(#D398FF, 20) : c_macd

c_postmacd  = macdhist > 0 ? c_defblue : c_defyellow

plot(i_rsiSwitch ? na : i_switch ? na : macdhist, "Histogram", style=plot.style_columns, color = i_vol ? c_postmacd : c_macd)

//══════════════════════════════════════════════════════════════════════════════

//  ▄▄▄▄▄
//  ▐       RSI overlay code. [3GBH] 
//  ▀▀▀▀▀

//══════════════════════════════════════════════════════════════════════════════

i_rsilvl    = input(true, 'RSI levels', group = 'RSI Settings', tooltip = 'Shows RSI overbought and oversold levels when RSI enabled.')
i_cRsi      = input(true, 'Basis Color', group = 'RSI Settings', inline = '0', tooltip = 'Middle line becomes GREEN / RED whether RSI is ABOVE / BELOW.')
i_ob        = input(20, 'OB Level', group = 'RSI Settings', inline = '1')
i_os        = input(-20, '|     OS Level', group = 'RSI Settings', inline = '1')

//══════════════════════════════════════════════════════════════════════════════

f_src       = rsi(close, 14)
f_rsi(src) =>
    var float counter = 0.0
    if src == 50
        counter := 0
    else if src >= 50.01
        counter := src - 50
    else if src <= 49.99
        counter := src - 50
    counter

rsi = f_rsi(f_src)

//══════════════════════════════════════════════════════════════════════════════

clr         = rising(rsi, 1) ? color.new(#56FF7C, 40) : color.new(#FF5656, 40)
clr2        = rising(rsi, 1) ? color.new(#56FF7C, 80) : color.new(#FF5656, 80)
clr3        = rsi > 0 ? color.new(#56FF7C, 80) : color.new(#FF5656, 80)

plot(i_rsiSwitch ? rsi : na, color=clr)
// use this line ^ of code. ----- To toggle on/off the overlay
// and change the levels in the Overbought and Oversold inputs.

plot(i_rsiSwitch ? 0 : na, 'Middle', color = i_cRsi ? clr3 : clr2)
plot(i_rsiSwitch and i_rsilvl ? i_ob : na, 'OB', color = color.new(#d6d6d6, 80))
plot(i_rsiSwitch and i_rsilvl  ? i_os : na, 'OS', color = color.new(#d6d6d6, 80))

//══════════════════════════════════════════════════════════════════════════════

//  ▄▄▄▄▄
//  ▐       STOCHASTIC overlay code. [3GBH] 
//  ▀▀▀▀▀

//══════════════════════════════════════════════════════════════════════════════

i_stlvl     = input(true, ' STOCH levels', group = 'Stochastic Settings', tooltip = 'Shows STOCHASTIC overbought and oversold levels when STOCHASTIC enabled.')
periodK     = input(14, "%K Length", group = "Stochastic Settings")
smoothK     = input(1, "%K Smoothing", group = "Stochastic Settings")
periodD     = input(3, "%D Smoothing", group = "Stochastic Settings")
k           = sma(stoch(close, high, low, periodK), smoothK)
d           = sma(k, periodD)

i_obST      = input(35, 'OB Level', group = "Stochastic Settings", inline = '1')
i_osST      = input(-35, '|     OS Level', group = "Stochastic Settings", inline = '1')

//══════════════════════════════════════════════════════════════════════════════

f_src1      = k
f_src2      = d

f_stoch(src) =>
    var float counter = 0.0
    if src == 50
        counter := 0
    else if src >= 50.01
        counter := src - 50
    else if src <= 49.99
        counter := src - 50
    counter

stoch_k     = f_stoch(f_src1)
stoch_d     = f_stoch(f_src2)

//══════════════════════════════════════════════════════════════════════════════

plot(i_switch ? stoch_k : na, "%K", color = color.new(#2962FF, 66))
plot(i_switch ? stoch_d : na, "%D", color = color.new(#FF6D00, 70))

plot(i_switch and i_stlvl ? i_obST : na, "Upper Band", color = color.new(#B3B3B3, 60))
plot(i_switch and i_stlvl ? i_osST : na, "Lower Band", color = color.new(#B3B3B3, 60))

//══════════════════════════════════════════════════════════════════════════════





