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 thelinereference you created, then callline.get_y1(lineId),line.get_y2(lineId), orline.get_price(lineId, bar_index)to read its price coordinates.
At a Glance
Section titled “At a Glance”| Difficulty | Intermediate |
| Time to implement | 10-15 min |
| Category | Drawing Tools |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Line price probe", overlay=true)
var line trendLine = naif 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))Why It Matters
Section titled “Why It Matters”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.
What You’ll Learn
Section titled “What You’ll Learn”- 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.
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
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. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Store the line reference
Assignline.new()to avarso the handle persists and can be reused.var line swingLine = naif ta.pivothigh(high, 5, 5)swingLine := line.new(bar_index[5], high[5], bar_index, high, color=color.teal) -
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) -
Sample the line at the current bar
Extend the line (if necessary) and retrieve the price atbar_indexto compare with market price.line.set_extend(swingLine, extend.right)linePriceNow = line.get_price(swingLine, bar_index)isAboveLine = close > linePriceNow
Example Playbook
Section titled “Example Playbook”//@version=5indicator("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 = navar label infoLabel = na
// Create/refresh the trend line when a new pivot high formsif 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))- 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.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- Ensure the line actually reaches the
xindex you query. Useline.set_extend(lineId, extend.right)or supply anxwithin 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()orhline()to compare them against other indicators visually.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”line.get_price() returns na
Section titled “line.get_price() returns na”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.
Key Takeaways
Section titled “Key Takeaways”- Keep the
line.new()handle in a persistent variable so you can query it later. line.get_y1()andline.get_y2()return the endpoints, whileline.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.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse Strategy Examples for ready-to-run templates
- Connect to Live Trading when you are ready to automate
Adapted from tradingcode.net, optimised for Algo Trade Analytics users.