Indicator · Pine Script v6 · On the chart

Key Reversals with 20-Bar High/Low Lines

by Ayandashaun

This indicator marks key reversal candles that print a new high or low versus the prior lookback window.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code57
Settings2inputs
LicenceNone statedfile: MPL-2.0
Last update2 weeks ago13 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

57 lines of Pine Script v6 from key-reversals.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

key-reversals.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("Key Reversals with 20-Bar High/Low Lines", overlay = true, max_lines_count = 500)

// --- Inputs ---
lookback = input.int(20, "Lookback Period", minval = 1, tooltip = "Number of bars to check for a new high or low.")
showLines = input.bool(true, "Show Horizontal Lines", tooltip = "Draw lines from the previous high/low to the reversal bar.")

// --- Colors ---
color BULL_COLOR = #5b9cf6 // Blue
color BEAR_COLOR = #f23645 // Red

// --- Core Logic ---
isBullCandle = close > open
isBearCandle = close < open

// Reference highs and lows for the lookback period (excluding current bar)
prevHighest = ta.highest(high[1], lookback)
prevLowest  = ta.lowest(low[1], lookback)

// Find the bar index of those highs/lows
// ta.highestbars returns a negative offset, so we add it to the current bar_index
prevHighestIdx = bar_index + ta.highestbars(high[1], lookback)
prevLowestIdx  = bar_index + ta.lowestbars(low[1], lookback)

// --- Key Reversal Detection ---
// Reversals must make a NEW high/low relative to the lookback period

// Bullish Reversal: New low (< prevLowest), close > prev close, bullish candle
isSingleBullish = low < prevLowest and close > close[1] and isBullCandle
isDoubleBullish = low < prevLowest and close > close[1] and close > close[2] and isBullCandle

// Bearish Reversal: New high (> prevHighest), close < prev close, bearish candle
isSingleBearish = high > prevHighest and close < close[1] and isBearCandle
isDoubleBearish = high > prevHighest and close < close[1] and close < close[2] and isBearCandle

// Prioritize double signals
isOnlySingleBullish = isSingleBullish and not isDoubleBullish
isOnlySingleBearish = isSingleBearish and not isDoubleBearish

// --- Visuals: Markers ---

// Single Reversals: One arrow
plotshape(isOnlySingleBullish, "Single Bullish", shape.arrowup,   location.belowbar, BULL_COLOR, size = size.small)
plotshape(isOnlySingleBearish, "Single Bearish", shape.arrowdown, location.abovebar, BEAR_COLOR, size = size.small)

// Double Reversals: Circles
plotshape(isDoubleBullish, "Double Bullish", shape.circle, location.belowbar, BULL_COLOR, size = size.small)
plotshape(isDoubleBearish, "Double Bearish", shape.circle, location.abovebar, BEAR_COLOR, size = size.small)

// --- Visuals: Horizontal Lines ---
if showLines
    if isSingleBullish or isDoubleBullish
        line.new(prevLowestIdx, prevLowest, bar_index, prevLowest, color = BULL_COLOR, style = line.style_dashed)
        
    if isSingleBearish or isDoubleBearish
        line.new(prevHighestIdx, prevHighest, bar_index, prevHighest, color = BEAR_COLOR, style = line.style_dashed)

Overview

A bullish signal needs a lower low, a close above the previous close, and a bullish candle; a bearish signal is the opposite. Double reversals also compare the close with the close two bars back and plot circles, while single reversals plot arrows. When enabled, dashed horizontal lines connect the prior extreme to the signal bar on the price chart. The lookback length and line display are the main settings. It does not generate entries, exits, or stops.

Details

Published by

Ayandashaun on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Candlestick patterns, Support & resistance

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
Lookback Periodint20lookback
Show Horizontal LinesbooltrueshowLines
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

[RS]MTF Candle Sticks

by ricardosantos79
261 stars

This indicator overlays up to five higher-timeframe candlestick groups on the price chart. For each enabled timeframe it requests open, high, low, and close and can show up to five candles, offset by a spacing setting. The main inputs set the candle count, the

Indicator v4 Candlestick patternsSupport & resistance Repaints
Updated 4 years ago · None stated GitHub repository of [RS]MTF Candle Sticks

Candlestick Patterns

by harryguiacorn
219 stars

This indicator plots optional candlestick pattern signals on the price chart. It detects engulfing bars, inside bars, HOLP and LOHP, kicker patterns, body-mass extremes, higher highs and lower lows, ATR warnings, and four-bar fractals. Optional pivot marks use

Indicator v5 Candlestick patternsVolatility & bands
Updated 1 year ago · None stated GitHub repository of Candlestick Patterns

Strat Assistant

by rickyzcarroll
90 stars

The Strat Assistant indicator identifies candlestick patterns from the STRAT methodology on the price chart. It detects inside bars, 2-candle directional bars, 3-candle outside bars, and sequences like 212 and 222 for continuations and reversals. Shapes such a

Indicator v4 Candlestick patternsSupport & resistance
Updated 5 years ago · None stated GitHub repository of Strat Assistant
56 stars

This overlay indicator highlights candles with a body smaller than 50% of their total range by coloring them white on the price chart. Simultaneously, it calculates and plots horizontal lines for open prices across multiple higher timeframes, such as daily, we

Indicator v6 Support & resistanceCandlestick patterns Repaints
Updated 2 months ago · None stated GitHub repository of Daily Weekly Monthly Yearly Opens

This Pine Script indicator identifies liquidity sweeps and confirms signals using a close-back candle pattern directly on the price chart. It tracks recent swing highs and lows over a customizable lookback period. A bullish signal forms when a prior bar sweeps

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 2 months ago · MPL-2.0 GitHub repository of Liquidity Sweep + Close-Back Confirmation

8020 System Indicator

by TamaraKGreen
46 stars

This overlay indicator draws 80/20 price levels and optional 40-point zone bands on the price chart. It marks repairs, cross sections, and forks with optional letter labels, and can highlight confluence when those marks sit near a level. Separate options shade

Indicator v6 Support & resistanceCandlestick patterns
Updated 7 weeks ago · None stated GitHub repository of 8020 System Indicator

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers