Golgistain

Automatic Trendlines (Proof of Concept)

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.
Remove from Favorite Scripts Add to Favorite Scripts
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????
Reply
snapshot


Nice work, anyway you could make it do trendlines like this, here are three examples. Top pin to a bottom pin or vice versa.
Reply
Maybe, better use style=linebr ? Nice lines))
Reply
oh, no. linebr is not good
Reply
it is very nice addition to tradingview scripts, thank you.
Reply
United States
United Kingdom
India
Deutschland
España
France
Việt Nam
Italia
Polska
Brasil
Россия
Türkiye
Indonesia
Malaysia
日本
한국
简体
繁體
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
Profile Profile Settings Account and Billing Support Priority Support Report Issue Ideas Published Followers Following Private Messages Chat Sign Out