Auto Trendlines
Auto Trendlines
indicator("Trendlines", overlay=true)
// LINEAR REGRESSION
// input
group1 = "LINEAR REGRESSION SETTING"
lengthInput = input.int(100, title="Length", minval = 1, maxval = 5000, group=group1)
sourceInput = input.source(close, title="Source", group=group1)
useUpperDevInput = true
upperMultInput = 3.0
useLowerDevInput = true
lowerMultInput = 3.0
useUpperDevInput2 = true
upperMultInput2 = 2.0
useLowerDevInput2 = true
lowerMultInput2 = 2.0
useUpperDevInput3 = true
upperMultInput3 = 1.0
useLowerDevInput3 = true
lowerMultInput3 = 1.0
// function
calcSlope(source, length) =>
max_bars_back(source, 5000)
if not barstate.islast or length <= 1
[float(na), float(na), float(na)]
else
sumX = 0.0
sumY = 0.0
sumXSqr = 0.0
sumXY = 0.0
for i = 0 to length - 1 by 1
val = source[i]
per = i + 1.0
sumX += per
sumY += val
sumXSqr += per * per
sumXY += val * per
slope = (length * sumXY - sumX * sumY) / (length * sumXSqr - sumX * sumX)
average = sumY / length
intercept = average - slope * sumX / length + slope
[slope, average, intercept]
//primary trendline
// trendline extension
a_Extension_Multiplier=
a_Extensions=="25"? 1 :
a_Extensions=="50"? 2 :
a_Extensions=="75"? 3 :
a_Extensions=="100"? 4 :
a_Extensions=="150"? 6 :
a_Extensions=="200"? 8 :
a_Extensions=="300"? 12 :
a_Extensions=="400"? 16 :
a_Extensions=="500"? 20 :
a_Extensions=="750"? 30 :
a_Extensions=="1000"? 40 :
a_Extensions=="Infinate"? 0 : na
// trendline function
a_f_trendline(a__input_function, a__delay, a__only_up, a__extend) =>
var int a_Ax = 1
var int a_Bx = 1
var float a_By = 0
var float a_slope = 0
a_Ay = fixnan(a__input_function)
if ta.change(a_Ay) != 0
a_Ax := time[a__delay]
a_By := a_Ay[1]
a_Bx := a_Ax[1]
a_slope := log_chart ? (math.log(a_Ay) - math.log(a_By)) / (a_Ax - a_Bx) : (a_Ay - a_By) / (a_Ax - a_Bx)
a_slope
else
a_Ax := a_Ax[1]
a_Bx := a_Bx[1]
a_By := a_By[1]
a_By
// draw trendline
var line a_trendline = na
var int a_Axbis = 0
var float a_Aybis = 0
var bool a__xtend = true
a_extension_time = a_Extension_Multiplier * a_bar_time * 25
a_Axbis := a_Ax + a_extension_time
a_Aybis := log_chart ? a_Ay * math.exp(a_extension_time * a_slope) : a_Ay + a_extension_time * a_slope
if a_Extension_Multiplier != 0
a__xtend := false
a__xtend
if ta.change(a_Ay) != 0
if not na(a_line_color)
a_trendline := line.new(a_Bx, a_By, a_Axbis, a_Aybis, xloc.bar_time, extend=a__xtend ? extend.right :
extend.none, color=a_Line_Color, style=a_Line_Type, width=a_width)
a_trendline
[a_Bx, a_By, a_Axbis, a_Aybis, a_slope]
// secondary trendline
// trendline extension
b_Extension_Multiplier=
b_Extensions=="25"? 1 :
b_Extensions=="50"? 2 :
b_Extensions=="75"? 3 :
b_Extensions=="100"? 4 :
b_Extensions=="150"? 6 :
b_Extensions=="200"? 8 :
b_Extensions=="300"? 12 :
b_Extensions=="400"? 16 :
b_Extensions=="500"? 20 :
b_Extensions=="750"? 30 :
b_Extensions=="1000"? 40 :
b_Extensions=="Infinate"? 0 : na
// trendline function
b_f_trendline(b__input_function, b__delay, b__only_up, b__extend) =>
var int b_Ax = 1
var int b_Bx = 1
var float b_By = 0
var float b_slope = 0
b_Ay = fixnan(b__input_function)
if ta.change(b_Ay) != 0
b_Ax := time[b__delay]
b_By := b_Ay[1]
b_Bx := b_Ax[1]
b_slope := log_chart ? (math.log(b_Ay) - math.log(b_By)) / (b_Ax - b_Bx) : (b_Ay - b_By) / (b_Ax -
b_Bx)
b_slope
else
b_Ax := b_Ax[1]
b_Bx := b_Bx[1]
b_By := b_By[1]
b_By
// draw trendlines
var line b_trendline = na
var int b_Axbis = 0
var float b_Aybis = 0
var bool b__xtend = true
b_extension_time = b_Extension_Multiplier * b_bar_time * 25
b_Axbis := b_Ax + b_extension_time
b_Aybis := log_chart ? b_Ay * math.exp(b_extension_time * b_slope) : b_Ay + b_extension_time * b_slope
if b_Extension_Multiplier != 0
b__xtend := false
b__xtend
if ta.change(b_Ay) != 0
if not na(b_line_color)
b_trendline := line.new(b_Bx, b_By, b_Axbis, b_Aybis, xloc.bar_time, extend=b__xtend ? extend.right :
extend.none, color=b_Line_Color, style=b_Line_Type, width=b_width)
b_trendline
[b_Bx, b_By, b_Axbis, b_Aybis, b_slope]
// plot
b_color_high = b_slope_high * time < 0 ? color.green : na
b_color_low = b_slope_low * time > 0 ? color.red : na