In a Pine Script indicator I want to use a horizontal line in calculating a setup. This horizontal line changes from time to time and needs to be placed and then repositioned after every setup by the user. But, in Pine Script it’s not possible to access user drawings.
The solution I came up with is to draw the horizontal line from two user inputs: a price input and a time input.
Once the horizontal line is configured the first time, I want to repositioned it from the chart by clicking and dragging from the indicator ‘drag point(s)’. The draggable points appear when the user hovers elements from the indicator, in this case the horizontal line, on the chart.
This works but there’s one problem:
When I hover over the indicator in the chart, I do get drag point options to reposition the horizontal line. But I have to do it in two steps. The price (y axis) and the time (x axis) point have separate drag points.
My question is: can this be changed so I have one drag point for both, so the line can be repositioned from just one drag point?
The current code I use for the horizontal line is as follows:
//@version=5
indicator(title="Horizontal line", overlay=true)
// HORIZONTAL LINE
hLinePrice = input.price( 0, title="Horizontal line price" )
hLineTimeStart = input.time( timestamp("26 July 2024 15:00 -0500"), title="Horizontal line time")
hLineBarsBack = (time - hLineTimeStart) / (1000 * timeframe.in_seconds(timeframe.period))
var line horizontalLine = na
if not barstate.isconfirmed or (barstate.isrealtime and barstate.islast and not barstate.isconfirmed)
horizontalLine := line.new(x1=bar_index[hLineBarsBack], y1=hLinePrice, x2=bar_index, y2=hLinePrice, width=1, extend=extend.right)
line.set_color(id=horizontalLine, color=color.black)
line.set_style(id=horizontalLine, style=line.style_solid)
if barstate.isconfirmed
line.delete(id=horizontalLine)
ricksportel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.