Code
Code
0
at https://mozilla.org/MPL/2.0/
// © ChartPrime
//@version=5
indicator('Linear Regression Channel', overlay = true, max_lines_count = 3)
//
-----------------------------------------------------------------------------------
// 𝙐𝙎𝙀𝙍 𝙄𝙉𝙋𝙐𝙏𝙎
----------------------------------}
//
-----------------------------------------------------------------------------------
----------------------------------{
bool bands = input.bool(true, "Plot Linear Regression Bands", group =
"Settings Bands")
//
-----------------------------------------------------------------------------------
// 𝙄𝙉𝘿𝙄𝘾𝘼𝙏𝙊𝙍 𝘾𝘼𝙇𝘾𝙐𝙇𝘼𝙏𝙄𝙊𝙉𝙎
----------------------------------}
//
-----------------------------------------------------------------------------------
----------------------------------{
//@function linear_regression
//@description Calculates linear regression coefficients for a given source and
window.
//@param src (series float) The data series on which linear regression is
calculated.
//@param window (int) The number of bars to use in the calculation.
//@returns the intercept slope, Deviation, end of the channel.
linear_regression(src, window) =>
sum_x = 0.0
sum_y = 0.0
sum_xy = 0.0
sum_x_sq = 0.0
// Calculate sums
for i = 0 to window - 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)
//
-----------------------------------------------------------------------------------
// 𝙑𝙄𝙎𝙐𝘼𝙇𝙄𝙕𝘼𝙏𝙄𝙊𝙉
----------------------------------}
//
-----------------------------------------------------------------------------------
----------------------------------{
if arr_dirc
l1 = label.new(chart.point.from_index(bar_index, hl2 > y2__ ? high : low),
text = hl2 > y2__ ? "⇗" : hl2 < y2__ ? "⇘" : "⇒",
textcolor = hl2 > y2__ ? col_up : hl2 < y2__ ? col_dn : gray,
color = color(na),
size = size.huge,
style = label.style_label_left
)
label.delete(l1[1])
//
-----------------------------------------------------------------------------------
----------------------------------}