Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study("Auto Trendline Component", overlay=true)
- //TIMELINE
- //November 13th 2019: Unlisted for 1st time in over a year
- //January 20th 2019: Utilized a sampling size of 100 to limit how much is displayed
- //January 19th 2019: Added Alerts for trendline crossovers/crossunders
- //January 5th 2019: Refined minors errors involving display
- //April 21 2018: Added Visual signals for trendline crossovers/crossunders
- //March 17 2018: Script set to private because of dozens of unwanted views and people might be using it without my permission
- //March 15 2018: Readded Timeframe component with improvements to its display over the chart
- //March 10 2018: Removed Timeframe component as it is currently too messy
- //March 5 2018: Refinements allow relevant price points to be identified for connecting
- //March 4 2018: Incorporated custom inputs for adjusting purposes
- //February 28 2018: Logarithmic component utilized
- //February 24 2018: Version 6 of my trendline function finally completed and archived
- use_current_res=input(true,title="Employ Current Resolution")
- resCustom=input("D",title="Integrate Custom Resolution",type=resolution)
- base_len=input(26,"Lookback Length",minval=0)
- mult=input(1,"Extend Trendlines Forward (Multiplier Component)",minval=0)
- col=input(true,"Color Lines")
- disp_select=input(true,"Display Rising & Falling Lines")
- wicks=input(false,title="Draw Wicks")
- log_graph=input(true,"Logarithmic")
- //Faster Method for Timeframe Conversion
- base_res=
- period=="1"?1:
- period=="3"?3:
- period=="5"?5:
- period=="15"?15:
- period=="30"?30:
- period=="45"?45:
- period=="60"?60:
- period=="120"?120:
- period=="180"?180:
- period=="240"?240:
- period=="D"?1440:
- period=="W"?10080:
- period=="M"?43200:na
- custom_res=
- resCustom=="1"?1:
- resCustom=="3"?3:
- resCustom=="5"?5:
- resCustom=="15"?15:
- resCustom=="30"?30:
- resCustom=="45"?45:
- resCustom=="60"?60:
- resCustom=="120"?120:
- resCustom=="180"?180:
- resCustom=="D"?1440:
- resCustom=="W"?10080:na
- res_factor=use_current_res?1:base_res>custom_res?1:round(custom_res/base_res)
- len= use_current_res? base_len : base_len*res_factor
- //p(x) => fixnan(dev((x=='l' ? lowest(len) : highest(len)), len) ? na : (x=='l' ? lowest(len) : highest(len)))
- //z(x) => p(x)[len-2]
- //c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),len), len) ? na : highest((barssince(change(p(x)) != 0)),len)))+1
- //Multi-timeframe allocations
- high_MTF=use_current_res?high:security(tickerid,resCustom,high)
- open_MTF=use_current_res?open:security(tickerid,resCustom,open)
- close_MTF=use_current_res?close:security(tickerid,resCustom,close)
- low_MTF=use_current_res?low:security(tickerid,resCustom,low)
- // Change in Time Axis (Approximation)
- //c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),base_len), base_len) ? na : highest((barssince(change(p(x)) != 0)),base_len)))+1
- // Alternative (AXIS)
- //draw_line(x,y) => s_(x,y)*d(x,y) + valuewhen(change(z(x))!=0,z(x),(y-1))
- // trendline function (SLOPE)
- //trendline(x,y) => s_(x,y)*d(x,y) + valuewhen(change(z(x))!=0,z(x),(y-1))
- trendline(input_function, line_up_only, delay) =>
- Ay = fixnan(input_function)
- By = na
- By := change(Ay) !=0 ? Ay[1] : By[1]
- Ax = 0
- Bx = na
- Bx := change(Ay) != 0 ? barssince(change(Ay[1]) != 0) : Bx[1]
- Bxbis = barssince(change(Ay) != 0)
- //Point Slope
- //s(x) => (z(x) - valuewhen(change(z(x))!=0,z(x),1))/c(x)
- slope = na
- slope := change(Ay) != 0 ? (log_graph ? ((log(Ay) - log(By)) / (Bx)) : ((Ay - By) / (Bx))) : slope[1]
- //Plot range
- //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)
- ext_function_till_startpoint = log_graph ? (Ay * exp(slope * (Bxbis))) : Ay + slope * Bxbis
- ext_function_RT = log_graph ? (Ay * exp(slope * (Bxbis + delay))) : Ay + slope * (Bxbis + delay)
- ext_function_future = log_graph ? (Ay * exp(slope * (Bxbis + delay * (1 + mult)))) : Ay + slope * (Bxbis + delay * (1 + mult))
- col_line = line_up_only ? (slope >= 0 ? green : na) : (slope <= 0 ? red : na)
- [ext_function_till_startpoint, ext_function_RT, ext_function_future, col_line]
- high_point=pivothigh(wicks?high_MTF:(close_MTF>open_MTF?close_MTF:open_MTF),len,len)
- low_point=pivotlow(wicks?low_MTF:(close_MTF>open_MTF?open_MTF:close_MTF),len,len)
- //Function that can access previous values of the slope for use for a line equation
- //previous_slope(x,y) => valuewhen(change(s(x))!=0,s(x),(y-1))
- // calculate high/low lines and extentions
- [ext_high_point, ext_high_point_RT, ext_high_point_future, color_high] = trendline(high_point, false, len)
- [ext_low_point, ext_low_point_RT, ext_low_point_future, color_low] = trendline(low_point, true, len)
- col_h = disp_select ? (col ? color_high : color_high == red ? black : na) : (col ? red : black)
- col_l = disp_select ? (col ? color_low : color_low == green ? black : na) : (col ? green : black)
- plot(high_point, title="Descending", color=col_h, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
- plot(ext_high_point, title="Descending", style=circles, color=col_h, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
- plot(ext_high_point_RT, title="Descending", style=circles, color=col_h,linewidth=2,transp=0, show_last=100)
- plot(ext_high_point_future, title="Descending", style=circles, color=col_h, offset=(len * mult),linewidth=2,transp=0, show_last=100)
- plot(low_point,title="Ascending", color=col_l, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
- plot(ext_low_point_RT[len] != ext_low_point ? ext_low_point : na, title="Ascending", style=circles, color=col_l, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
- plot(ext_low_point_future[len] != ext_low_point_RT ? ext_low_point_RT : na, title="Ascending", style=circles, color=col_l,linewidth=2,transp=0, show_last=100)
- plot(ext_low_point_future, title="Ascending", style=circles, color=col_l, offset=(len * mult),linewidth=2,transp=0, show_last=100)
- crossoverUpper = not na(col_h) and crossover(close, ext_high_point_RT)
- alertcondition(crossoverUpper, title="Alert on Upper Trendline Breakout", message="Upper Trendline Breakout!")
- //plotshape(crossoverUpper, style=shape.triangleup, location=location.abovebar, size=size.tiny, color=green)
- crossunderLower = not na(col_l) and crossunder(close, ext_low_point_RT)
- alertcondition(crossunderLower, title="Alert on Lower Trendline Breakout", message="Lower Trendline Breakout!")
- //plotshape(crossunderLower, style=shape.triangledown, location=location.belowbar, size=size.tiny, color=red)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement