Indicator · Pine Script v6 · On the chart

Previous HTF High/Low

by haydenfowler

Previous HTF High/Low is an indicator that plots horizontal lines representing the high and low levels of previously completed higher timeframe bars directly on the main price chart.

View on GitHub

GitHub stars3on the repository
Pine versionv6current
Lines of code153
Settings4inputs
LicenceNone statedfile: MPL-2.0
Last update6 weeks ago7 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

153 lines of Pine Script v6 from prev-htf-high-low.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

prev-htf-high-low.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=6
indicator("Previous HTF High/Low", overlay=true, max_lines_count=500)

// USER SETTINGS

timeframe_input = input.string("D", title="Timeframe",
                               options=["Auto", "1m", "5m", "15m", "30m", "1h", "4h", "D", "W", "M"],
                               tooltip="Select 'Auto' to automatically use one timeframe higher than the chart, or choose a specific timeframe")

line_color_input = input.color(#FFA500, title="Line Color")
line_thickness_input = input.int(1, title="Line Thickness", minval=1, maxval=10)
line_style_input = input.string("Solid", title="Line Style", options=["Solid", "Dotted", "Dashed"])

// TIMEFRAME HELPERS

get_timeframe_string(tf_input) =>
    switch tf_input
        "1m" => "1"
        "5m" => "5"
        "15m" => "15"
        "30m" => "30"
        "1h" => "60"
        "4h" => "240"
        => tf_input  // "Auto", "D", "W", "M" pass through unchanged

get_next_higher_timeframe() =>
    chart_tf = timeframe.in_seconds() / 60
    result = switch
        chart_tf < 1 => "1"
        chart_tf < 5 => "5"
        chart_tf < 15 => "15"
        chart_tf < 30 => "30"
        chart_tf < 60 => "60"
        chart_tf < 240 => "240"
        chart_tf < 1440 => "D"
        chart_tf < 10080 => "W"
        => "M"
    result

selected_tf = timeframe_input == "Auto" ? get_next_higher_timeframe() : get_timeframe_string(timeframe_input)

line_style = switch line_style_input
    "Solid" => line.style_solid
    "Dotted" => line.style_dotted
    "Dashed" => line.style_dashed
    => line.style_solid

// DISPLAY COUNT
// How many periods of the selected TF fit into the next-higher TF

get_next_higher_for_tf(tf_seconds) =>
    tf_minutes = tf_seconds / 60
    result = switch
        tf_minutes < 1 => "1"
        tf_minutes < 5 => "5"
        tf_minutes < 15 => "15"
        tf_minutes < 30 => "30"
        tf_minutes < 60 => "60"
        tf_minutes < 240 => "240"
        tf_minutes < 1440 => "D"
        tf_minutes < 10080 => "W"
        => "M"
    result

selected_tf_seconds = timeframe.in_seconds(selected_tf)
next_higher_tf = get_next_higher_for_tf(selected_tf_seconds)
next_higher_tf_seconds = timeframe.in_seconds(next_higher_tf)
display_count = int(next_higher_tf_seconds / selected_tf_seconds)

// GUARD — only render when chart TF is lower than selected TF
chart_is_lower = timeframe.in_seconds() < selected_tf_seconds

// DATA

[htf_high, htf_low] = request.security(syminfo.tickerid, selected_tf, [high, low])

new_period = timeframe.change(selected_tf)

// STORAGE

var float[] prev_highs = array.new<float>()
var float[] prev_lows = array.new<float>()
var int[] period_start_bars = array.new<int>()
var int[] period_end_bars = array.new<int>()
var line[] high_lines = array.new<line>()
var line[] low_lines = array.new<line>()

var float completed_high = na
var float completed_low = na
var int current_period_start = na

// PERIOD TRACKING

if chart_is_lower
    if new_period
        // Store the just-completed period (if we have data)
        if not na(completed_high) and not na(current_period_start)
            // Cap array size — delete oldest if at limit
            if array.size(prev_highs) >= display_count
                array.shift(prev_highs)
                array.shift(prev_lows)
                array.shift(period_start_bars)
                array.shift(period_end_bars)
                old_h_line = array.shift(high_lines)
                old_l_line = array.shift(low_lines)
                line.delete(old_h_line)
                line.delete(old_l_line)

            array.push(prev_highs, completed_high)
            array.push(prev_lows, completed_low)
            array.push(period_start_bars, current_period_start)
            array.push(period_end_bars, bar_index - 1)

            // Create lines for this completed period
            array.push(high_lines, line.new(x1=current_period_start, y1=completed_high,
                                            x2=bar_index, y2=completed_high,
                                            color=line_color_input, width=line_thickness_input, style=line_style))
            array.push(low_lines, line.new(x1=current_period_start, y1=completed_low,
                                           x2=bar_index, y2=completed_low,
                                           color=line_color_input, width=line_thickness_input, style=line_style))

            // Update the second-to-last entry's lines to extend into this period
            entry_count = array.size(high_lines)
            if entry_count >= 2
                prev_h = array.get(high_lines, entry_count - 2)
                prev_l = array.get(low_lines, entry_count - 2)
                if not na(prev_h)
                    line.set_x2(prev_h, bar_index - 1)
                if not na(prev_l)
                    line.set_x2(prev_l, bar_index - 1)

        // Start tracking the new period
        completed_high := htf_high
        completed_low := htf_low
        current_period_start := bar_index
    else
        // Update running high/low for current period
        if not na(htf_high)
            completed_high := htf_high
        if not na(htf_low)
            completed_low := htf_low

    // Extend the most recent completed period's lines to current bar
    n = array.size(high_lines)
    if n > 0
        last_h = array.get(high_lines, n - 1)
        last_l = array.get(low_lines, n - 1)
        if not na(last_h)
            line.set_x2(last_h, bar_index)
        if not na(last_l)
            line.set_x2(last_l, bar_index)

Overview

Users can manually specify a higher timeframe or set it to automatically select the next standard timeframe above the active chart. Line settings allow customization of line color, thickness, and line styles such as solid, dotted, or dashed. The script automatically guards against rendering if the chart timeframe is equal to or higher than the selected timeframe.

Details

Published by

haydenfowler on GitHub

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
TimeframestringDtimeframe_input
Line Colorcolor#FFA500line_color_input
Line Thicknessint1line_thickness_input
Line StylestringSolidline_style_input
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

947 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 5 weeks ago · GPL-3.0 GitHub repository of Moving Average Channel

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows
368 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 3 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 8 days ago · MIT GitHub repository of Volume Profile and Indicator by DGT

Swing labels

by tradoor-terminal
320 stars

Calculates swing highs and swing lows on the price chart using pivot high and pivot low detection over a specified length. It places labels above pivot highs and below pivot lows to highlight key structural swing points. The indicator measures and displays the

Indicator v6 Support & resistanceTrend
Updated 3 days ago · AGPL-3.0 GitHub repository of Swing labels

BZ-tracker

by Allen1212
302 stars

The BZ-tracker indicator marks ARK Invest buy and sell transactions on stock charts with labels. It plots green labels for purchases and red labels for sales, each annotated with a multiplier such as X1 or X2 to show the number of ARK ETFs trading the stock on

Indicator v4 Support & resistanceScreeners & dashboards
Updated 3 years ago · None stated GitHub repository of BZ-tracker

More Support & resistance scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers