Aarika Balance of Power Trend (ABOPC)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © hlsolanki

//@version=5
//
indicator('Aarika Balance of Power Trend', shorttitle='ABOPT', overlay=false)
//
reg_trend_on = input(true, 'Activate Reg Trend Line')
length = input.int(defval=21, title='Length Reg Trend line=', minval=1)
//
BullTrend_hist = 0.0
BearTrend_hist = 0.0

BullTrend = (close - ta.lowest(low, 50)) / ta.atr(5)
BearTrend = (ta.highest(high, 50) - close) / ta.atr(5)
BearTrend2 = -1 * BearTrend

Trend = BullTrend - BearTrend

hline(0, title='Nivel 0', color=color.new (color.black, 0), linestyle=hline.style_solid, linewidth=1)
niv_0 = plot(0, title='Nivel 0"', color=color.new(color.black, 50), linewidth=1, style=plot.style_line)
hline(2, title='Nivel 2', color=color.new (color.red, 0), linestyle=hline.style_solid, linewidth=2)
niv_2 = plot(2, title='Nivel 2', color=color.new(color.green, 50), linewidth=1, style=plot.style_line)
hline(-2, title='Nivel -2', color=color.new (color.green, 0), linestyle=hline.style_solid, linewidth=2)
niv_22 = plot(-2, title='Nivel -2"', color=color.new(color.red, 50), linewidth=1, style=plot.style_line)

a = plot(BullTrend, title='Bull Trend', color=color.new(color.green, 0), linewidth=1, style=plot.style_line)
b = plot(BearTrend2, title='Bear Trend', color=color.new(color.red, 0), linewidth=1, style=plot.style_line)

fill(a, niv_2, color=color.new(color.green, 50))
fill(b, niv_22, color=color.new(color.red, 50))
fill(niv_22, niv_2, color=color.new(color.yellow, 90))

plot(reg_trend_on == false ? Trend : na, title='Trend', color=color.new(color.black, 0), linewidth=1, style=plot.style_line)

if BearTrend2 > -2
    BearTrend_hist := BearTrend2 + 2
    BearTrend_hist
plot(BearTrend_hist, title='Bear Trend Hist', color=color.new(color.green, 0), linewidth=1, style=plot.style_columns)

if BullTrend < 2
    BullTrend_hist := BullTrend - 2
    BullTrend_hist
plot(BullTrend_hist, title='Bull Trend Hist', color=color.new(color.red, 0), linewidth=1, style=plot.style_columns)

x = bar_index
y = Trend
x_ = ta.sma(x, length)
y_ = ta.sma(y, length)
mx = ta.stdev(x, length)
my = ta.stdev(y, length)
c = ta.correlation(x, y, length)
slope = c * (my / mx)
inter = y_ - slope * x_
reg_trend = x * slope + inter
//
plot(reg_trend_on == true ? reg_trend : na, title='Trend', color=color.new(color.black, 100), linewidth=2, style=plot.style_line)

Boolean_1 = input.bool(true, title='--- BOP ---')
THL = high != low ? high - low : 0.01

BullOpen = (high - open) / THL
BearOpen = (open - low) / THL

BullClose = (close - low) / THL
BearClose = (high - close) / THL

BullOC = close > open ? (close - open) / THL : 0
BearOC = open > close ? (open - close) / THL : 0

BullReward = (BullOpen + BullClose + BullOC) / 3
BearReward = (BearOpen + BearClose + BearOC) / 3

BOP = BullReward - BearReward

ma(Type, src, len) =>
    A = ta.ema(src, len)
    B = ta.sma(src, len)
    Type == 'EMA' ? A : Type == 'SMA' ? B : na

EMA = input(50, 'Ema Length')
SmoothBOP = ta.ema(BOP, EMA)

TEMA = input(50, 'Tema Length')
xPrice = SmoothBOP
xEMA1 = ta.ema(SmoothBOP, TEMA)
xEMA2 = ta.ema(xEMA1, TEMA)
xEMA3 = ta.ema(xEMA2, TEMA)
nRes = 3 * xEMA1 - 3 * xEMA2 + xEMA3
SmootherBOP = nRes

plot(SmoothBOP * 100, 'BOP ', color=color.new(color.maroon, 0), linewidth=2)
plot(SmootherBOP * 100, 'SmootherBOP', color=color.new(color.navy, 0), linewidth=2)
//bgcolor(SmoothBOP > SmootherBOP ? color.green : color.red)
