Indicator · Pine Script v5 · On the chart

Pattern Trade Levels

by zachisit

Pattern Trade Levels is a price-chart overlay that plots levels typed in by the user for a chart pattern.

View on GitHub

GitHub stars11on the repository
Pine versionv5runs as is
Lines of code79
Settings10inputs
LicenceMIT
Last update4 days ago24 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

79 lines of Pine Script v5 from scripts/pattern_levels.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

pattern_levels.pine
//@version=5
// Pattern Trade Levels — companion to scripts/pattern_scan.py (issue #348).
// Type in the boundary and pattern low from any chart pattern; draws
// confirm (+3%), measured-move target, both stop conventions, and an R:R table.
// Levels are drawn as hand-built long-dash segments (Pine's style_dashed is a
// fixed micro-dash; the drawing-tool look needs manual segments).
indicator("Pattern Trade Levels", overlay = true, max_lines_count = 500, max_labels_count = 20)

B        = input.float(0.0,   "Boundary / breakout level (B)",            step = 0.01)
L        = input.float(0.0,   "Pattern low (L)",                          step = 0.01)
S        = input.float(0.0,   "Structural stop (last higher low, 0=off)", step = 0.01)
confPct  = input.float(3.0,   "Confirm / negation buffer %",              step = 0.1, minval = 0)
useLog   = input.bool(true,   "Log measured move (BxB/L) — off = B+(B-L)")
entryOpt = input.string("Confirm", "Entry for R:R", options = ["Confirm", "Boundary", "Current price"])
dashOn   = input.int(6, "Dash length (bars)",  minval = 1, group = "Style")
dashOff  = input.int(5, "Dash gap (bars)",     minval = 1, group = "Style")
spanBack = input.int(260, "Extend left (bars)", minval = 20, maxval = 5000, group = "Style")
spanFwd  = input.int(30, "Extend right (bars)", minval = 0, maxval = 400, group = "Style")

confirm  = B * (1 + confPct / 100)          // daily close above = validated breakout
negStop  = B * (1 - confPct / 100)          // close back below after breakout = failed
target   = useLog ? B * B / L : B + (B - L)
entry    = entryOpt == "Confirm" ? confirm : entryOpt == "Boundary" ? B : close
stopUsed = S > 0 ? S : negStop
rr       = entry - stopUsed > 0 ? (target - entry) / (entry - stopUsed) : na

var lvlLines  = array.new<line>()
var lvlLabels = array.new<label>()

f_clear() =>
    while array.size(lvlLines) > 0
        line.delete(array.pop(lvlLines))
    while array.size(lvlLabels) > 0
        label.delete(array.pop(lvlLabels))

f_lvl(float y, color col, string txt) =>
    i = math.max(0, bar_index - spanBack)
    rightEdge = bar_index + spanFwd
    // At most 95 segments per level keeps all five levels below 500 IDs.
    spacing = math.max(dashOn + dashOff, int(math.ceil((rightEdge - i) / 95.0)))
    while i < rightEdge
        array.push(lvlLines, line.new(i, y, math.min(i + dashOn, rightEdge), y, color = col, width = 1,
                                      style = line.style_solid))
        i += spacing
    array.push(lvlLabels, label.new(bar_index - 30, y,
               txt + "  " + str.tostring(y, format.mintick),
               color = color.new(color.black, 100), textcolor = col,
               style = label.style_none, size = size.normal))

if barstate.islast
    f_clear()
if barstate.islast and B > 0 and L > 0 and L < B
    f_lvl(B,       color.fuchsia, "boundary")
    f_lvl(confirm, color.aqua,    "confirm +" + str.tostring(confPct, "#.#") + "%")
    f_lvl(target,  color.lime,    "target")
    f_lvl(negStop, color.orange,  "negation stop")
    if S > 0
        f_lvl(S, color.red, "structural stop")

    var table t = table.new(position.top_right, 2, 5, bgcolor = color.new(color.black, 20))
    table.cell(t, 0, 0, "Entry (" + entryOpt + ")", text_color = color.gray,  text_size = size.small)
    table.cell(t, 1, 0, str.tostring(entry, format.mintick),  text_color = color.white, text_size = size.small)
    table.cell(t, 0, 1, "Target",                  text_color = color.gray,  text_size = size.small)
    table.cell(t, 1, 1, str.tostring(target, format.mintick) + "  (+" + str.tostring((target / entry - 1) * 100, "#.#") + "%)",
               text_color = color.lime,  text_size = size.small)
    table.cell(t, 0, 2, "Stop",                    text_color = color.gray,  text_size = size.small)
    table.cell(t, 1, 2, str.tostring(stopUsed, format.mintick) + "  (-" + str.tostring((1 - stopUsed / entry) * 100, "#.#") + "%)",
               text_color = color.orange, text_size = size.small)
    table.cell(t, 0, 3, "R : R",                   text_color = color.gray,  text_size = size.small)
    table.cell(t, 1, 3, na(rr) ? "n/a" : str.tostring(rr, "#.##") + "R",
               text_color = rr >= 2 ? color.lime : color.red, text_size = size.small)
    table.cell(t, 0, 4, "Dist to confirm",         text_color = color.gray,  text_size = size.small)
    table.cell(t, 1, 4, str.tostring((confirm / close - 1) * 100, "#.##") + "%",
               text_color = color.white, text_size = size.small)

alertcondition(B > 0 and ta.crossover(close, confirm), "Confirm breakout",
               "Price crossed above the confirm level — breakout validated")
alertcondition(B > 0 and ta.crossover(close, B), "Boundary touch",
               "Price crossed above the pattern boundary")

Overview

Pattern Trade Levels draws the breakout boundary, a confirmation level and a negation stop from a percentage buffer, a measured-move target using either a log formula or an arithmetic extension, and an optional structural stop. A corner table shows the selected entry, target, stop, and reward-to-risk ratio. Lines appear only when the boundary is above the pattern low, and the script places no orders.

Details

Published by

zachisit on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Support & resistance, Breakout

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.

Topics

algorithmic-tradingbacktestingequitymachine-learningmonte-carloopen-sourcepythonquantitative-financestrategiestrading

Settings

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

SettingTypeDefaultVariable
Boundary / breakout level (B)float0.0B
Pattern low (L)float0.0L
Structural stop (last higher low, 0=off)float0.0S
Confirm / negation buffer %float3.0confPct
Log measured move (BxB/L) — off = B+(B-L)booltrueuseLog
Entry for R:RstringConfirmentryOpt
Dash length (bars)int6dashOn
Dash gap (bars)int5dashOff
Extend left (bars)int260spanBack
Extend right (bars)int30spanFwd
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

Sideway Zones Live

by dearvn
257 stars

Sideway Zones Live is an overlay indicator that estimates inner and outer sideways bands from recent highs and lows with a rational quadratic kernel. It tracks swing pivots over a loopback period and counts bars that remain between the outer high and low. Afte

Indicator v5 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of Sideway Zones Live
219 stars

This indicator identifies High Of Low Price (HOLP) and Low Of High Price (LOHP) candlestick setups based on price extremes over a specified lookback period. A HOLP signal triggers when the current bar closes above the high of a recent session low, whereas a LO

Indicator v5 Candlestick patternsBreakout
Updated 1 year ago · None stated GitHub repository of Candlestick Patterns - HOLP-LOHP
218 stars

This indicator plots dynamic support and resistance levels on the price chart. It detects pivot highs and lows using the pivot length and tracks touches to build strength-based levels. Support and resistance zones appear as colored boxes while lines mark key l

Indicator v6 Support & resistanceBreakout
Updated 3 months ago · None stated GitHub repository of Dynamic Support & Resistance

Donchian Channels (DC)

by mihakralj
99 stars

This indicator calculates and plots Donchian Channels directly on the price chart. It determines the highest high and lowest low over a user-selected lookback period using an optimized monotonic deque algorithm, as well as a middle basis line computed as their

Indicator v6 Volatility & bandsSupport & resistance
Updated 4 weeks ago · Apache-2.0 GitHub repository of Donchian Channels (DC)

Lows from New Highs

by be-thomas
65 stars

This indicator tracks bar lows during consecutive rising highs using Pine Script arrays. When at least four lows accumulate or the price breaches the minimum low in the stack, the script sorts the recorded lows in ascending order and flushes them. It plots up

Indicator v4 Support & resistanceBreakout
Updated 4 days ago · GPL-3.0 GitHub repository of Lows from New Highs

This indicator tracks smart money market structure directly on the price chart. It calculates dual-period swing highs and swing lows to detect Changes of Character (CHoCH), Breaks of Structure (BOS), inducements, and liquidity sweeps. When structural levels ar

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · Apache-2.0 GitHub repository of Market Structure with Inducements & Sweeps [LuxAlgo]

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