# Automatic Trendlines (Proof of Concept)

2872 5
This is more of an experiment of what can be done in pinescript
Figured I'd put it out there for any comments
The blue lines are rough manual extrapolations of what could be relevant trendlines

Each line doesn't exist as it's own entity, it is a sum of four changing parts.
```study(title="Automatic Trendlines", shorttitle="Trendlines",overlay=true)
len = input(14, minval=1, title="Length")

//Defines a function for a pivot point
p(x) => fixnan(dev((x=='l' ? lowest(len) : highest(len)), len) ? na : (x=='l' ? lowest(len) : highest(len)))

//The offset pivot point is useful for later calculations (this one syncs with the c(x) function, plot p(x),c(x), and z(x) to see what I mean)
z(x) => p(x)[len-2]

//Defines a function for change in time axis
c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),len), len) ? na : highest((barssince(change(p(x)) != 0)),len)))+1

//Define a function for the variable time value x (in line=mx+b) that must work for future values (other values of "y"), would work better with a true series based on the value of "y" for the second part, could get it to work
d(x,y) => barssince(change(z(x)) !=0) + (y==1 ? 0 : y==2 ? c(x) : y==3 ? (c(x) + valuewhen(change(z(x))!=0,c(x),(1))) : y==4 ? (c(x) + valuewhen(change(z(x))!=0,c(x),(1))+ valuewhen(change(z(x))!=0,c(x),(2))) : na)

//Define a function for slope
s(x) => (z(x) - valuewhen(change(z(x))!=0,z(x),1))/c(x)

//Function that can access previous values of the slope for use in the line equation
s_(x,y) => valuewhen(change(s(x))!=0,s(x),(y-1))

//Final Function (line=mx+b)
line_(x,y) => s_(x,y)*d(x,y) + valuewhen(change(z(x))!=0,z(x),(y-1))

plot(line_('l',1), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('l',2), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('l',3), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('l',4), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('h',1), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('h',2), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('h',3), style=circles, color=black, linewidth=2, offset=-2*len+3)
plot(line_('h',4), style=circles, color=black, linewidth=2, offset=-2*len+3)```
Nicee man! Do you think you can make a regression channel????

Nice work, anyway you could make it do trendlines like this, here are three examples. Top pin to a bottom pin or vice versa.
Maybe, better use style=linebr ? Nice lines))
vitvlkv
oh, no. linebr is not good
EN English
EN English (UK)
EN English (IN)
DE Deutsch
FR Français
ES Español
IT Italiano
PL Polski
SV Svenska
TR Türkçe
RU Русский
PT Português
ID Bahasa Indonesia
MS Bahasa Melayu
TH ภาษาไทย
VI Tiếng Việt
JA 日本語
KO 한국어
ZH 简体中文
ZH 繁體中文
AR العربية
Home Stock Screener Forex Signal Finder Cryptocurrency Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Feature Request Blog & News FAQ Help & Wiki Twitter