Get the TradingView chart's lowest value with Pine Script
Get the TradingView chart’s lowest value with Pine Script
Section titled “Get the TradingView chart’s lowest value with Pine Script”TL;DR
Walk a series with a persistentvarso you always know the lowest value visible on the chart, regardless of timeframe or data gaps.
At a Glance
Section titled “At a Glance”| Difficulty | Beginner |
| Time to implement | 10-15 min |
| Category | Utility |
Quick Actions
Section titled “Quick Actions”Quick Start
Section titled “Quick Start”//@version=5indicator("Chart lowest", overlay=false)
var float chartLowest = nachartLowest := na(chartLowest) ? low : math.min(chartLowest, low)
plot(chartLowest, "Lowest low on chart", color=color.red)Why It Matters
Section titled “Why It Matters”TradingView charts can span years of data. When you need to mark the lowest low (or the lowest value of any custom series) for visual cues, alerts, or risk controls, scanning the entire visible history manually is not feasible. A lightweight helper function keeps a running minimum so you can reuse it across scripts.
What You’ll Learn
Section titled “What You’ll Learn”- Creating a persistent helper that tracks the lowest value in a series
- Reusing the helper for lows, moving averages, or custom series with gaps
- Displaying the chart-wide minimum and updating it when new data loads
- Guarding against
navalues and history limitations
Quick Reference
Section titled “Quick Reference”| Call | Purpose |
|---|---|
var | Persists the lowest value across bars. |
math.min(a, b) | Compares current data against the stored minimum. |
nz(value, default) | Provides a fallback when the persistent series is na. |
plot(series) | Visualises the running minimum on the chart. |
timeframe.expand | (Optional) Loads additional history if you need a longer window. |
Implementation Blueprint
Section titled “Implementation Blueprint”-
Store the running minimum in a persistent variable
Initialise the variable with the first value, then compare each new bar.var float lowestSeries = dataSerieslowestSeries := math.min(lowestSeries, dataSeries) -
Handle
nainputs gracefully
Data with gaps (e.g., conditional series) should skipnavalues.if not na(dataSeries)lowestSeries := math.min(lowestSeries, dataSeries) -
Expose the helper as a reusable function
Wrap the logic so you can request the chart lowest for any series.chartLowest(series float) =>var float lowest = seriesif not na(series)lowest := math.min(lowest, series)lowest
Example Playbook
Section titled “Example Playbook”//@version=5indicator("Chart-wide lows", overlay=true)
chartLowest(series float) => var float lowest = series if not na(series) lowest := math.min(lowest, series) lowest
lowestLow = chartLowest(low)emaValue = ta.ema(close, 50)lowestEMA = chartLowest(emaValue)fridayClose = dayofweek == dayofweek.friday ? close : nalowestFri = chartLowest(fridayClose)
plot(lowestLow, "Lowest low", color=color.red)plot(lowestEMA, "Lowest EMA", color=color.orange)plot(lowestFri, "Lowest Friday close", color=color.teal)- The `var` variable remembers prior minima, even when the helper is called multiple times.
- Series with gaps (Friday closes) still update correctly because `na` values are ignored.
- Multiple calls reuse the same logic to track different series simultaneously.
Pro Tips & Pitfalls
Section titled “Pro Tips & Pitfalls”- The chart-wide minimum is not necessarily the all-time low—it reflects only the bars currently loaded.
- If you change symbols or zoom levels, the helper automatically recomputes after the new data loads.
- To reset the minimum (e.g., for session-based tracking), reinitialise the
varwhenever your session boundary is reached. - For highest values, mirror the logic using
math.max.
Troubleshooting & FAQ
Section titled “Troubleshooting & FAQ”The minimum changes when I scroll—why?
Section titled “The minimum changes when I scroll—why?”TradingView loads additional bars when you scroll back. More history can introduce lower lows, so the running minimum updates accordingly.
How do I start fresh at a session open?
Section titled “How do I start fresh at a session open?”Reset the persistent variable when session.isfirstbar is true, or maintain separate minima per session using a dictionary.
Key Takeaways
Section titled “Key Takeaways”- A simple
var+math.minpattern lets you track chart-wide minima for any series. - Always account for
navalues and the current history window. - Reuse the helper to highlight extremes, build alerts, or compute drawdown references.
- Extend the idea to maxima or other cumulative statistics with minimal changes.
Keep Going
Section titled “Keep Going”- Open AI Editor to apply the pattern with guided assistance
- Browse PineScript 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.