I’ve create a line by line.new() in pinescript.
now I want to know how much is my line angle by degree?
how can I get the angle of my line1 ?
//@version=5
indicator("line.new")
var line1 = line.new(0, low, bar_index, high, extend=extend.right)
>Solution :
We can calculate the angle of a line using line.new() in PineScript:
//@version=5
indicator("Line Angle", overlay=true)
// Define the line using line.new
var line1 = line.new(0, low, bar_index, high, extend=extend.right, color=color.blue, width=2)
// Calculate the angle in radians
angleRad = atan(line.get_price(high, bar_index) - line.get_price(low, bar_index), line.get_x2(line1) - line.get_x1(line1))
// Convert angle to degrees
angleDeg = angleRad * (180 / math.pi)
// Print the angle on chart
plotchar(0, "", "Angle: " + tostring(angleDeg, "#.##°"), location=location.top)
We can use the atan() function to calculate the angle in radians, and then convert it to degrees by multiplying it with (180 / math.pi). Finally, we plot the angle on the chart using plotchar().