Skip to content

How to get the price coordinates from a TradingView trend line?

How to get the price coordinates from a TradingView trend line?

Section titled “How to get the price coordinates from a TradingView trend line?”

TL;DR
Keep the line reference you created, then call line.get_y1(lineId), line.get_y2(lineId), or line.get_price(lineId, bar_index) to read its price coordinates.

DifficultyIntermediate
Time to implement10-15 min
CategoryDrawing Tools
//@version=5
indicator("Line price probe", overlay=true)
var line trendLine = na
if barstate.isnew and ta.crossover(close, ta.sma(close, 50))
trendLine := line.new(bar_index[5], high[5], bar_index, high,
extend=extend.right, color=color.orange, width=2)
if not na(trendLine)
entryPrice = line.get_y1(trendLine)
currentAtLine = line.get_price(trendLine, bar_index)
label.new(bar_index, currentAtLine, "Line @ " + str.tostring(currentAtLine, format.mintick))
Tip. `line.get_price(lineId, x)` returns `na` unless the line reaches that `x` (or is extended with `extend.right/left`). Extend the line if you plan to probe future bars.

Once you know a line’s price you can evaluate whether price is above or below a trend, trigger alerts when it touches key support, or anchor stop levels directly on the line. Reading the line back from Pine lets you close the loop between drawing tools and automation.

  • Keep line references around so you can query them later.
  • Read the exact prices at a line’s start and end points.
  • Sample the line at the current bar (or any bar) using line.get_price().
  • Annotate charts and drive logic based on line-derived levels.
CallPurpose
line.new(x1, y1, x2, y2)Draws a trend line and returns a reference handle.
line.get_y1(lineId)Returns the starting point’s price.
line.get_y2(lineId)Returns the ending point’s price.
line.get_price(lineId, x)Samples the line’s price at the supplied bar index.
line.set_extend(lineId, extend)Extends the line so future line.get_price() calls succeed.
  1. Store the line reference
    Assign line.new() to a var so the handle persists and can be reused.

    var line swingLine = na
    if ta.pivothigh(high, 5, 5)
    swingLine := line.new(bar_index[5], high[5], bar_index, high, color=color.teal)
  2. Inspect the endpoints
    Fetch the prices at the creation points for logging or labelling.

    if not na(swingLine)
    startPrice = line.get_y1(swingLine)
    endPrice = line.get_y2(swingLine)
  3. Sample the line at the current bar
    Extend the line (if necessary) and retrieve the price at bar_index to compare with market price.

    line.set_extend(swingLine, extend.right)
    linePriceNow = line.get_price(swingLine, bar_index)
    isAboveLine = close > linePriceNow
//@version=5
indicator("Trend line touch tracker", overlay=true, max_lines_count=50, max_labels_count=500)
left = input.int(5, "Pivot left", minval=1)
right = input.int(5, "Pivot right", minval=1)
lineColor = input.color(color.orange, "Line colour")
var line activeTrend = na
var label infoLabel = na
// Create/refresh the trend line when a new pivot high forms
if ta.pivothigh(high, left, right)
pivotBar = bar_index[right]
pivotPrice = high[right]
if na(activeTrend)
activeTrend := line.new(pivotBar, pivotPrice, bar_index, high,
extend=extend.right, color=lineColor, width=2)
else
line.set_xy1(activeTrend, pivotBar, pivotPrice)
line.set_xy2(activeTrend, bar_index, high)
line.set_extend(activeTrend, extend.right)
if not na(activeTrend)
startPrice = line.get_y1(activeTrend)
latestPrice = line.get_y2(activeTrend)
projectedNow = line.get_price(activeTrend, bar_index)
touchedLine = not na(projectedNow) and math.abs(close - projectedNow) <= syminfo.mintick * 2
if not na(infoLabel)
label.delete(infoLabel)
infoText = "Start: " + str.tostring(startPrice, format.mintick) +
"\nLatest: " + str.tostring(latestPrice, format.mintick) +
"\nNow: " + (na(projectedNow) ? "n/a" : str.tostring(projectedNow, format.mintick))
infoLabel := label.new(bar_index, high, infoText,
textcolor=color.white,
style=label.style_label_down,
bgcolor=color.new(color.orange, 65))
if touchedLine
alert("Price touched trend line at " + str.tostring(projectedNow, format.mintick))
Why this works.
  • The script keeps the line reference alive and updates its anchors as new pivots appear.
  • `line.get_price()` projects the level onto the current bar, enabling touch detection.
  • Labels summarise the line's start, last anchor, and projected price for quick review.
  • Ensure the line actually reaches the x index you query. Use line.set_extend(lineId, extend.right) or supply an x within the line’s bounds.
  • Delete or reuse old lines to stay within TradingView’s line limits (max_lines_count).
  • When sampling historic prices, pass the bar index of interest: line.get_price(lineId, bar_index - 10).
  • Combine line prices with plot() or hline() to compare them against other indicators visually.

The x coordinate is outside the current line. Extend the line or query a bar within the drawn segment.

I lost the line reference after recreating it

Section titled “I lost the line reference after recreating it”

Store the handle in a var variable. When you overwrite it with a new line.new(), update the variable so future calls reference the latest line.

Can I convert the price to ticks or percent?

Section titled “Can I convert the price to ticks or percent?”

Yes—once you have the price, compare it with close (or any other reference) and divide by syminfo.mintick or close to express the difference in ticks or percent.

  • Keep the line.new() handle in a persistent variable so you can query it later.
  • line.get_y1() and line.get_y2() return the endpoints, while line.get_price() samples any bar along the line.
  • Extend lines if you want to probe future bars; otherwise queries outside the drawn range return na.
  • Use the retrieved prices for alerts, labels, stop placement, or comparisons against market price.

Adapted from tradingcode.net, optimised for Algo Trade Analytics users.