xmd79

Angle-based Trend Indicator

Apr 28th, 2023
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. //@version=5
  2. indicator("Angle-based Trend Indicator", shorttitle="Trend Angle", overlay=true)
  3.  
  4. // User inputs
  5. src = input(close, title="Source")
  6. lookback = input.int(21, title="Lookback Period", minval=1)
  7. angle_threshold = input.int(68, title="Angle Threshold (degrees)", minval=1, maxval=89)
  8. bullish_color = input(color.green, title="Bullish Angle Color")
  9. bearish_color = input(color.red, title="Bearish Angle Color")
  10. neutral_color = input(color.gray, title="Neutral Angle Color")
  11. projection_enabled = input(true, title="Enable Projection Line")
  12. projection_length = input.int(21, title="Projection Length", minval=1)
  13. proj_bullish_color = input(color.green, title="Projection Bullish Color")
  14. proj_bearish_color = input(color.red, title="Projection Bearish Color")
  15. proj_neutral_color = input(color.gray, title="Projection Neutral Color")
  16.  
  17. // Custom function to convert radians to degrees
  18. rad2deg(rad) =>
  19. rad * (180 / 3.14159265359)
  20.  
  21. // Function to calculate the angle in degrees
  22. angle(src, period) =>
  23. rad2deg(math.atan((src - src[period]) / (period * syminfo.mintick)))
  24.  
  25. // Calculate the trend angle
  26. trend_angle = angle(src, lookback)
  27.  
  28. // Calculate the line points based on the trend angle
  29. line_point1 = line.new(x1=bar_index[lookback], y1=src[lookback], x2=bar_index, y2=src, color=color.gray, width=2)
  30. line.delete(line_point1[1])
  31.  
  32. // Set line color based on the trend angle
  33. line_color = trend_angle < -angle_threshold ? bearish_color : (trend_angle > angle_threshold ? bullish_color : neutral_color)
  34. line.set_color(line_point1, color=line_color)
  35.  
  36. // Display the angle value as a label above or below the starting point of the line
  37. var label angle_label = label.new(na, na, "", color=color.gray, textcolor=color.white, style=label.style_label_down)
  38. label.set_xy(angle_label, bar_index[lookback], src[lookback])
  39. label.set_text(angle_label, str.tostring(trend_angle, "#.##°"))
  40. label.set_color(angle_label, color=line_color)
  41.  
  42. // Calculate and draw the projection line
  43. if projection_enabled
  44. projection_slope = (src - src[lookback]) / lookback
  45. projection_y1 = src
  46. projection_y2 = src + (projection_slope * projection_length)
  47. projection_line = line.new(x1=bar_index, y1=projection_y1, x2=bar_index + projection_length, y2=projection_y2, color=color.gray, width=2)
  48. line.delete(projection_line[1])
  49. line.set_color(projection_line, color=line_color)
  50. line.set_width(projection_line, 2)
  51.  
  52. // Set projection line color based on the trend angle
  53. projection_color = trend_angle < -angle_threshold ? proj_bearish_color : (trend_angle > angle_threshold ? proj_bullish_color : proj_neutral_color)
  54. line.set_color(projection_line, color=projection_color)
  55.  
Advertisement
Add Comment
Please, Sign In to add comment