Bar Dependent Moving Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Mahan_Mahmoudi

//@version=5
indicator("BDMA 2.0",max_bars_back = 5000,format=format.price,overlay=true)

// Security BDMA Counter Locks
yearly = request.security(syminfo.tickerid,"1M",close)
monthly = request.security(syminfo.tickerid,"1W",close)
daily = request.security(syminfo.tickerid,"1D",close)
hourly = request.security(syminfo.tickerid,"60",close)
custom = input.int(200,title=" Custom BDMA Period",minval=2)

var int yearlycounter = 0
var int monthlycounter = 0
var int dailycounter = 0
var int hourlycounter = 0
var int fibcounter = 4182 
var int fib4182 = 0
var bool yearlylock = false
var bool monthlylock= false

if yearly[1]<yearly or yearly[1]>yearly and monthlylock == false
    yearlycounter := yearlycounter + 1 

if yearlycounter > 4999
    monthlylock := true
    yearlycounter := 5000

if monthly[1]<monthly or monthly[1]>monthly and monthlylock == true
    monthlycounter := monthlycounter + 1 

if monthlycounter > 4999
    monthlycounter := 4999

if yearlycounter < 4182 and monthlylock == false
    yearlycounter := 4182
if monthlycounter < 4182 and monthlylock == true
    monthlycounter := 4182


if yearlycounter < fibcounter 
    fib4182 := fibcounter

fibmov1 = monthlylock ? ta.sma(yearly,fibcounter/1) :ta.sma(monthly,fibcounter/1)
fibmov2 = monthlylock ? ta.sma(yearly,fibcounter/2) :ta.sma(monthly,fibcounter/2)
fibmov3 = monthlylock ? ta.sma(yearly,fibcounter/3) :ta.sma(monthly,fibcounter/3)
fibmov5 = monthlylock ? ta.sma(yearly,fibcounter/5) :ta.sma(monthly,fibcounter/5)
fibmov8 = monthlylock ? ta.sma(yearly,fibcounter/8) :ta.sma(monthly,fibcounter/8)
fibmov13 = monthlylock ? ta.sma(yearly,fibcounter/13) :ta.sma(monthly,fibcounter/13)
fibmov21 = monthlylock ? ta.sma(yearly,fibcounter/21) :ta.sma(monthly,fibcounter/21)
fibmov34 = monthlylock ? ta.sma(yearly,fibcounter/34) :ta.sma(monthly,fibcounter/34) 
fibmov55 = monthlylock ? ta.sma(yearly,fibcounter/55) :ta.sma(monthly,fibcounter/55)
fibmov89 = monthlylock ? ta.sma(yearly,fibcounter/89) :ta.sma(monthly,fibcounter/89)
fibmov144 = monthlylock ? ta.sma(yearly,fibcounter/144) :ta.sma(monthly,fibcounter/144)
fibmov233 = monthlylock ? ta.sma(yearly,fibcounter/233) :ta.sma(monthly,fibcounter/233)
fibmov377 = monthlylock ? ta.sma(yearly,fibcounter/377) :ta.sma(monthly,fibcounter/377)
fibmov610 = monthlylock ? ta.sma(yearly,fibcounter/610) :ta.sma(monthly,fibcounter/610)
fibmov987 = monthlylock ? ta.sma(yearly,fibcounter/987) :ta.sma(monthly,fibcounter/987)
fibmov1597 = monthlylock ? ta.sma(yearly,fibcounter/1597) :ta.sma(monthly,fibcounter/1597)
fibmov2584 = monthlylock ? ta.sma(yearly,fibcounter/2584) :ta.sma(monthly,fibcounter/2584)
fibmov4181 = monthlylock ? ta.sma(yearly,fibcounter/4181) :ta.sma(monthly,fibcounter/4181)


fma = (fibmov1 + fibmov2 + fibmov3 + fibmov5 + fibmov8 + fibmov13 + fibmov21 + fibmov34 + fibmov55 + fibmov89 + fibmov144 + fibmov233 + fibmov377 + fibmov610 + fibmov987 + fibmov1597 + fibmov2584 + fibmov4181) / 18


plot(fibmov2,title="Relative Fibonacci 4182 2",color=color.yellow, linewidth= 3, style = plot.style_line) 
plot(fibmov3,title="Relative Fibonacci 4182 3",color=color.green , linewidth= 2, style = plot.style_line) 
plot(fibmov5,title="Relative Fibonacci 4182 5",color=color.black , linewidth= 2 , style = plot.style_line) 
plot(fibmov8,title="Relative Fibonacci 4182 8",color=color.red , linewidth= 2 , style = plot.style_line) 
plot(fibmov13,title="Relative Fibonacci 4182 13",color=color.navy , linewidth= 2 , style = plot.style_line) 
plot(fibmov21,title="Relative Fibonacci 4182 21",color=color.gray , linewidth= 1 , style = plot.style_line) 
plot(fibmov34,title="Relative Fibonacci 4182 34",color=color.fuchsia , linewidth= 1 , style = plot.style_line) 
plot(fibmov55,title="Relative Fibonacci 4182 55",color=color.maroon , linewidth= 1 , style = plot.style_line) 
plot(fibmov89,title="Relative Fibonacci 4182 89",color=color.lime , linewidth= 1 , style = plot.style_line) 
plot(fibmov144,title="Relative Fibonacci 4182 144",color=color.aqua , linewidth= 1 , style = plot.style_line) 
plot(fibmov233,title="Relative Fibonacci 4182 233",color=color.blue , linewidth= 1 , style = plot.style_line)
plot(fibmov377,title="Relative Fibonacci 4182 377",color=color.silver , linewidth= 1 , style = plot.style_line) 
plot(fma,title="Fibonacci Moving Average",color=color.white,linewidth=4)

plot(monthlylock ? ta.sma(yearly,monthlycounter/custom) :ta.sma(monthly,yearlycounter/custom),title=" Custom BDMA Average",color=color.rgb(0, 255, 251))
