// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BigBeluga
//@version=5
indicator(“Regression Indicator [BigBeluga]”, overlay = false, max_bars_back = 500)
// =====================================================================================================================}
// INPUTS
// ====================================================================================================================={
int length = input.int(400, “Regression Length”)
int length1 = input.int(100, “Regression Normalization”)
int length2 = input.int(50, “Signal Line”)
color m_color = #049ef7
// =====================================================================================================================}
// CALCULATIONS
// ====================================================================================================================={
// @function regression_indicator is a Normalized Ratio of Regression Lines with close
regression_indicator(src, length) =>
sum_x = 0.0
sum_y = 0.0
sum_xy = 0.0
sum_x_sq = 0.0
distance = 0.0
// Calculate Sum
for i = 0 to length – 1 by 1
sum_x += i + 1
sum_y += src[i]
sum_xy += (i + 1) * src[i]
sum_x_sq += math.pow(i + 1, 2)
// Calculate linear regression coefficients
slope = (length * sum_xy – sum_x * sum_y)
/ (length * sum_x_sq – math.pow(sum_x, 2))
intercept = (sum_y – slope * sum_x) / length
// Calculate Regression Indicator
y1 = intercept + slope
distance := (close – y1)
distance_n = ta.sma((distance – ta.sma(distance, length1))
/ ta.stdev(distance, length1), 10)
[distance_n, y1]
[y, y1] = regression_indicator(close, length)
// Signal Lines
mean = ta.sma(y, length2)
mean1 = ta.ema(y, 5)
// Conditions
cond1 = ta.crossunder(mean1, mean) and y > 0
cond2 = ta.crossover(mean1, mean) and y < 0
cond3 = ta.crossover(y, 0)
cond4 = ta.crossunder(y, 0)
// =====================================================================================================================}
// PLOT
// ====================================================================================================================={
// Candles Colors
color = y > 0 ? color.from_gradient(y, 0, 1, na, m_color) : color.from_gradient(y, -1, 0, chart.fg_color, na)
barcolor(color)
plotcandle(open, high, low, close, “Color Candles”,
color = color,
wickcolor = color,
bordercolor = color,
force_overlay = true
)
// Plot Regression Line
plot(series = y1,
title = “Regression Line”,
color = color,
linewidth = 2,
force_overlay = true
)
// Plot Regression Indicator
p1 = plot(series = y,
title = “Regression Indicator”,
color = color,
linewidth = 1
)
p0 = plot(series = 0,
title = “Zero Line”,
color = color,
linewidth = 1
)
// Fill Color Regression Indicator
fill(p1, p0, 2.5, 0, m_color, na, title = “Upper Fill”)
fill(p1, p0, 0, -2.5, na, chart.fg_color, title = “Lower Fill”)
// Signal Line
plot(mean, color = color.rgb(3, 100, 165, 52), linewidth = 2)
// Arrow Trend Direction
var txt = “”
switch
cond3 => txt := “➚”
cond4 => txt := “➘”
if barstate.islast
label.delete(
label.new(
x = bar_index+3,
y = y1,
text = txt,
style = label.style_label_center,
color = color(na),
textcolor = color,
size = size.huge,
force_overlay = true
)[1]
)
// Reversion Signals
if cond1
label.new(
x = bar_index,
y = high,
text = “◓”,
style = label.style_label_down,
color = color(na),
textcolor = chart.fg_color,
size = size.large,
force_overlay = true
)
if cond2
label.new(
x = bar_index,
y = low,
text = “◒”,
style = label.style_label_up,
color = color(na),
textcolor = m_color,
size = size.large,
force_overlay = true
)
// Trend Signals
plotchar(cond3 ? 0 : na, “TrendUp”, “⦿”,
location = location.absolute,
size = size.small,
color = #0364a5
)
plotchar(cond3 ? close : na, “TrendUp”, “⦿”,
location = location.absolute,
size = size.small,
color = #0364a5,
force_overlay = true
)
plotchar(cond4 ? 0 : na, “TrendDn”, “⦿”,
location = location.absolute,
size = size.small,
color = chart.fg_color
)
plotchar(cond4 ? close : na, “TrendDn”, “⦿”,
location = location.absolute,
size = size.small,
color = chart.fg_color,
force_overlay = true
)
// =====================================================================================================================}