Indicator · Pine Script v6 · On the chart

Taught to Trade - Swing Ratio Grid

by obekheet

Calculates retracement grid levels on the price chart using confirmed swing pivot highs and lows based on a user-defined pivot lookback.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code152
Settings8inputs
LicenceMPL-2.0
Last update12 days ago16 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

152 lines of Pine Script v6 from scripts/swing-ratio-grid.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

swing-ratio-grid.pine
//@version=6
// Taught to Trade - Swing Ratio Grid
//
// A retracement grid that makes its two hidden parameters visible.
//
// Every retracement rule depends on two things it usually refuses to state:
//   (1) WHICH SWING you measured, and
//   (2) WHICH RATIO SET you used.
// This tool puts both on the chart. The swing comes from a pivot lookback you set
// yourself, and the pivot is only used once it is CONFIRMED - that many bars after
// it formed - so nothing here is drawn from information that did not exist at the time.
//
// The ratio set is switchable, including an arbitrary 0.4 / 0.5 / 0.6 control grid that
// has no connection to the golden ratio. Switching between sets and watching how little
// the levels move is the entire point of the tool.
//
// Marking where price traded is a fact. Nothing here predicts, ranks or scores anything,
// and no buy/sell signal is sent. Alerts are conditions you arm yourself.
//
// All series reads happen at top level; conditional blocks touch only var scalars.
indicator("Taught to Trade - Swing Ratio Grid", shorttitle = "TT SWING GRID",
     overlay = true, max_bars_back = 5000)

// ─────────────────────────────── Inputs ───────────────────────────────
grpS = "Swing"
pivLen = input.int(20, "Swing pivot lookback (bars each side)", minval = 2, maxval = 200, group = grpS,
     tooltip = "The parameter most retracement rules leave unstated. A pivot is only confirmed this many bars AFTER it forms, so every level drawn here uses confirmed structure only. Raise it for fewer, larger swings.")

grpR = "Ratio set"
setName = input.string("Fibonacci", "Grid", group = grpR,
     options = ["Fibonacci", "Arbitrary control (0.4/0.5/0.6)", "Thirds"],
     tooltip = "Switch between the sets and watch how little the levels move. On most instruments the difference is smaller than one bar's range. That comparison is the reason this input exists.")

grpD = "Draw"
showGrid  = input.bool(true, "Grid levels",        group = grpD)
showSwing = input.bool(true, "Swing high / low",   group = grpD)
showTbl   = input.bool(true, "Readout table",      group = grpD)
tblPosIn  = input.string("Bottom left", "Table position", group = grpD,
     options = ["Bottom left", "Bottom right", "Top left", "Top right", "Middle right"],
     tooltip = "Bottom left sits over the oldest bars, away from the price scale and the badges TradingView draws in the bottom-right corner.")

colGrid  = input.color(color.new(#2F6FED, 0), "Grid",  group = grpD, inline = "c1")
colSwing = input.color(color.new(#8A94A6, 0), "Swing", group = grpD, inline = "c1")

// ──────────────────────── Pivots (top level only) ────────────────────────
ph = ta.pivothigh(pivLen, pivLen)
pl = ta.pivotlow(pivLen, pivLen)

var float swHi    = na
var int   swHiBar = na
var float swLo    = na
var int   swLoBar = na

if not na(ph)
    swHi    := ph
    swHiBar := bar_index - pivLen
if not na(pl)
    swLo    := pl
    swLoBar := bar_index - pivLen

// ─────────────────────────── Ratio selection ───────────────────────────
isFib   = setName == "Fibonacci"
isThird = setName == "Thirds"

r1 = isFib ? 0.236 : isThird ? 0.333 : 0.4
r2 = isFib ? 0.382 : isThird ? 0.500 : 0.5
r3 = isFib ? 0.500 : isThird ? 0.667 : 0.6
r4 = isFib ? 0.618 : na
r5 = isFib ? 0.786 : na

bool  ok  = not na(swHi) and not na(swLo) and swHi > swLo
float rng = ok ? swHi - swLo : na

f_lvl(float r) => ok and not na(r) and rng > 0 ? swLo + rng * r : na

l1 = f_lvl(r1)
l2 = f_lvl(r2)
l3 = f_lvl(r3)
l4 = f_lvl(r4)
l5 = f_lvl(r5)

// ─────────────────────────────── Draw ───────────────────────────────
plot(showGrid  ? l1 : na, "Grid level 1", color = colGrid,  linewidth = 1, style = plot.style_linebr)
plot(showGrid  ? l2 : na, "Grid level 2", color = colGrid,  linewidth = 1, style = plot.style_linebr)
plot(showGrid  ? l3 : na, "Grid level 3", color = colGrid,  linewidth = 2, style = plot.style_linebr)
plot(showGrid  ? l4 : na, "Grid level 4", color = colGrid,  linewidth = 1, style = plot.style_linebr)
plot(showGrid  ? l5 : na, "Grid level 5", color = colGrid,  linewidth = 1, style = plot.style_linebr)

plot(showSwing and ok ? swHi : na, "Swing high", color = colSwing, linewidth = 1, style = plot.style_linebr)
plot(showSwing and ok ? swLo : na, "Swing low",  color = colSwing, linewidth = 1, style = plot.style_linebr)

// ─────────────────────────────── Table ───────────────────────────────
bgCol  = color.new(#0E1526, 10)
txtCol = color.new(#E6EAF2, 0)
hdrCol = color.new(#2F6FED, 0)

tblPos = tblPosIn == "Bottom right" ? position.bottom_right : tblPosIn == "Top left" ? position.top_left :
     tblPosIn == "Top right" ? position.top_right : tblPosIn == "Middle right" ? position.middle_right : position.bottom_left

var table t = table.new(tblPos, 2, 7, border_width = 1,
     border_color = color.new(#2F6FED, 70))

f_num(float v) => na(v) ? "-" : str.tostring(v, format.mintick)

f_row(int r, string k, string v) =>
    table.cell(t, 0, r, k, text_color = txtCol, text_size = size.small,
         text_halign = text.align_left,  bgcolor = bgCol)
    table.cell(t, 1, r, v, text_color = txtCol, text_size = size.small,
         text_halign = text.align_right, bgcolor = bgCol)

// retracement position of the current close inside the swing
retrPct = ok and rng > 0 ? (close - swLo) / rng * 100.0 : na

// short label for the active set
setShort = isFib ? "Fibonacci" : isThird ? "Thirds" : "Control 0.4/0.5/0.6"

if barstate.islast and showTbl
    table.cell(t, 0, 0, "SWING GRID", text_color = color.white, text_size = size.small, bgcolor = hdrCol)
    table.cell(t, 1, 0, setShort,     text_color = color.white, text_size = size.small, bgcolor = hdrCol)
    f_row(1, "Swing high",        f_num(swHi))
    f_row(2, "Swing low",         f_num(swLo))
    f_row(3, "Swing range",       f_num(rng))
    f_row(4, "Pivot lookback",    str.tostring(pivLen) + " bars each side")
    f_row(5, "Confirmed after",   str.tostring(pivLen) + " bars")
    f_row(6, "Close in swing",    na(retrPct) ? "-" : str.tostring(retrPct, "#.#") + "%")

// ─────────────── Alerts (conditions you arm yourself) ───────────────
// Every ta.* call is evaluated at top level on every bar so its history stays
// consistent. Calling them inside the alertcondition expression would let the
// short-circuit skip bars and corrupt the crossover state.
xL1 = ta.cross(close, l1)
xL2 = ta.cross(close, l2)
xL3 = ta.cross(close, l3)
xL4 = ta.cross(close, l4)
xL5 = ta.cross(close, l5)
xHi = ta.crossover(close, swHi)
xLo = ta.crossunder(close, swLo)

alertcondition(not na(l1) and xL1, title = "Close crossed grid level 1",
     message = "Swing Ratio Grid: close crossed the first grid level.")
alertcondition(not na(l2) and xL2, title = "Close crossed grid level 2",
     message = "Swing Ratio Grid: close crossed the second grid level.")
alertcondition(not na(l3) and xL3, title = "Close crossed the midpoint level",
     message = "Swing Ratio Grid: close crossed the midpoint grid level.")
alertcondition(not na(l4) and xL4, title = "Close crossed grid level 4",
     message = "Swing Ratio Grid: close crossed the fourth grid level.")
alertcondition(not na(l5) and xL5, title = "Close crossed grid level 5",
     message = "Swing Ratio Grid: close crossed the fifth grid level.")
alertcondition(not na(swHi) and xHi, title = "Close crossed above the swing high",
     message = "Swing Ratio Grid: close crossed above the confirmed swing high.")
alertcondition(not na(swLo) and xLo, title = "Close crossed below the swing low",
     message = "Swing Ratio Grid: close crossed below the confirmed swing low.")

Overview

Taught to Trade - Swing Ratio Grid allows traders to compare traditional Fibonacci levels against alternative ratio sets, such as thirds or an arbitrary 0.4/0.5/0.6 control grid, to evaluate price reaction against different measurements. The indicator includes options to display swing high and low levels, grid lines, and an informational readout table. Levels are calculated strictly on confirmed historical pivots without repainting.

Details

Published by

obekheet on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

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.

Topics

pine-scriptpinescripttechnical-analysistrading-indicatorstradingviewtradingview-indicators

Settings

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

SettingTypeDefaultVariable
Swing pivot lookback (bars each side)int20pivLen
GridstringFibonaccisetName
Grid levelsbooltrueshowGrid
Swing high / lowbooltrueshowSwing
Readout tablebooltrueshowTbl
Table positionstringBottom lefttblPosIn
Gridcolorcolor.new(#2F6FED, 0)colGrid
Swingcolorcolor.new(#8A94A6, 0)colSwing
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

948 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 10 days 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
330 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 2 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 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

BZ-tracker

by Allen1212
303 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 2 years ago · None stated GitHub repository of BZ-tracker

Function - Linear Regression

by ricardosantos79
261 stars

This indicator fits a linear regression through stored pivot highs and lows and draws the resulting channel on the price chart. A function returns predicted values, maximum and minimum deviations, and the average absolute deviation. Confirmed two-bar pivots up

Indicator v5 TrendSupport & resistance
Updated 4 years ago · None stated GitHub repository of Function - Linear Regression

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