Advertisement
AnthonyAlexander

Auto Trendline Component [VERSION 6]

Feb 24th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. //@version=3
  2. study("Auto Trendline Component", overlay=true)
  3.  
  4. //TIMELINE
  5.  
  6. //November 13th 2019: Unlisted for 1st time in over a year
  7. //January 20th 2019: Utilized a sampling size of 100 to limit how much is displayed
  8. //January 19th 2019: Added Alerts for trendline crossovers/crossunders
  9. //January 5th 2019: Refined minors errors involving display
  10. //April 21 2018: Added Visual signals for trendline crossovers/crossunders
  11. //March 17 2018: Script set to private because of dozens of unwanted views and people might be using it without my permission
  12. //March 15 2018: Readded Timeframe component with improvements to its display over the chart
  13. //March 10 2018: Removed Timeframe component as it is currently too messy
  14. //March 5 2018: Refinements allow relevant price points to be identified for connecting
  15. //March 4 2018: Incorporated custom inputs for adjusting purposes
  16. //February 28 2018: Logarithmic component utilized
  17. //February 24 2018: Version 6 of my trendline function finally completed and archived
  18.  
  19. use_current_res=input(true,title="Employ Current Resolution")
  20. resCustom=input("D",title="Integrate Custom Resolution",type=resolution)
  21. base_len=input(26,"Lookback Length",minval=0)
  22. mult=input(1,"Extend Trendlines Forward (Multiplier Component)",minval=0)
  23. col=input(true,"Color Lines")
  24. disp_select=input(true,"Display Rising & Falling Lines")
  25. wicks=input(false,title="Draw Wicks")
  26. log_graph=input(true,"Logarithmic")
  27.  
  28. //Faster Method for Timeframe Conversion
  29. base_res=
  30. period=="1"?1:
  31. period=="3"?3:
  32. period=="5"?5:
  33. period=="15"?15:
  34. period=="30"?30:
  35. period=="45"?45:
  36. period=="60"?60:
  37. period=="120"?120:
  38. period=="180"?180:
  39. period=="240"?240:
  40. period=="D"?1440:
  41. period=="W"?10080:
  42. period=="M"?43200:na
  43. custom_res=
  44. resCustom=="1"?1:
  45. resCustom=="3"?3:
  46. resCustom=="5"?5:
  47. resCustom=="15"?15:
  48. resCustom=="30"?30:
  49. resCustom=="45"?45:
  50. resCustom=="60"?60:
  51. resCustom=="120"?120:
  52. resCustom=="180"?180:
  53. resCustom=="D"?1440:
  54. resCustom=="W"?10080:na
  55.  
  56. res_factor=use_current_res?1:base_res>custom_res?1:round(custom_res/base_res)
  57. len= use_current_res? base_len : base_len*res_factor
  58. //p(x) => fixnan(dev((x=='l' ? lowest(len) : highest(len)), len) ? na : (x=='l' ? lowest(len) : highest(len)))
  59. //z(x) => p(x)[len-2]
  60. //c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),len), len) ? na : highest((barssince(change(p(x)) != 0)),len)))+1
  61.  
  62.  
  63. //Multi-timeframe allocations
  64. high_MTF=use_current_res?high:security(tickerid,resCustom,high)
  65. open_MTF=use_current_res?open:security(tickerid,resCustom,open)
  66. close_MTF=use_current_res?close:security(tickerid,resCustom,close)
  67. low_MTF=use_current_res?low:security(tickerid,resCustom,low)
  68.  
  69. // Change in Time Axis (Approximation)
  70. //c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),base_len), base_len) ? na : highest((barssince(change(p(x)) != 0)),base_len)))+1
  71.  
  72. // Alternative (AXIS)
  73. //draw_line(x,y) => s_(x,y)*d(x,y) + valuewhen(change(z(x))!=0,z(x),(y-1))
  74.  
  75. // trendline function (SLOPE)
  76.  
  77. //trendline(x,y) => s_(x,y)*d(x,y) + valuewhen(change(z(x))!=0,z(x),(y-1))
  78. trendline(input_function, line_up_only, delay) =>
  79. Ay = fixnan(input_function)
  80. By = na
  81. By := change(Ay) !=0 ? Ay[1] : By[1]
  82. Ax = 0
  83. Bx = na
  84. Bx := change(Ay) != 0 ? barssince(change(Ay[1]) != 0) : Bx[1]
  85. Bxbis = barssince(change(Ay) != 0)
  86. //Point Slope
  87. //s(x) => (z(x) - valuewhen(change(z(x))!=0,z(x),1))/c(x)
  88. slope = na
  89. slope := change(Ay) != 0 ? (log_graph ? ((log(Ay) - log(By)) / (Bx)) : ((Ay - By) / (Bx))) : slope[1]
  90. //Plot range
  91. //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)
  92.  
  93. ext_function_till_startpoint = log_graph ? (Ay * exp(slope * (Bxbis))) : Ay + slope * Bxbis
  94. ext_function_RT = log_graph ? (Ay * exp(slope * (Bxbis + delay))) : Ay + slope * (Bxbis + delay)
  95. ext_function_future = log_graph ? (Ay * exp(slope * (Bxbis + delay * (1 + mult)))) : Ay + slope * (Bxbis + delay * (1 + mult))
  96. col_line = line_up_only ? (slope >= 0 ? green : na) : (slope <= 0 ? red : na)
  97. [ext_function_till_startpoint, ext_function_RT, ext_function_future, col_line]
  98.  
  99. high_point=pivothigh(wicks?high_MTF:(close_MTF>open_MTF?close_MTF:open_MTF),len,len)
  100. low_point=pivotlow(wicks?low_MTF:(close_MTF>open_MTF?open_MTF:close_MTF),len,len)
  101.  
  102. //Function that can access previous values of the slope for use for a line equation
  103. //previous_slope(x,y) => valuewhen(change(s(x))!=0,s(x),(y-1))
  104.  
  105. // calculate high/low lines and extentions
  106. [ext_high_point, ext_high_point_RT, ext_high_point_future, color_high] = trendline(high_point, false, len)
  107. [ext_low_point, ext_low_point_RT, ext_low_point_future, color_low] = trendline(low_point, true, len)
  108.  
  109. col_h = disp_select ? (col ? color_high : color_high == red ? black : na) : (col ? red : black)
  110. col_l = disp_select ? (col ? color_low : color_low == green ? black : na) : (col ? green : black)
  111.  
  112. plot(high_point, title="Descending", color=col_h, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
  113. plot(ext_high_point, title="Descending", style=circles, color=col_h, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
  114. plot(ext_high_point_RT, title="Descending", style=circles, color=col_h,linewidth=2,transp=0, show_last=100)
  115. plot(ext_high_point_future, title="Descending", style=circles, color=col_h, offset=(len * mult),linewidth=2,transp=0, show_last=100)
  116.  
  117. plot(low_point,title="Ascending", color=col_l, offset=-(len+res_factor),linewidth=2,transp=0, show_last=100)
  118. 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)
  119. 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)
  120. plot(ext_low_point_future, title="Ascending", style=circles, color=col_l, offset=(len * mult),linewidth=2,transp=0, show_last=100)
  121.  
  122. crossoverUpper = not na(col_h) and crossover(close, ext_high_point_RT)
  123. alertcondition(crossoverUpper, title="Alert on Upper Trendline Breakout", message="Upper Trendline Breakout!")
  124. //plotshape(crossoverUpper, style=shape.triangleup, location=location.abovebar, size=size.tiny, color=green)
  125.  
  126. crossunderLower = not na(col_l) and crossunder(close, ext_low_point_RT)
  127. alertcondition(crossunderLower, title="Alert on Lower Trendline Breakout", message="Lower Trendline Breakout!")
  128. //plotshape(crossunderLower, style=shape.triangledown, location=location.belowbar, size=size.tiny, color=red)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement