1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
166 lines of Pine Script v6 from pine/gex_levels_tradingview.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
gex_levels_tradingview.pine
//@version=6
indicator("GEX Levels from a Level String, with OI Age Check",
shorttitle = "GEX",
overlay = true,
max_lines_count = 500,
max_labels_count = 500)
//──────────────────────────────────────────────────────────────────────────────
// Draws gamma-exposure levels that were computed OFF the chart from an option
// chain. Pine cannot call an API at runtime, so the levels arrive as one text
// string pasted into the input below. Format (version 1, no whitespace):
//
// v1;asof=20260904T2000Z;spot=5400.00;FLIP=5430.34;CW=5500;PW=5300;L=5500:6.899e8,5300:-2.412e8
//
// asof open-interest snapshot time, UTC, yyyyMMddTHHmmZ
// spot underlying price used for the computation
// FLIP gamma flip (zero crossing of cumulative GEX); "na" when there is no valid crossing
// CW call wall (strike with the largest positive GEX)
// PW put wall (strike with the most negative GEX)
// L top strikes as strike:GEX pairs, sorted by |GEX| descending; strikes
// equal to a wall are not drawn twice
//
// The grammar is split on ';', '=', ',' and ':' only. Unknown keys are ignored.
//
// For US-listed options, open interest is reported once per day, so a level
// string is only as fresh as its OI snapshot. The table states that age and turns red past the
// threshold you set, instead of letting old levels look current.
//
// Object management: everything is deleted and redrawn on the last bar only,
// so the live count is 3 + N lines and 3 + N labels, well under the caps.
//──────────────────────────────────────────────────────────────────────────────
// ── Inputs ───────────────────────────────────────────────────────────────────
grpS = "Level string"
levelStr = input.string("", "Level string (v1 format, see the description)", group = grpS, display = display.none)
staleAfter = input.float(24.0, "Warn when the OI snapshot is older than (hours)", minval = 1.0, group = grpS,
tooltip = "For US-listed options, open interest is reported once per day. Past this age, a newer open-interest update exists than the one the levels were computed from.")
maxExtra = input.int(10, "Extra |GEX| levels to draw", minval = 0, maxval = 20, group = grpS)
showTable = input.bool(true, "Show freshness table", group = grpS)
// ── Parsing helpers ──────────────────────────────────────────────────────────
// str.tonumber() is not documented to accept exponent notation, so the
// mantissa and the exponent are parsed separately: "6.899e8" -> 6.899 * 10^8.
parseSci(string s) =>
parts = str.split(s, "e")
m = str.tonumber(array.get(parts, 0))
e = array.size(parts) > 1 ? str.tonumber(array.get(parts, 1)) : 0.0
na(m) ? float(na) : m * math.pow(10, na(e) ? 0.0 : e)
// "20260904T2000Z" -> UNIX ms in UTC. Anything of the wrong length is na.
parseAsOf(string s) =>
if str.length(s) != 14
int(na)
else
y = int(str.tonumber(str.substring(s, 0, 4)))
mo = int(str.tonumber(str.substring(s, 4, 6)))
d = int(str.tonumber(str.substring(s, 6, 8)))
h = int(str.tonumber(str.substring(s, 9, 11)))
mi = int(str.tonumber(str.substring(s, 11, 13)))
timestamp("UTC", y, mo, d, h, mi, 0)
// ── State ────────────────────────────────────────────────────────────────────
var float flipLv = na
var float callLv = na
var float putLv = na
var float spotIn = na
var int asOfMs = na
var bool parseOk = false
var float[] xStrike = array.new_float()
var float[] xGex = array.new_float()
var line[] drawn = array.new_line()
var label[] tags = array.new_label()
var table hud = table.new(position.top_right, 1, 4, border_width = 1)
drawLevel(float px, string name, color c, int width) =>
ln = line.new(bar_index - 1, px, bar_index, px, extend = extend.both, color = c, width = width)
lb = label.new(bar_index, px, name + " " + str.tostring(px, format.mintick),
style = label.style_label_left, color = color.new(c, 20), textcolor = color.white, size = size.small)
array.push(drawn, ln)
array.push(tags, lb)
// ── Parse + redraw on the last bar only ──────────────────────────────────────
if barstate.islast
for ln in drawn
line.delete(ln)
for lb in tags
label.delete(lb)
array.clear(drawn)
array.clear(tags)
array.clear(xStrike)
array.clear(xGex)
flipLv := na
callLv := na
putLv := na
spotIn := na
asOfMs := na
parseOk := false
if str.length(levelStr) > 0
fields = str.split(levelStr, ";")
if array.size(fields) > 0 and array.get(fields, 0) == "v1"
parseOk := true
for f in fields
kv = str.split(f, "=")
if array.size(kv) == 2
key = array.get(kv, 0)
val = array.get(kv, 1)
switch key
"asof" => asOfMs := parseAsOf(val)
"spot" => spotIn := str.tonumber(val)
"FLIP" => flipLv := str.tonumber(val) // "na" parses to na, which is the intent
"CW" => callLv := str.tonumber(val)
"PW" => putLv := str.tonumber(val)
"L" =>
for item in str.split(val, ",")
pair = str.split(item, ":")
if array.size(pair) == 2
array.push(xStrike, str.tonumber(array.get(pair, 0)))
array.push(xGex, parseSci(array.get(pair, 1)))
// Extra levels first (behind), headline levels on top. The string is
// already sorted by |GEX| descending, so the index is the rank.
// Guarded: a Pine `for 0 to -1` iterates downward instead of skipping.
nExtra = math.min(array.size(xStrike), maxExtra)
if nExtra > 0
for i = 0 to nExtra - 1
k = array.get(xStrike, i)
g = array.get(xGex, i)
if not na(k) and k != callLv and k != putLv
// Rank 0 is nearly opaque; the last one fades out. |GEX| in
// words would be noise on a chart, so only the sign is coloured.
transp = 40 + int(50 * i / nExtra)
drawLevel(k, (g >= 0 ? "+" : "-") + "GEX", color.new(g >= 0 ? color.teal : color.orange, transp), 1)
if not na(putLv)
drawLevel(putLv, "Put wall", color.red, 2)
if not na(callLv)
drawLevel(callLv, "Call wall", color.green, 2)
if not na(flipLv)
drawLevel(flipLv, "Gamma flip", color.yellow, 2)
// Freshness table. timenow is the chart's wall clock, so the age is
// real even on a stale-looking historical bar.
if showTable
ageH = na(asOfMs) ? float(na) : (timenow - asOfMs) / 3600000.0
stale = na(ageH) or ageH >= staleAfter
bg = not parseOk ? color.new(color.gray, 20) : stale ? color.new(color.red, 10) : color.new(color.green, 40)
status = not parseOk ? "No level string" : na(ageH) ? "asof missing - treat as STALE" :
stale ? "STALE: OI " + str.tostring(ageH, "#.#") + "h old" : "OI " + str.tostring(ageH, "#.#") + "h old"
table.cell(hud, 0, 0, "GEX levels", text_color = color.white, bgcolor = color.new(color.gray, 20), text_size = size.small)
table.cell(hud, 0, 1, status, text_color = color.white, bgcolor = bg, text_size = size.small)
table.cell(hud, 0, 2, na(asOfMs) ? "" : "OI as of " + str.format_time(asOfMs, "yyyy-MM-dd HH:mm", "UTC") + " UTC",
text_color = color.white, bgcolor = color.new(color.gray, 20), text_size = size.small)
table.cell(hud, 0, 3, na(spotIn) ? "" : "computed at spot " + str.tostring(spotIn, format.mintick),
text_color = color.white, bgcolor = color.new(color.gray, 20), text_size = size.small)
// ── Alerts ───────────────────────────────────────────────────────────────────
// alertcondition() must live at global scope. The levels are set on the last
// bar, which is where realtime alerts evaluate, so these fire on live crosses.
// ta.cross() is called unconditionally on every bar (v6 `and` short-circuits,
// which would otherwise break its history); against na it is simply false.
crossFlip = ta.cross(close, flipLv)
crossCall = ta.cross(close, callLv)
crossPut = ta.cross(close, putLv)
alertcondition(crossFlip, "GEX: price crossed gamma flip", "Price crossed the gamma flip level")
alertcondition(crossCall, "GEX: price crossed call wall", "Price crossed the call wall")
alertcondition(crossPut, "GEX: price crossed put wall", "Price crossed the put wall")
Overview
GEX Levels from a Level String, with OI Age Check draws key options positioning levels directly on the price chart, including the gamma flip level, call wall, put wall, and additional high-GEX strike levels. It calculates the elapsed time since the open interest snapshot was recorded and displays a freshness status table on chart. If the data exceeds the configurable maximum age threshold, the table flags the data as stale. Settings allow customizing the stale time limit, number of extra strike levels, and table visibility.
Custom string parsing for external GEX payloads
Plots gamma flip, call wall, and put wall lines
Configurable extra strike levels based on magnitude
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Level string (v1 format, see the description)
string
levelStr
Warn when the OI snapshot is older than (hours)
float
24.0
staleAfter
Extra |GEX| levels to draw
int
10
maxExtra
Show freshness table
bool
true
showTable
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.
This overlay indicator scans a pasted list of symbols for ICT liquidity sweep patterns using prior swing sweeps. The screening method looks back one, two, or three swings and can list long and short signals in a results table with adjustable rows, position, an
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
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
This indicator overlays an intraday SPX and ES chart with a midnight session range, regular-trading-hours opening levels, and a VWAP projection box. It draws the midnight high, mid, and low, flags edge rejections within an absolute tolerance, and plots optiona
The Golden Bullet Strategy is an overlay signal indicator based on Smart Money Concepts and time-based liquidity models. It identifies liquidity sweeps of a designated reference session range during a specified hunt window. It then confirms Market Structure Sh
Indicatorv6SMC / ICT / price actionSessions & time
This indicator plots a session VWAP of the typical price on the price chart. It marks an up triangle under a bar when close crosses above VWAP and a down triangle above a bar when close crosses below it. A star pinned at the top of the pane marks bars whose vo