# // This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License https://creativecommons.org/licenses/by-sa/4.0/
# //@version=4
# study("A Useful MA Weighting Function For Controlling Lag & Smoothness","BWMA",true)
# length = input(50)
# beta = input(3.,"-Lag",minval=1,maxval=10)
# alpha = input(3.,"+Lag",minval=1,maxval=10)
# src = input(close)
input length = 20;
input beta = 3.0;
input alpha = 3.0;
input src = CLOSE;
# //----
# var b = array.new_float(0)
# var css = array.new_color(na)
# if barstate.isfirst
# for i = 0 to length-1
# x = i/(length-1)
# w = pow(x,alpha-1)*pow(1-x,beta-1)
# array.push(b,w)
def b = fold i = 0 to (length - 1) with w = 0 do w + (power((i / (length - 1)), alpha - 1 ) * power(1 - (i / (length - 1)), beta - 1));
# den = array.sum(b)
# //----
# sum = 0.
# for i = 0 to length-1
# sum := sum + src[i]*array.get(b,i)
# def sum = fold j = 0 to (length - 1) with s = 0 do s + src[j] * b[j];
def sum = fold j = 0 to (length - 1) with s = 0 do s + src[j] * (power((j / (length - 1)), alpha - 1 ) * power(1 - (j / (length - 1)), beta - 1));
# filt = sum/den
def filt = sum / b;
# //----
# os = rsi(filt,length)/100
# plot(filt,"Plot",array.get(css,round(os*199)),2)
plot indicator = filt;
addLabel(yes, indicator);
def os = RSI(length = length, price = filt);
indicator.assignValueColor(
if os > 80 then color.dark_green
else if os > 60 then color.green
else if os > 40 then color.orange
else if os > 20 then color.red
else color.dark_red
);