BTC Golden Bottom with Adaptive Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MightyZinger

//@version=4
study("BTC Golden Bottom", shorttitle="MZ BTC Bottom", overlay=true)

src = security('INDEX:BTCUSD', timeframe.period, close)

length = input(title="Adaptive MA Length",  defval=365)
majLength = input(title="Major Length",  defval=100)
minLength = input(title="Minor Length",  defval=50)

//  Envelop Parameters
showEnv    = input(true, title="Show AMA Envelope", group="ENVELOPE DISTANCE ZONE")
atr_mult        = input(20, title="Distance (Envelope) Multiplier", step=.1, group="ENVELOPE DISTANCE ZONE")
env_atr    = input(40, title="Envelope ATR Length", group="ENVELOPE DISTANCE ZONE")
i_Symmetrical 	= input(true, "Apply Symmetrically Weighted Moving Average at Envelope", group="ENVELOPE DISTANCE ZONE")

minAlpha = 2 / (minLength + 1)
majAlpha = 2 / (majLength + 1)

hh = highest(length + 1)
ll = lowest(length + 1)

mult = hh - ll != 0 ? abs(2 * src - ll - hh) / (hh - ll) : 0

final = mult * (minAlpha - majAlpha) + majAlpha

ma = 0.0
ma := nz(ma[1]) + pow(final, 2) * (src - nz(ma[1]))


ama_plot = plot(ma, title="MZ BTC Golden Bottom Line", linewidth=4, color=color.green, transp=0)

// Enveloping AMA with ATR bands
env_MA = ma
dev = atr_mult * atr(env_atr)
// Bands
m1 = input(0.236, minval=0, step=0.01, title="Fib 1", inline = "fibs1")
m2 = input(0.382, minval=0, step=0.01, title="Fib 2", inline = "fibs1")
m3 = input(0.5, minval=0, step=0.01, title="Fib 3", inline = "fibs1")
m4 = input(0.618, minval=0, step=0.01, title="Fib 4", inline = "fibs1")
m5 = input(0.764, minval=0, step=0.01, title="Fib 5", inline = "fibs1")
m6 = input(1, minval=0, step=0.01, title="Fib 6", inline = "fibs1")


upper_1= i_Symmetrical ? swma(env_MA + (m1*dev)) : env_MA + (m1*dev)
upper_2= i_Symmetrical ? swma(env_MA + (m2*dev)) : env_MA + (m2*dev)
upper_3= i_Symmetrical ? swma(env_MA + (m3*dev)) : env_MA + (m3*dev)
upper_4= i_Symmetrical ? swma(env_MA + (m4*dev)) : env_MA + (m4*dev)
upper_5= i_Symmetrical ? swma(env_MA + (m5*dev)) : env_MA + (m5*dev)
upper_6= i_Symmetrical ? swma(env_MA + (m6*dev)) : env_MA + (m6*dev)



// Plotting AMA Envelope Distance Zone

p1 = plot(showEnv ? upper_1 : na, color=color.new(color.red, 50), linewidth=1, title="0.236")
p2 = plot(showEnv ? upper_2 : na, color=color.new(color.red, 50), linewidth=1, title="0.382")
p3 = plot(showEnv ? upper_3 : na, color=color.new(color.green, 50), linewidth=3, title="0.5")
p4 = plot(showEnv ? upper_4 : na, color=color.new(color.blue, 50), linewidth=1, title="0.618")
p5 = plot(showEnv ? upper_5 : na, color=color.new(color.blue, 50), linewidth=1, title="0.764")
p6 = plot(showEnv ? upper_6 : na, color=color.new(color.blue, 50), linewidth=2, title="1")



fill(p1, ama_plot, color=color.new(color.red, 90))
fill(p5, p6, color=color.new(color.blue, 80))

