Real Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mablue (Masoud Azizi)
// Real indicator is an indicator to convert ohlcv charts to one oscilator!
// there is an effective thing in all charts: "Volume", and I used this thing to mix it by price
// Buy: on crossing above to bollinger-lower-band (on UpTrends)
// Sell: on crossing under bollinger-upper-band (on DownTrends)
// use an sma 200 to determine trends ;)
//@version=5
indicator(title="Real Indicator", shorttitle="REAL", precision=3, timeframe="", timeframe_gaps=true)
timeperiod = input.int(20, minval=3)
rv = input.float(0.25, "Real Variance", options=[0.05,0.10,0.25,0.5,0.75,0.90])
bbv = input.float(2, "Bollinger Bands Variance",minval=0.1,step=0.1)

Fast_BB_Color = input.color(color.red)
BB_Color = input.color(color.green)
Slow_BB_Color = input.color(color.blue)
Real_Color = input.color(color.gray)

real(time_period) =>
    // ta.hma((close/close[1]-1)*(volume[1]/volume[2]),time_period)
    // ta.hma(close >= close[1] ? close*volume : -close*volume,time_period)
    ta.hma((close/close[1]-1)*(volume/volume[1]),time_period)

r1 = real(math.round(timeperiod*(1-rv)))
r2 = real(timeperiod)
r3 = real(math.round(timeperiod*(1+rv)))
r = (r1+r2+r3)/3

[middle1, upper1, lower1] = ta.bb(r1, timeperiod, bbv)
[middle2, upper2, lower2] = ta.bb(r2, timeperiod, bbv)
[middle3, upper3, lower3] = ta.bb(r3, timeperiod, bbv)


pr = plot(r, "Real", color=Real_Color)
pr1 = plot(r1, "Real 1", color=Fast_BB_Color,display=display.none)
pr2 = plot(r2, "Real 2", color=BB_Color,display=display.none)
pr3 = plot(r3, "Real 3", color=Slow_BB_Color,display=display.none)

// hline(0)

pm1 = plot(middle1,"Middle 1", color=Fast_BB_Color,display=display.none)
pm2 = plot(middle2,"Middle 2", color=BB_Color,display=display.none)
pm3 = plot(middle3,"Middle 3", color=Slow_BB_Color,display=display.none)

pu1 = plot(upper1,"Upper 1", color=Fast_BB_Color,display=display.none)
pu2 = plot(upper2,"Upper 2", color=BB_Color,display=display.none)
pu3 = plot(upper3,"Upper 3", color=Slow_BB_Color,display=display.none)

pl1 = plot(lower1,"Lower 1", color=Fast_BB_Color,display=display.none)
pl2 = plot(lower2,"Lower 2", color=BB_Color,display=display.none)
pl3 = plot(lower3,"Lower 3", color=Slow_BB_Color,display=display.none)


fill(pu1, pm1, title = "BB 1 BG U", color=color.new(Fast_BB_Color, 85))
fill(pl1, pm1, title = "BB 1 BG D", color=color.new(Fast_BB_Color, 80))

fill(pu2, pm2, title = "BB 2 BG U", color=color.new(BB_Color, 85))
fill(pl2, pm2, title = "BB 2 BG D", color=color.new(BB_Color, 80))

fill(pu3, pm3, title = "BB 3 BG U", color=color.new(Slow_BB_Color, 85))
fill(pl3, pm3, title = "BB 3 BG D", color=color.new(Slow_BB_Color, 80))

fill(pr1, pu1, color = r1 > upper1 ? color.new(Fast_BB_Color, 70) : color.new(Fast_BB_Color, 100),display=display.none)
fill(pr1, pl1, color = r1 < lower1 ? color.new(Fast_BB_Color, 70) : color.new(Fast_BB_Color, 100),display=display.none)

fill(pr2, pu2, color = r2 > upper2 ? color.new(BB_Color, 70) : color.new(BB_Color, 100),display=display.none)
fill(pr2, pl2, color = r2 < lower2 ? color.new(BB_Color, 70) : color.new(BB_Color, 100),display=display.none)

fill(pr3, pu3, color = r3 > upper3 ? color.new(Slow_BB_Color, 70) : color.new(Slow_BB_Color, 100),display=display.none)
fill(pr3, pl3, color = r3 < lower3 ? color.new(Slow_BB_Color, 70) : color.new(Slow_BB_Color, 100),display=display.none)

fill(pr, pu3, color = r > upper1 ? color.new(color.red, 66) : color.new(color.red, 100),display=display.all)
fill(pr, pl3, color = r < lower1 ? color.new(color.green, 66) : color.new(color.green, 100),display=display.all)
fill(pr, pu3, color = r > upper2 ? color.new(color.red, 66) : color.new(color.red, 100),display=display.all)
fill(pr, pl3, color = r < lower2 ? color.new(color.green, 66) : color.new(color.green, 100),display=display.all)
fill(pr, pu3, color = r > upper3 ? color.new(color.red, 66) : color.new(color.red, 100),display=display.all)
fill(pr, pl3, color = r < lower3 ? color.new(color.green, 66) : color.new(color.green, 100),display=display.all)
