Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Angle-based Trend Indicator", shorttitle="Trend Angle", overlay=true)
- // User inputs
- src = input(close, title="Source")
- lookback = input.int(21, title="Lookback Period", minval=1)
- angle_threshold = input.int(68, title="Angle Threshold (degrees)", minval=1, maxval=89)
- bullish_color = input(color.green, title="Bullish Angle Color")
- bearish_color = input(color.red, title="Bearish Angle Color")
- neutral_color = input(color.gray, title="Neutral Angle Color")
- projection_enabled = input(true, title="Enable Projection Line")
- projection_length = input.int(21, title="Projection Length", minval=1)
- proj_bullish_color = input(color.green, title="Projection Bullish Color")
- proj_bearish_color = input(color.red, title="Projection Bearish Color")
- proj_neutral_color = input(color.gray, title="Projection Neutral Color")
- // Custom function to convert radians to degrees
- rad2deg(rad) =>
- rad * (180 / 3.14159265359)
- // Function to calculate the angle in degrees
- angle(src, period) =>
- rad2deg(math.atan((src - src[period]) / (period * syminfo.mintick)))
- // Calculate the trend angle
- trend_angle = angle(src, lookback)
- // Calculate the line points based on the trend angle
- line_point1 = line.new(x1=bar_index[lookback], y1=src[lookback], x2=bar_index, y2=src, color=color.gray, width=2)
- line.delete(line_point1[1])
- // Set line color based on the trend angle
- line_color = trend_angle < -angle_threshold ? bearish_color : (trend_angle > angle_threshold ? bullish_color : neutral_color)
- line.set_color(line_point1, color=line_color)
- // Display the angle value as a label above or below the starting point of the line
- var label angle_label = label.new(na, na, "", color=color.gray, textcolor=color.white, style=label.style_label_down)
- label.set_xy(angle_label, bar_index[lookback], src[lookback])
- label.set_text(angle_label, str.tostring(trend_angle, "#.##°"))
- label.set_color(angle_label, color=line_color)
- // Calculate and draw the projection line
- if projection_enabled
- projection_slope = (src - src[lookback]) / lookback
- projection_y1 = src
- projection_y2 = src + (projection_slope * projection_length)
- projection_line = line.new(x1=bar_index, y1=projection_y1, x2=bar_index + projection_length, y2=projection_y2, color=color.gray, width=2)
- line.delete(projection_line[1])
- line.set_color(projection_line, color=line_color)
- line.set_width(projection_line, 2)
- // Set projection line color based on the trend angle
- projection_color = trend_angle < -angle_threshold ? proj_bearish_color : (trend_angle > angle_threshold ? proj_bullish_color : proj_neutral_color)
- line.set_color(projection_line, color=projection_color)
Advertisement
Add Comment
Please, Sign In to add comment