# Vex: the BackQuant scripting language (complete reference for AI) Vex is BackQuant's language for chart indicators. This file is the whole language: a compact guide first, then the full manual. Every example compiles in the BackQuant terminal. ## Compact guide # Vex 1: chart indicator scripts Vex scripts describe series (one value per bar) with formulas. One statement per line; indented lines continue the line above. // starts a comment. This guide is Vex 1: start a script with the line //@vex=1 (never Pine's //@version). ## Rules - name = formula. Each name is set once; order does not matter. - Names can't reuse a word Vex already has: the bar values, the BackQuant data (basis, oi, iv, gex, funding, rv, ...), the color names (up, down, red, ...) and the function names below. Pine's basis line is a clash: call it mid. - A long formula breaks onto indented lines, one choice per line: ma = if average == "ema" then ema(close, length) else if average == "wma" then wma(close, length) else sma(close, length) Keep lines under about 100 characters (a pasted script with a very long line can break). - At most 16 plots and 24 settings per script. fill(...) takes any two formulas, so a band or gradient step needs no plot of its own. - A top-level name set to a plain value is a SETTING the user can change: a number (length = 14), true/false, a color (col = blue), a price (source = close), or a choice list (average = "ema" or "sma" or "wma"; first is the default). To say the type, a label or limits, use a typed input: length = int(14, "RSI length", min = 2, max = 200), mult = float(2, "Width", step = 0.25), smooth = bool(true, "Smooth"), average = string("ema" or "sma", "Average"), src = source(close, "Price"), col = color(blue, "Line color"); label, min, max and step are optional. These words make inputs only, never conversions (Pine's int(x) is round(x) or floor(x)). Use a choice with if: ma = if average == "ema" then ema(close, length) else if average == "wma" then wma(close, length) else sma(close, length). - Calls use brackets and commas: ema(close, 20). Built-ins also take named values: bands(close, 20, mult = 2.5). - prev(x) is x on the bar before; prev(x, n) is n bars before. Lengths and n must be fixed (a number or a setting). - A value carried over between bars: "stop starts close" then "stop = ... prev(stop) ...". - if c then a else b (else is required; none means no value). c ? a : b is the same: color = f > s ? green : red. - Operators: + - * / %, == != < <= > >=, and or not. Division by zero and missing history give an empty value; maths with an empty value is empty; comparisons with it are false; max/min skip empty values. - closed is true on finished bars, false on the bar still forming: "and closed" stops marks coming and going. - Functions: spread(src, n) = highest(src, n) - lowest(src, n). A longer one indents its lines; the last line is the result. No loops, no recursion (loop_score covers the for-loop score). - Results with several lines use a dot: b = bands(close, 20, 2) then b.upper. ## Output (each call on its own line) - name("title", where = chart|pane): the script's name; where it draws (normally decided: prices on the chart, anything else in its own pane). - plot(value, "title", color = c, width = 1-4, style = line|bars|dots|steps, offset = n). color may be a formula: color = if x > 0 then up else down. offset = n draws it n bars ahead, or back when negative (Pine's offset). - level(70, 30): grey dashed horizontal lines. - fill(a, b, color = c, opacity = 0-100, offset = n): fills between two lines (drawn at its lines' offset unless it gives one). color may be a formula (a ribbon: color = if fast > slow then green else red); opacity defaults to 12. - mark(condition, "text {name}", where = above|below, shape = arrow|dot, color = c, at = price, offset = -n): marks bars where a yes/no is true. at = price makes it a label at that price; {name} puts a value in the text (up to 40 characters); offset = -n draws it n bars back (a pivot on its own bar). - bar_color(c) colors the candles; background(c) colors behind the bars (#rrggbbaa for see-through; none leaves a bar). - alert(condition, "message {name}") alerts on EVERY closed bar where the condition is true; {name} puts a value of the script in the message. So alert on the bar something starts (crosses_above(...), or cond and not prev(cond)), never on a state that holds for many bars (close > mid would alert on nearly every bar). ## Bar values - open: The bar's open. - high: The bar's high. - low: The bar's low. - close: The bar's close (the latest price on the forming bar). - volume: The bar's volume (empty where the chart has none). - time: When the bar opened, in seconds since 1970 (UTC). - bar: The bar number: 0 for the first bar loaded, then 1, 2, ... - hl2: (high + low) / 2 - hlc3: (high + low + close) / 3 - ohlc4: (open + high + low + close) / 4 - tr: True range: the bar's range including any gap from the previous close. - buy_volume: Volume bought by takers in the bar (crypto; empty where the chart has none). - sell_volume: Volume sold by takers in the bar (crypto; empty where the chart has none). - delta: buy_volume - sell_volume: the bar's taker delta. - cvd: Cumulative volume delta: total(delta) from the first bar loaded. - obv: On-balance volume: running volume, added on up closes and taken off on down closes. - vwap: Volume-weighted average price of the session (resets each UTC day), on hlc3. - hour: Hour of the bar's open, 0 to 23 (UTC). - minute: Minute of the bar's open, 0 to 59. - weekday: Day of the week of the bar's open: 1 Monday to 7 Sunday (UTC). - new_day: True on the first bar of each UTC day (and the first bar loaded). - closed: True once the bar is final; false on the bar still forming. Use "and closed" so a mark or alert only fires on finished bars (no repainting). ## BackQuant data (lined up with the chart's bars; empty where the symbol has none) - gex: Net dealer gamma exposure (flow-signed, all expiries), in $ per 1% move. Crypto only. - oi: Open interest, all venues, in $ (BTC, ETH, SOL, HYPE). - funding: Funding rate across venues, in basis points per 8 hours (BTC, ETH, SOL, HYPE). - liq_long: Long positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). - liq_short: Short positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). - iv: Implied volatility: DVOL, in percent (BTC and ETH). - prem_calls: Net call premium in the bar (bought minus sold), in $ (BTC and ETH). - prem_puts: Net put premium in the bar (bought minus sold), in $ (BTC and ETH). - gamma_flip: The gamma flip (HVL): the price nearest spot where cumulative dealer gamma changes sign, from the same dealer book as gex (Flow, all expiries; empty with no crossing within 25%). Crypto only. - call_wall: The call wall: the strike above the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. - put_wall: The put wall: the strike below the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. - etf_flow: Net creations into the US spot ETFs, in $ per day (BTC, ETH, SOL, XRP; daily, lined up with the bars). - rv: Realised volatility, in percent: what price has done, to set against iv (BTC and ETH, daily). - rr25: 30-day 25-delta risk reversal: call IV minus put IV, in vol points (above 0, calls are pricier). Crypto only. - fly25: 30-day 25-delta butterfly: how much the wings cost over ATM, in vol points. Crypto only. - basis: The perp aggregate over the spot aggregate, in basis points (above 0, perps trade rich). Crypto. - cb_premium: Coinbase's USD price over Binance's USDT price, in percent (above 0, US buyers pay up). BTC, ETH, SOL. - put_call: Puts' open interest over calls' (above 1, more puts open). BTC and ETH, daily. - short_share: The share of the day's volume reported as short sales on FINRA venues, in percent. US equities, daily. - dex: Net dealer delta, in $ (flow-signed, all expiries). Crypto only. - vanna: Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, all expiries). Crypto only. - charm: Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, all expiries). Crypto only. - vega: Net dealer vega, in $ per vol point (flow-signed, all expiries). Crypto only. - Each greek by expiry: gex, dex, vanna, charm or vega with _front (nearest expiry), _0dte (nearest while it expires within a day), _week (within 7 days) or _month (within 31 days), e.g. charm_front, gex_0dte. Crypto only. ## Averages - sma(x, n): Simple moving average of x over n bars. - ema(x, n): Exponential moving average of x over n bars (seeded with the SMA). - rma(x, n): Wilder's moving average (the one RSI and ATR use). - wma(x, n): Weighted moving average: the newest bar weighs n, the oldest 1. - dema(x, n): Double EMA: 2 x ema(x, n) - ema(ema(x, n), n), quicker to turn than an EMA. - tema(x, n): Triple EMA: 3 x e1 - 3 x e2 + e3 for an EMA taken one, two and three times. - t3(x, n, vf = 0.7): Tillson T3: six EMAs blended by the volume factor vf (0.7 as usual), smooth with little lag. - alma(x, n, offset = 0.85, sigma = 6): Arnaud Legoux moving average: Gaussian weights centred offset of the way from the oldest bar (0.85 leans to the newest), as wide as n / sigma. - hma(x, n): Hull moving average: smooth and quick to turn. - vwma(x, n): Volume-weighted moving average. ## Windows - highest(x, n): The highest x over the last n bars. - lowest(x, n): The lowest x over the last n bars. - sum(x, n): The total of x over the last n bars. - stdev(x, n): Standard deviation of x over the last n bars (population). - change(x, n = 1): How much x moved over n bars: x - prev(x, n). - roc(x, n): Rate of change: percent move of x over n bars. - total(x): Running total of x from the first bar (empty values add nothing). - median(x, n): The middle value of x over the last n bars. - dev(x, n): Mean absolute deviation of x from its n-bar average. - percentrank(x, n): Percent of the previous n values of x at or below this one (0 to 100). - linreg(x, n): Linear regression: the least-squares line through the last n values of x, at this bar. - correlation(a, b, n): Correlation of a and b over the last n bars (-1 to 1). - loop_score(x, from, to): The for-loop score: for each bar back from `from` to `to`, +1 if x is above it now, -1 if not (or if either is empty). - prev(x, n = 1): x as it was n bars ago (empty before the first bar, unless x has a starts line). ## Indicators - rsi(x, n): Wilder's relative strength index, 0 to 100. - atr(n): Average true range over n bars (Wilder smoothing). - cci(n): Commodity channel index on hlc3. - mfi(n): Money flow index on hlc3 and volume, 0 to 100. - bands(x, n, mult = 2): Bollinger bands: the SMA, and mult standard deviations above and below. Parts: .upper .mid .lower. - macd(x, fast = 12, slow = 26, signal = 9): MACD: fast EMA minus slow EMA, its EMA signal, and the gap between them. Parts: .line .signal .hist. - stoch(n = 14, ksmooth = 1, dsmooth = 3): Stochastic: where the close sits in the n-bar range (0 to 100), smoothed, and its average. Parts: .k .d. - donchian(n = 20): Donchian channel: the highest high and lowest low of the last n bars, and their middle. Parts: .upper .mid .lower. - pivot_high(x, left = 5, right = 5): A pivot high: x of the bar `right` bars back when it is higher than the `left` bars before it and the `right` bars after it; empty elsewhere. It is given on the bar that confirms it (`right` bars later); plot it with offset = -right to draw it on the pivot bar. - pivot_low(x, left = 5, right = 5): A pivot low: x of the bar `right` bars back when it is lower than the `left` bars before it and the `right` bars after it; empty elsewhere. Given on the bar that confirms it. - ichimoku(conversion = 9, base = 26, span_b = 52): Ichimoku: the conversion and base lines (the middle of the n-bar high-low range) and the leading spans as worked out on each bar. Draw the spans 25 bars ahead with offset = 25, and the lagging line as plot(close, offset = -25). Parts: .conversion .base .span_a .span_b. - keltner(x, n = 20, atr_n = 10, mult = 2): Keltner channel: an EMA, and mult ATRs above and below it. Parts: .upper .mid .lower. - williams_r(n = 14): Williams %R: where the close sits in the n-bar range, -100 at the low to 0 at the high. - stochrsi(x, rsi_n = 14, stoch_n = 14, ksmooth = 3, dsmooth = 3): Stochastic RSI: the stochastic of the RSI, smoothed, and its average. Parts: .k .d. - cmf(n = 20): Chaikin money flow: where closes sit in their bars, weighted by volume, over n bars (-1 to 1). - dmi(n = 14, smooth = 14): Directional movement: +DI, -DI and the ADX trend strength. Parts: .plus .minus .adx. - supertrend(n = 10, factor = 3): Supertrend on hl2: a trailing line under the price in an up trend (dir 1), over it in a down trend (dir -1). Parts: .line .dir. - psar(start = 0.02, inc = 0.02, max = 0.2): Parabolic SAR: a stop that accelerates with the trend; dir 1 when it sits under the price. Parts: .value .dir. ## Events - crosses_above(a, b): True on the bar a goes from at or below b to above it. (yes/no) - crosses_below(a, b): True on the bar a goes from at or above b to below it. (yes/no) - rising(x, n): True when x is above every value of the last n bars before it. (yes/no) - falling(x, n): True when x is below every value of the last n bars before it. (yes/no) - count(cond, n): How many of the last n bars cond was true on. - bars_since(cond): Bars since cond was last true (0 on a bar where it is true; empty before it ever was). - when(cond, x, k = 0): x on the most recent bar where cond was true (k = 1 for the one before that, ...). ## Other bars (no lookahead: each bar reads the other bar that had closed by its close) - timeframe("4h", x): x worked out on another timeframe of this chart's market (1m, 2m, 5m, 15m, 30m, 1h, 2h, 4h, 1D, 1W). Each bar reads the value of the last bar of that timeframe that had closed by its own close, so history never repaints; the newest bar follows the one still forming. - symbol("ETHUSDT", x, timeframe = "4h"): x worked out on another market's bars (its own timeframe with timeframe =), lined up with this chart's bars the same way: no lookahead. Another market is drawn in its own pane. ## Maths - max(a, b, ...): The largest of the values (empty values are skipped). - min(a, b, ...): The smallest of the values (empty values are skipped). - abs(x): x without its sign. - sqrt(x): Square root (empty below 0). - log(x): Natural logarithm (empty at 0 and below). - exp(x): e to the power x. - round(x): Nearest whole number. - floor(x): Whole number at or below x. - ceil(x): Whole number at or above x. - sign(x): 1 if x is above 0, -1 below, 0 at 0. - pow(x, p): x to the power p. - clamp(x, lo, hi): x kept between lo and hi. - fallback(x, y): x, or y where x is empty. - empty(x): True where x has no value (not enough history yet, or a division by zero). (yes/no) ## Colors green, red, blue, orange, purple, yellow, teal, pink, grey, white, aqua, lime, fuchsia, maroon, navy, olive, silver, black, up, down, or #rrggbb / #rrggbbaa. ## Translating from Pine Script Vex is not Pine: never write Pine words. The usual translations: - indicator("T", overlay = true) -> name("T", where = chart); //@version is not needed - input.int(14, "Length", minval = 1, maxval = 200) -> length = int(14, "Length", min = 1, max = 200); input.float -> float(...); input.bool -> bool(...); input.string("EMA", options = ["EMA", "SMA"]) -> string("EMA" or "SMA", "...") ; input.source -> source(close, "..."); input.color -> color(...) - x[1], x[n] -> prev(x), prev(x, n); nz(x, y) -> fallback(x, y); na -> none - var s = 0 with s := ... -> s starts 0 then s = if ... then ... else prev(s) - c ? a : b works as it is (or if c then a else b); switch x / 'A' => a / 'B' => b / => c -> an if chain on indented lines: if x == "A" then a else if x == "B" then b else c - ta.x and math.x drop the prefix: ta.stdev -> stdev, math.abs -> abs, math.sum -> sum, math.pow -> pow - A Pine function that keeps its own var state (a KAMA, a trailing stop) is written at the top level, with starts: k starts close then k = prev(k) + sc * (close - prev(k)) - request.security(syminfo.tickerid, "240", x) -> timeframe("4h", x); request.security("ETHUSDT", timeframe.period, close) -> symbol("ETHUSDT", close) - for i = a to b: total += x > x[i] ? 1 : -1 -> loop_score(x, a, b) (no other loops) - ta.ema / ta.sma / ta.rsi / ta.atr -> ema / sma / rsi / atr(n); ta.crossover / ta.crossunder -> crosses_above / crosses_below - plot / hline / fill -> plot(...) / level(...) / fill(...); plotshape -> mark(...); barcolor / bgcolor -> bar_color / background - p1 = plot(a) ... fill(p1, p2) -> fill(a, b) on the values themselves; plot(x, display = display.none) and plots drawn only to be filled -> leave them out (fill needs no plot) - color.new(col, t) as a line color -> #rrggbbaa with alpha 100 - t percent (color.new(#FF019A, 80) -> #FF019A33); in a fill use opacity instead - ta.linreg(x, n, 0) -> linreg(x, n); ta.linreg(x, n, 0) - ta.linreg(x, n, n - 1) -> 2 * (linreg(x, n) - sma(x, n)); ta.percentile_nearest_rank(x, n, 50) -> median(x, n) - fill(p1, p2, color = color.new(col, 80)) -> fill(a, b, color = col, opacity = 20) (opacity = 100 minus Pine's transparency) - ta.pivothigh(high, 5, 5) / ta.pivotlow -> pivot_high(high, 5, 5) / pivot_low (given on the confirming bar, as Pine; plot with offset = -5 to draw on the pivot) - label.new(bar_index[5], ph, str.tostring(ph)) -> mark(not empty(ph), "{ph}", at = ph, offset = -5) - Ichimoku: ich = ichimoku(9, 26, 52), then plot(ich.span_a, offset = 25), plot(ich.span_b, offset = 25), fill(ich.span_a, ich.span_b), plot(close, offset = -25) - alertcondition(cond, title, message) -> alert(cond, "message") when cond is an event; when cond is a state that holds for many bars, alert on its start: alert(cond and not prev(cond), "message") - No: arrays, strings beyond titles, strategies, labels, boxes, lines, tables. ## Example ``` //@vex=1 name("MACD") fast = 12 slow = 26 m = macd(close, fast, slow, 9) plot(m.hist, "Histogram", style = bars, color = if m.hist > 0 then up else down) plot(m.line, "MACD", color = blue) plot(m.signal, "Signal", color = orange) level(0) ``` # Getting started What Vex is, and your first script. Vex is BackQuant's language for chart indicators. You write formulas, then say what to draw; the chart works out the rest: the settings, where it draws, the colors. Scripts run in your browser on the chart's own bars, on every tick, and use BackQuant data (`gex`, `oi`, `funding`, ...) like any price. **A whole indicator** ```vex name("My RSI") length = 14 gain = max(close - prev(close), 0) loss = max(prev(close) - close, 0) rsi = 100 - 100 / (1 + rma(gain, length) / rma(loss, length)) plot(rsi, "RSI", color = blue) level(70, 30) ``` - `name("My RSI")` names the script: the legend and the indicator list show it. - `length = 14` is a plain number at the top, so it is a setting you can change on the chart. - `prev(close)` is the close of the bar before. - `plot(...)` draws a value. RSI isn't a price, so it gets its own pane. - `level(70, 30)` draws two dashed lines. To run it: open the Script Editor (the SCRIPT command, or the code button on a chart's toolbar), write the script, and tick a chart under Add to chart. Each chart runs it with its own settings. Your scripts are also listed under My scripts in every chart's Indicators dialog. See also: [Settings](/documentation/vex/settings), [Drawing](/documentation/vex/drawing), [Coming from Pine Script](/documentation/vex/coming-from-pine-script), [Writing scripts with an AI](/documentation/vex/writing-scripts-with-an-ai) # Lines and comments One statement per line; indent to carry on. - One statement per line. - A line that starts at the left edge starts a new statement; an indented line carries on the one above. - Inside brackets a statement can run over as many lines as it likes. - `//` starts a comment, to the end of the line. ```vex trend = if close > ema(close, 50) then 1 else -1 // indented: the same statement plot(trend, "Trend", style = steps) ``` See also: [Names and formulas](/documentation/vex/names-and-formulas), [The version line](/documentation/vex/the-version-line) # The version line //@vex=1 on the first line says which Vex a script is written for. A script may start with `//@vex=1`. New scripts start with it, and the editor also records the version with every saved script and share link, so you never have to write it. If a later Vex ever changes what something means, a script keeps the meaning it was written for. Pine's `//@version=5` line is not needed; the editor points it out and the script still runs. ```vex //@vex=1 name("EMA") plot(ema(close, 21)) ``` # Names and formulas name = formula; operators; each name set once, in any order. A name holds one value per bar: `spread = high - low`. Every name is a series, so `spread` has a value on every bar of the chart. | Kind | Operators | | --- | --- | | Maths | `+ - * / %` | | Comparing | `== != < <= > >=` | | Yes/no | `and or not` | | Picking | `if c then a else b`, or `c ? a : b` | - Each name is set once. To change a value over time, use its value on the bar before: `prev(name)`. - Order does not matter: a name may be used above the line that sets it. - A value may share its name with a built-in function: `rsi = rsi(close, 14)` is fine (`rsi(...)` is still the function). - Names are letters, digits and `_`, starting with a letter. ```vex body = abs(close - open) range = high - low plot(range > 0 ? body / range : none, "Body share") ``` See also: [Picking a value: if and ? :](/documentation/vex/picking-a-value-if-and), [Looking back: prev](/documentation/vex/looking-back-prev), [Empty values](/documentation/vex/empty-values) # Settings A plain value at the top is a setting; int(), float() and friends add a label and limits. Any top-level name set to a plain value becomes a setting in the chart's settings dialog, labelled from its name (`fast_length` shows as "Fast length"). There is no input keyword. | You write | The setting is | | --- | --- | | `length = 14` | a whole number | | `mult = 2.5` | a decimal | | `source = close` | a price dropdown: close, open, high, low, hl2, hlc3, ohlc4 | | `smooth = true` | a checkbox | | `average = "ema" or "sma"` | a dropdown; the first is the default | | `col = blue` | a color picker | To give the type, a label or limits, write a typed input instead, still alone at the top. The label, `min`, `max` and `step` are all optional; `min`, `max` and `step` are for `int` and `float`. | You write | The setting is | | --- | --- | | `length = int(14, "RSI length", min = 2, max = 200)` | a whole number: the dialog keeps it whole | | `mult = float(2, "Band width", min = 0.5, step = 0.25)` | a decimal, even starting at a whole number | | `smooth = bool(true, "Smooth it")` | a checkbox | | `average = string("ema" or "sma", "Average")` | a dropdown of text (always text, even "high" or "low") | | `src = source(close, "Price")` | a price dropdown (`source("close" or "hl2")` offers just those) | | `col = color(blue, "Line color")` | a color picker | ```vex length = int(20, "Length", min = 2, max = 200) mult = float(2, "Width", min = 0.5, max = 5, step = 0.25) src = source(close, "Price") average = string("ema" or "sma", "Average") b = bands(src, length, mult) plot(b.upper, "Upper") plot(average == "ema" ? ema(src, length) : sma(src, length), "Average") plot(b.lower, "Lower") ``` - A number used as a length or a `prev` count is kept between 1 and 5000 automatically. - A saved value outside the limits (from an older version of the script) is used at the nearest limit. - These words make settings only. Inside a formula, Pine's `int(x)` is `round(x)`, `floor(x)` or `ceil(x)`. - Every plotted line also gets color and width settings on the Style tab, and every fill an opacity. See also: [int](/documentation/vex/reference/settings#int), [float](/documentation/vex/reference/settings#float), [string](/documentation/vex/reference/settings#string), [source](/documentation/vex/reference/settings#source), [color](/documentation/vex/reference/settings#color) # Picking a value: if and ? : if c then a else b, or c ? a : b, picks a value per bar. `if c then a else b` gives `a` on bars where `c` is true and `b` elsewhere. `c ? a : b` is the same thing, shorter. `else` is required; use `none` for "no value". ```vex fast = ema(close, 9) slow = ema(close, 21) trend = fast > slow ? 1 : -1 plot(fast, color = fast > slow ? green : red) plot(slow, color = if fast > slow then green else red) plot(trend, "Trend", style = steps) ``` - `? :` binds loosest: `a + b > c ? x : y` picks on the whole comparison. - They chain: `a ? 1 : b ? 2 : 3`, or `if a then 1 else if b then 2 else 3`. - Both sides must be the same kind of value (two numbers, two colors, two yes/nos). - With a setting as the condition, `if` can pick between texts and whole formulas: `average == "ema" ? ema(close, n) : sma(close, n)`. See also: [if](/documentation/vex/reference/formulas#if), [Colors](/documentation/vex/colors), [none](/documentation/vex/reference/formulas#none) # Looking back: prev prev(x) is x on the bar before; prev(x, n) is n bars back. - `prev(x)` is `x` one bar ago, `prev(x, 5)` five bars ago (Pine's `x[1]` and `x[5]`). - The count must be fixed: a number or a setting, not something that changes from bar to bar. - Before there is an earlier bar the value is empty; `fallback(prev(x), y)` fills it. ```vex rose = close > prev(close) momentum = close - prev(close, 10) plot(momentum, "Momentum") mark(rose and prev(rose) and prev(rose, 2), "3", where = below) ``` See also: [prev](/documentation/vex/reference/windows#prev), [Values that carry over: starts](/documentation/vex/values-that-carry-over-starts), [fallback](/documentation/vex/reference/maths#fallback) # Values that carry over: starts A name reading its own prev, with a starts line for the first bar. A value that builds up from bar to bar (a trailing stop, a counter, a state) reads its own `prev`. A `starts` line says what `prev` gives before there is an earlier bar. This is Pine's `var`. **A trailing stop** ```vex name("ATR stop", where = chart) stop starts close stop = if close > prev(stop) then max(prev(stop), close - 2 * atr(14)) else min(prev(stop), close + 2 * atr(14)) plot(stop, "Stop", style = steps) ``` **A yes/no state** ```vex name("Long or flat", where = pane) long starts false long = if crosses_above(close, ema(close, 50)) then true else if crosses_below(close, ema(close, 50)) then false else prev(long) plot(long ? 1 : 0, "Long", style = steps) ``` Lines may read each other's `prev` in any order, so counters that reset each other work: ```vex name("Run counters", where = pane) rises starts 0 falls starts 0 rises_next = if close > open then prev(rises) + 1 else prev(rises) falls_next = if close < open then prev(falls) + 1 else prev(falls) rises = if falls_next > 3 then 0 else rises_next falls = if rises_next > 3 then 0 else falls_next plot(rises, "Rises") plot(falls, "Falls") ``` See also: [starts](/documentation/vex/reference/formulas#starts), [Looking back: prev](/documentation/vex/looking-back-prev) # Your own functions name(a, b) = formula; longer ones indent their lines. ```vex spread(src, n) = highest(src, n) - lowest(src, n) plot(spread(close, 20), "Spread") ``` A longer function indents its lines; the last line is the result: ```vex zscore(src, n) = m = sma(src, n) (src - m) / stdev(src, n) plot(zscore(close, 20), "Z") level(2, 0, -2) ``` - Functions can use other functions, but not themselves. - They take named values like the built-ins: `zscore(n = 50, src = close)`. - Calling one twice with the same inputs costs nothing extra. - There are no loops; `loop_score` covers the common for-loop score, and the window functions (`highest`, `sum`, `count`, ...) cover the rest. See also: [loop_score](/documentation/vex/reference/windows#loop-score) # Results with several parts bands(), macd() and others give several lines: pick one with a dot. ```vex name("Bands", where = chart) b = bands(close, 20, 2) plot(b.upper, "Upper", color = grey) plot(b.mid, "Middle", color = blue) plot(b.lower, "Lower", color = grey) fill(b.upper, b.lower) ``` | Built-in | Parts | | --- | --- | | `bands` | `.upper` `.mid` `.lower` | | `macd` | `.line` `.signal` `.hist` | | `stoch` | `.k` `.d` | | `donchian` | `.upper` `.mid` `.lower` | | `ichimoku` | `.conversion` `.base` `.span_a` `.span_b` | | `keltner` | `.upper` `.mid` `.lower` | | `stochrsi` | `.k` `.d` | | `dmi` | `.plus` `.minus` `.adx` | | `supertrend` | `.line` `.dir` | | `psar` | `.value` `.dir` | See also: [bands](/documentation/vex/reference/indicators#bands), [macd](/documentation/vex/reference/indicators#macd), [supertrend](/documentation/vex/reference/indicators#supertrend) # Drawing plot, level, fill, mark, bar_color, background; where a script draws. | Call | Does | | --- | --- | | `plot(value, "title", color = c, width = 1-4, style = line \| bars \| dots \| steps, offset = n)` | Draws a value (`offset` shifts it n bars ahead, or back) | | `level(a, b, ...)` | Grey dashed lines at fixed values | | `fill(a, b, color = c, opacity = 0-100, offset = n)` | Fills between two lines | | `mark(condition, "text {name}", where = above \| below, shape = arrow \| dot, color = c, at = price, offset = -n)` | Marks the bars where a yes/no is true; with `at` a label at a price | | `bar_color(c)` | Colors the candles per bar | | `background(c)` | Colors behind each bar | | `alert(condition, "message {name}")` | Alerts when a bar closes with it true | | `name("title", where = chart \| pane)` | Names the script; where it draws | Where it draws, unless `name(..., where = ...)` says: if everything plotted is a price (moving averages, highs and lows, bands, `close` plus or minus a price distance) it draws on the chart; anything else (RSI, volume, ATR, MACD) gets its own pane. A script with only marks or coloring draws on the chart. Each drawing call goes on its own line. A plot's defaults: a line in the next palette color, width 1, titled with the formula as written. See also: [plot](/documentation/vex/reference/drawing#plot), [fill](/documentation/vex/reference/drawing#fill), [mark](/documentation/vex/reference/drawing#mark), [Colors](/documentation/vex/colors) # Colors Names, #hex, up and down, and colors that change per bar. - Names: green, red, blue, orange, purple, yellow, teal, pink, grey, white, aqua, lime, fuchsia, maroon, navy, olive, silver, black. - `#rrggbb`, or `#rrggbbaa` with transparency (`#26a69a40` is a quarter solid). - `up` and `down` are your candle colors, so they follow your theme. - A color may be a formula, picking one per bar: `color = x > 0 ? up : down`. - Every plotted color is also a setting on the Style tab. ```vex name("Colored", where = chart) fast = ema(close, 9) slow = ema(close, 21) plot(fast, color = fast > slow ? #00ff00 : #ff0000) plot(slow, color = fast > slow ? #00ff00 : #ff0000) fill(fast, slow, color = fast > slow ? #00ff00 : #ff0000, opacity = 30) bar_color(fast > slow ? up : down) ``` See also: [fill](/documentation/vex/reference/drawing#fill), [bar_color](/documentation/vex/reference/drawing#bar-color), [background](/documentation/vex/reference/drawing#background) # Marks, finished bars and alerts mark() and alert() on yes/no conditions; "and closed" for finished bars. ```vex name("Crossovers", where = chart) fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) mark(crosses_above(fast, slow) and closed, "B", where = below) mark(crosses_below(fast, slow) and closed, "S", where = above, color = orange) alert(crosses_above(fast, slow), "Fast crossed above slow at {close}") ``` A mark can also be a label at a price, with values in its text: **Swing highs and lows, labelled with their price** ```vex name("Pivot labels", where = chart) ph = pivot_high(high, 8, 8) pl = pivot_low(low, 8, 8) mark(not empty(ph), "{ph}", at = ph, shape = dot, color = orange, offset = -8) mark(not empty(pl), "{pl}", at = pl, where = below, shape = dot, color = teal, offset = -8) ``` The newest bar is still forming: its close moves with every trade, so a condition on it can be true one moment and false the next. `closed` is true on finished bars and false on the forming one. Add `and closed` so a mark never appears and then vanishes. An alert fires once, when a bar closes with its condition true (never for the history loaded with the chart, never in bar replay), while a chart running the script is open. It follows your alert settings: quiet hours, desktop notifications and sound. `{name}` puts a value of the script in the message. The editor's Alerts tab lists what fired. See also: [mark](/documentation/vex/reference/drawing#mark), [alert](/documentation/vex/reference/drawing#alert), [closed](/documentation/vex/reference/bar-values#closed) # Empty values Not enough history, a division by zero, or none. A value is empty before there is enough history (a 20-bar average on bar 5), after a division by zero, or where it was set to `none` (Pine's `na`). - Maths with an empty value stays empty. - A comparison with one is false. - `max` and `min` skip empty values, so `max(prev(stop), x)` works from the first bar. - `empty(x)` tests for it; `fallback(x, y)` swaps it for `y` (Pine's `na(x)` and `nz(x, y)`). - An empty value draws nothing: a plot has a gap there. ```vex r = rsi(close, 14) plot(r > 70 ? r : none, "Overbought only", style = dots) plot(fallback(prev(close), close), "Previous close") ``` See also: [empty](/documentation/vex/reference/maths#empty), [fallback](/documentation/vex/reference/maths#fallback), [none](/documentation/vex/reference/formulas#none) # BackQuant data gex, vanna, charm, oi, funding, liquidations, iv and premium as series. BackQuant's data are names like `close`: one value per bar, lined up with the chart exactly as its own pane lines it up (the value known at each bar's close, never one from the future). Where a symbol has no such data the values are empty and the legend says so. Options data (gex, the gamma levels, iv, the premiums, rr25, fly25) is crypto only: on a stock chart it is empty; the stock's own bars, volume and short_share are there. | Name | What | | --- | --- | | `gex` | Net dealer gamma exposure (flow-signed, all expiries), in $ per 1% move. Crypto only. | | `oi` | Open interest, all venues, in $ (BTC, ETH, SOL, HYPE). | | `funding` | Funding rate across venues, in basis points per 8 hours (BTC, ETH, SOL, HYPE). | | `liq_long` | Long positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). | | `liq_short` | Short positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). | | `iv` | Implied volatility: DVOL, in percent (BTC and ETH). | | `prem_calls` | Net call premium in the bar (bought minus sold), in $ (BTC and ETH). | | `prem_puts` | Net put premium in the bar (bought minus sold), in $ (BTC and ETH). | | `gamma_flip` | The gamma flip (HVL): the price nearest spot where cumulative dealer gamma changes sign, from the same dealer book as gex (Flow, all expiries; empty with no crossing within 25%). Crypto only. | | `call_wall` | The call wall: the strike above the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. | | `put_wall` | The put wall: the strike below the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. | | `etf_flow` | Net creations into the US spot ETFs, in $ per day (BTC, ETH, SOL, XRP; daily, lined up with the bars). | | `rv` | Realised volatility, in percent: what price has done, to set against iv (BTC and ETH, daily). | | `rr25` | 30-day 25-delta risk reversal: call IV minus put IV, in vol points (above 0, calls are pricier). Crypto only. | | `fly25` | 30-day 25-delta butterfly: how much the wings cost over ATM, in vol points. Crypto only. | | `basis` | The perp aggregate over the spot aggregate, in basis points (above 0, perps trade rich). Crypto. | | `cb_premium` | Coinbase's USD price over Binance's USDT price, in percent (above 0, US buyers pay up). BTC, ETH, SOL. | | `put_call` | Puts' open interest over calls' (above 1, more puts open). BTC and ETH, daily. | | `short_share` | The share of the day's volume reported as short sales on FINRA venues, in percent. US equities, daily. | | `dex` | Net dealer delta, in $ (flow-signed, all expiries). Crypto only. | | `vanna` | Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, all expiries). Crypto only. | | `charm` | Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, all expiries). Crypto only. | | `vega` | Net dealer vega, in $ per vol point (flow-signed, all expiries). Crypto only. | The greeks `gex`, `dex`, `vanna`, `charm` and `vega` (all expiries) each come by expiry too: add `_front` (the nearest expiry), `_0dte` (the nearest while it expires within a day), `_week` (expiries within 7 days) or `_month` (within 31 days), as in `charm_front` or `gex_0dte`. Crypto only. These are reserved names: a script that used `dex`, `vanna`, `charm` or `vega` for its own value needs another name (`my_vega`). ```vex name("Charm into expiry") c = charm_front / 1e6 plot(c, "Front charm ($m delta per day)", style = bars, color = if c > 0 then up else down) plot(vanna / 1e6, "Vanna, all expiries ($m)", color = orange) level(0) ``` ```vex name("Dealer gamma") g = gex / 1e9 plot(g, "Net GEX ($bn)", style = bars, color = g > 0 ? up : down) plot(ema(g, 20), "Average", color = orange) level(0) ``` See also: [gex](/documentation/vex/reference/backquant-data#gex), [oi](/documentation/vex/reference/backquant-data#oi), [funding](/documentation/vex/reference/backquant-data#funding), [liq_long](/documentation/vex/reference/backquant-data#liq-long), [liq_short](/documentation/vex/reference/backquant-data#liq-short), [iv](/documentation/vex/reference/backquant-data#iv), [prem_calls](/documentation/vex/reference/backquant-data#prem-calls), [prem_puts](/documentation/vex/reference/backquant-data#prem-puts), [gamma_flip](/documentation/vex/reference/backquant-data#gamma-flip), [call_wall](/documentation/vex/reference/backquant-data#call-wall), [put_wall](/documentation/vex/reference/backquant-data#put-wall), [etf_flow](/documentation/vex/reference/backquant-data#etf-flow), [rv](/documentation/vex/reference/backquant-data#rv), [rr25](/documentation/vex/reference/backquant-data#rr25), [fly25](/documentation/vex/reference/backquant-data#fly25), [basis](/documentation/vex/reference/backquant-data#basis), [cb_premium](/documentation/vex/reference/backquant-data#cb-premium), [put_call](/documentation/vex/reference/backquant-data#put-call), [short_share](/documentation/vex/reference/backquant-data#short-share), [dex](/documentation/vex/reference/backquant-data#dex), [vanna](/documentation/vex/reference/backquant-data#vanna), [charm](/documentation/vex/reference/backquant-data#charm), [vega](/documentation/vex/reference/backquant-data#vega) # Other timeframes and markets timeframe("4h", x) and symbol("ETHUSDT", x): a formula on other bars. `timeframe("4h", x)` works `x` out on the 4h bars of the chart's market; `symbol("ETHUSDT", x)` on another market's bars, at the chart's timeframe or its own with `timeframe = "4h"`. The names of your script used inside are worked out on those bars too. **A 4h average and the daily close on any chart** ```vex name("Higher timeframe", where = chart) fast = ema(close, 20) plot(fast, "EMA 20") plot(timeframe("4h", fast), "EMA 20 on 4h", color = orange, style = steps) plot(timeframe("1D", close), "Daily close", color = blue, style = steps) ``` **Another market** ```vex name("BTC in ETH") eth = symbol("ETHUSDT", close) plot(close / eth, "BTC / ETH") ``` - Each bar reads the value of the last other bar that had closed by its own close. A 1h bar at 13:00 sees the 4h bar that closed at 12:00, never the one closing at 16:00: history shows what was known then and nothing repaints. - The newest bar reads the other bar still forming, so it moves live (on this chart's own market, with every trade). - Where the other market has no bar (a closed stock market on a crypto chart) the value is empty: a gap, not an old value. - Timeframes: 1m, 2m, 5m, 15m, 30m, 1h, 2h, 4h, 1D, 1W. Up to 4 per script. - Not yet: BackQuant data on other bars, or these calls inside your own functions. See also: [timeframe](/documentation/vex/reference/other-bars#timeframe), [symbol](/documentation/vex/reference/other-bars#symbol), [Coming from Pine Script](/documentation/vex/coming-from-pine-script) # Coming from Pine Script The usual Pine words and what Vex writes instead. Vex is not Pine, but most indicators translate line by line. Search this manual for a Pine name (`ta.crossover`, `nz`, `hline`) to find the Vex one. | Pine | Vex | | --- | --- | | `//@version=5` | `//@vex=1` (optional) | | `indicator("T", overlay = true)` | `name("T", where = chart)` | | `input.int(14, "Length", minval = 1)` | `length = int(14, "Length", min = 1)` | | `input.float`, `input.bool`, `input.source`, `input.color` | `float(...)`, `bool(...)`, `source(close, ...)`, `color(...)` | | `input.string("EMA", options = ["EMA", "SMA"])` | `average = string("EMA" or "SMA", "Average")` | | `x[1]`, `x[n]` | `prev(x)`, `prev(x, n)` | | `var s = 0` then `s := ...` | `s starts 0` then `s = ... prev(s) ...` | | `c ? a : b` | `c ? a : b` (or `if c then a else b`) | | `na`, `na(x)`, `nz(x, y)` | `none`, `empty(x)`, `fallback(x, y)` | | `ta.ema`, `ta.rsi`, `ta.atr`, ... | `ema`, `rsi`, `atr`, ... | | `ta.crossover`, `ta.crossunder` | `crosses_above`, `crosses_below` | | `plot`, `hline`, `fill`, `plotshape` | `plot`, `level`, `fill`, `mark` | | `barcolor`, `bgcolor` | `bar_color`, `background` | | `color.new(col, 80)` on a fill | `fill(a, b, color = col, opacity = 20)` | | `color.lime`, `color.aqua`, ... | `lime`, `aqua`, ... | | `alertcondition(c, title, msg)` | `alert(c, "message")` | | `for i = 1 to n: score += x > x[i] ? 1 : -1` | `loop_score(x, 1, n)` | | `barstate.isconfirmed` | `closed` | | `request.security(syminfo.tickerid, "240", x)` | `timeframe("4h", x)` | | `request.security("ETHUSDT", timeframe.period, close)` | `symbol("ETHUSDT", close)` | Not in Vex (yet): loops in general, tables and strategies. The editor's robot button copies a prompt that translates a Pine script for you. See also: [Writing scripts with an AI](/documentation/vex/writing-scripts-with-an-ai), [Values that carry over: starts](/documentation/vex/values-that-carry-over-starts), [Settings](/documentation/vex/settings) # Writing scripts with an AI The AI prompt button copies a prompt that teaches any AI the language. The Script Editor's AI prompt button (the robot; in the ... menu on a narrow editor) copies a prompt that contains this whole language guide. Paste it into Claude, ChatGPT or any other AI and describe what you want at the end. Three kinds: - Write a new script: describe the indicator. - Change this script: it includes your script and its current problems. - Translate from Pine: paste the Pine script at the end. Paste the answer into the editor. If anything is wrong, the editor says what and where; copy the Change prompt and the AI gets the problems with the script. See also: [Coming from Pine Script](/documentation/vex/coming-from-pine-script), [Errors and the value inspector](/documentation/vex/errors-and-the-value-inspector) # Errors and the value inspector What the editor tells you, and how to see every value. - Errors say what is wrong and how to fix it, at the exact place: "clsoe" isn't a name. Did you mean close? - Words from other languages (`input`, `var`, `hline`, `bgcolor`, `[1]`, `&&`) get a message saying what Vex uses instead. - Warnings catch what runs but is probably a mistake: a name never used, nothing drawn. - One broken line never causes errors elsewhere, and while you type the chart keeps the last version that worked. - The Values tab lists every named value at the bar under the cursor (hover the chart to pick a bar), and which names are empty on every bar: the usual reason a line doesn't show. - Press F1 on a word in the editor to open its page here. | Editor tool | How | | --- | --- | | Format code: one space around operators, none inside brackets | `Shift+Alt+F`, or More, Format code | | Rename a name everywhere (its uses, `{name}` in texts, your functions' inputs) | `F2` with the cursor on it | | Version history: earlier versions kept in this browser, restore any | More, Version history | | This reference, on the word at the cursor | `F1` | See also: [Empty values](/documentation/vex/empty-values), [Limits](/documentation/vex/limits) # Limits How big a script can be. | Limit | Value | | --- | --- | | Script size | 32 KB | | Calculations per bar | 2,000 | | Looking back, window length | 5,000 bars | | Lines plotted | 16 | | Settings | 24 | There are no loops and no recursion, so every script finishes in a fixed number of steps per bar: a script can never freeze the chart. A live tick recomputes only the forming bar. # Vex reference: Settings | Name | What | | --- | --- | | [`int`](#int) | A whole-number setting with its own label and limits (at the top of the script). | | [`float`](#float) | A decimal setting with its own label and limits. | | [`bool`](#bool) | A checkbox setting. | | [`string`](#string) | A dropdown of choices; the first is the default. | | [`source`](#source) | A price setting (close, open, high, low, hl2, hlc3, ohlc4). | | [`color`](#color) | A color setting. | ## int ```vex int(14, "Label", min =, max =, step =) ``` A whole-number setting with its own label and limits (at the top of the script). **In Pine Script:** `input.int(defval, title, minval, maxval, step)` **Example** ```vex length = int(14, "RSI length", min = 2, max = 200) plot(rsi(close, length)) ``` ## float ```vex float(2, "Label", min =, max =, step =) ``` A decimal setting with its own label and limits. **In Pine Script:** `input.float(defval, title, minval, maxval, step)` **Example** ```vex mult = float(2, "Width", min = 0.5, step = 0.25) b = bands(close, 20, mult) plot(b.upper) plot(b.lower) ``` ## bool ```vex bool(true, "Label") ``` A checkbox setting. **In Pine Script:** `input.bool(defval, title)` **Example** ```vex smooth = bool(true, "Smooth it") plot(smooth ? ema(close, 9) : close) ``` ## string ```vex string("a" or "b", "Label") ``` A dropdown of choices; the first is the default. **In Pine Script:** `input.string(defval, title, options)` **Example** ```vex average = string("ema" or "sma", "Average") plot(average == "ema" ? ema(close, 20) : sma(close, 20)) ``` ## source ```vex source(close, "Label") ``` A price setting (close, open, high, low, hl2, hlc3, ohlc4). **In Pine Script:** `input.source(defval, title)` **Example** ```vex src = source(close, "Price") plot(ema(src, 20)) ``` ## color ```vex color(blue, "Label") ``` A color setting. **In Pine Script:** `input.color(defval, title)` **Example** ```vex col = color(blue, "Line color") plot(ema(close, 20), color = col) ``` # Vex reference: Drawing | Name | What | | --- | --- | | [`name`](#name) | Names the script (the legend and menus show it). where = chooses the price chart or its own pane; without it, Vex decides. | | [`plot`](#plot) | Draws a value. The color may be a formula, picking one per bar. offset = n draws it n bars ahead (past the newest bar) or back (negative). | | [`level`](#level) | Grey dashed horizontal lines at fixed values (numbers or settings). | | [`fill`](#fill) | Fills between two lines: up color where a is above b, down below, unless a color is given. The color may be a formula, one per bar (a ribbon). opacity is how solid it is (default 12); a #rrggbbaa color keeps its own. | | [`mark`](#mark) | Marks the bars where a yes/no is true. Add "and closed" to mark finished bars only. at = price puts it at a price (a label); {name} in the text shows a value; offset = -n draws it n bars back. | | [`alert`](#alert) | Alerts when a bar closes with the condition true, while a chart running the script is open (your alert settings decide sound and notifications). {name} puts a value in the message. | | [`bar_color`](#bar-color) | Colors the chart's candles per bar (none leaves a bar as it is). | | [`background`](#background) | Colors behind each bar; use a see-through color (#rrggbbaa). | ## name ```vex name("title", where = chart|pane) ``` Names the script (the legend and menus show it). where = chooses the price chart or its own pane; without it, Vex decides. **In Pine Script:** `indicator(title, overlay = true)` **Example** ```vex name("EMA 21", where = chart) plot(ema(close, 21)) ``` ## plot ```vex plot(value, "title", color = c, width = 1-4, style = line|bars|dots|steps, offset = n) ``` Draws a value. The color may be a formula, picking one per bar. offset = n draws it n bars ahead (past the newest bar) or back (negative). **In Pine Script:** `plot(series, title, color, linewidth, style, offset)` **Example** ```vex plot(rsi(close, 14), "RSI", color = blue, width = 2) plot(volume, "Volume", style = bars, color = close > open ? up : down) ``` See also: [Drawing](/documentation/vex/drawing), [Colors](/documentation/vex/colors) ## level ```vex level(a, b, ...) ``` Grey dashed horizontal lines at fixed values (numbers or settings). **In Pine Script:** `hline(price)` **Example** ```vex plot(rsi(close, 14)) level(70, 50, 30) ``` ## fill ```vex fill(a, b, color = c, opacity = 0-100, offset = n) ``` Fills between two lines: up color where a is above b, down below, unless a color is given. The color may be a formula, one per bar (a ribbon). opacity is how solid it is (default 12); a #rrggbbaa color keeps its own. With a color formula the fill is a ribbon: each stretch takes the color of the bar it starts from, as the lines do. opacity sets how solid it is, 0 to 100 (default 12); it is also a setting on the Style tab. A #rrggbbaa color keeps its own transparency. **In Pine Script:** `fill(plot1, plot2, color)` **Example** ```vex name("Ribbon", where = chart) fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) fill(fast, slow, color = fast > slow ? green : red, opacity = 35) ``` See also: [Colors](/documentation/vex/colors) ## mark ```vex mark(condition, "text {name}", where = above|below, shape = arrow|dot, color = c, at = price, offset = -n) ``` Marks the bars where a yes/no is true. Add "and closed" to mark finished bars only. at = price puts it at a price (a label); {name} in the text shows a value; offset = -n draws it n bars back. `at = price` puts the mark at that price: a label. `{name}` in its text shows the value on that bar, as alert messages do (up to 40 characters). `offset = -n` draws it n bars back, so a pivot found `right` bars later is labelled on its own bar. **In Pine Script:** `plotshape(condition, text, location, style, color); label.new(x, y, text)` **Example** ```vex fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) mark(crosses_above(fast, slow) and closed, "B", where = below) mark(crosses_below(fast, slow) and closed, "S", where = above, shape = dot, color = orange) ``` See also: [Marks, finished bars and alerts](/documentation/vex/marks-finished-bars-and-alerts) ## alert ```vex alert(condition, "message {name}") ``` Alerts when a bar closes with the condition true, while a chart running the script is open (your alert settings decide sound and notifications). {name} puts a value in the message. **In Pine Script:** `alertcondition(condition, title, message)` **Example** ```vex fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) alert(crosses_above(fast, slow), "Fast crossed above slow at {close}") ``` See also: [Marks, finished bars and alerts](/documentation/vex/marks-finished-bars-and-alerts) ## bar_color ```vex bar_color(color) ``` Colors the chart's candles per bar (none leaves a bar as it is). **In Pine Script:** `barcolor(color)` **Example** ```vex bar_color(close > ema(close, 50) ? up : down) ``` ## background ```vex background(color) ``` Colors behind each bar; use a see-through color (#rrggbbaa). **In Pine Script:** `bgcolor(color)` **Example** ```vex background(rsi(close, 14) > 70 ? #ef444420 : none) ``` # Vex reference: Formulas | Name | What | | --- | --- | | [`if`](#if) | Picks a value per bar. condition ? a : b is the same. | | [`starts`](#starts) | What prev(name) reads before there is an earlier bar. | | [`none`](#none) | No value (an empty bar, no color). | | [`and`](#and) | Both are true. | | [`or`](#or) | Either is true. Between texts at the top level, a list of choices for a setting. | | [`not`](#not) | The opposite yes/no. | ## if ```vex if condition then a else b ``` Picks a value per bar. condition ? a : b is the same. **In Pine Script:** `condition ? a : b` **Example** ```vex plot(close > open ? high : low) plot(if close > open then 1 else -1, "Up or down", style = steps) ``` See also: [Picking a value: if and ? :](/documentation/vex/picking-a-value-if-and) ## starts ```vex name starts value ``` What prev(name) reads before there is an earlier bar. **In Pine Script:** `var x = value` **Example** ```vex n starts 0 n = new_day ? 0 : prev(n) + 1 plot(n, "Bars into the day", style = steps) ``` See also: [Values that carry over: starts](/documentation/vex/values-that-carry-over-starts) ## none No value (an empty bar, no color). **In Pine Script:** `na` **Example** ```vex plot(close > open ? close : none, style = dots) ``` See also: [Empty values](/documentation/vex/empty-values) ## and ```vex a and b ``` Both are true. **Example** ```vex plot(close) mark(close > open and volume > sma(volume, 20), where = below, shape = dot) ``` ## or ```vex a or b ``` Either is true. Between texts at the top level, a list of choices for a setting. **Example** ```vex plot(close) mark(new_day or crosses_above(close, ema(close, 50)), where = below, shape = dot) ``` ## not ```vex not a ``` The opposite yes/no. **Example** ```vex plot(close) mark(not (close > open), where = above, shape = dot) ``` # Vex reference: Bar values | Name | What | | --- | --- | | [`open`](#open) | The bar's open. | | [`high`](#high) | The bar's high. | | [`low`](#low) | The bar's low. | | [`close`](#close) | The bar's close (the latest price on the forming bar). | | [`volume`](#volume) | The bar's volume (empty where the chart has none). | | [`time`](#time) | When the bar opened, in seconds since 1970 (UTC). | | [`bar`](#bar) | The bar number: 0 for the first bar loaded, then 1, 2, ... | | [`hl2`](#hl2) | (high + low) / 2 | | [`hlc3`](#hlc3) | (high + low + close) / 3 | | [`ohlc4`](#ohlc4) | (open + high + low + close) / 4 | | [`tr`](#tr) | True range: the bar's range including any gap from the previous close. | | [`buy_volume`](#buy-volume) | Volume bought by takers in the bar (crypto; empty where the chart has none). | | [`sell_volume`](#sell-volume) | Volume sold by takers in the bar (crypto; empty where the chart has none). | | [`delta`](#delta) | buy_volume - sell_volume: the bar's taker delta. | | [`cvd`](#cvd) | Cumulative volume delta: total(delta) from the first bar loaded. | | [`obv`](#obv) | On-balance volume: running volume, added on up closes and taken off on down closes. | | [`vwap`](#vwap) | Volume-weighted average price of the session (resets each UTC day), on hlc3. | | [`hour`](#hour) | Hour of the bar's open, 0 to 23 (UTC). | | [`minute`](#minute) | Minute of the bar's open, 0 to 59. | | [`weekday`](#weekday) | Day of the week of the bar's open: 1 Monday to 7 Sunday (UTC). | | [`new_day`](#new-day) | True on the first bar of each UTC day (and the first bar loaded). | | [`closed`](#closed) | True once the bar is final; false on the bar still forming. Use "and closed" so a mark or alert only fires on finished bars (no repainting). | ## open The bar's open. **Returns:** a number per bar **Example** ```vex plot(open) ``` ## high The bar's high. **Returns:** a number per bar **Example** ```vex plot(high) ``` ## low The bar's low. **Returns:** a number per bar **Example** ```vex plot(low) ``` ## close The bar's close (the latest price on the forming bar). **Returns:** a number per bar **Example** ```vex plot(close) ``` ## volume The bar's volume (empty where the chart has none). **Returns:** a number per bar **Example** ```vex plot(volume) ``` ## time When the bar opened, in seconds since 1970 (UTC). Pine's time is in milliseconds; Vex's is in seconds. **Returns:** a number per bar **In Pine Script:** `time / 1000` **Example** ```vex plot(time) ``` ## bar The bar number: 0 for the first bar loaded, then 1, 2, ... **Returns:** a number per bar **In Pine Script:** `bar_index` **Example** ```vex plot(bar) ``` ## hl2 (high + low) / 2 **Returns:** a number per bar **Example** ```vex plot(hl2) ``` ## hlc3 (high + low + close) / 3 **Returns:** a number per bar **Example** ```vex plot(hlc3) ``` ## ohlc4 (open + high + low + close) / 4 **Returns:** a number per bar **Example** ```vex plot(ohlc4) ``` ## tr True range: the bar's range including any gap from the previous close. **Returns:** a number per bar **In Pine Script:** `ta.tr` **Example** ```vex plot(tr) ``` ## buy_volume Volume bought by takers in the bar (crypto; empty where the chart has none). **Returns:** a number per bar **Example** ```vex plot(buy_volume) ``` ## sell_volume Volume sold by takers in the bar (crypto; empty where the chart has none). **Returns:** a number per bar **Example** ```vex plot(sell_volume) ``` ## delta buy_volume - sell_volume: the bar's taker delta. **Returns:** a number per bar **Example** ```vex plot(delta) ``` ## cvd Cumulative volume delta: total(delta) from the first bar loaded. **Returns:** a number per bar **Example** ```vex plot(cvd) ``` ## obv On-balance volume: running volume, added on up closes and taken off on down closes. **Returns:** a number per bar **In Pine Script:** `ta.obv` **Example** ```vex plot(obv) ``` ## vwap Volume-weighted average price of the session (resets each UTC day), on hlc3. **Returns:** a number per bar **In Pine Script:** `ta.vwap` **Example** ```vex plot(vwap) ``` ## hour Hour of the bar's open, 0 to 23 (UTC). **Returns:** a number per bar **In Pine Script:** `hour(time, "UTC")` **Example** ```vex background(hour >= 13 and hour < 21 ? #3b82f615 : none) ``` ## minute Minute of the bar's open, 0 to 59. **Returns:** a number per bar **In Pine Script:** `minute(time, "UTC")` **Example** ```vex plot(minute) ``` ## weekday Day of the week of the bar's open: 1 Monday to 7 Sunday (UTC). Pine counts Sunday as 1; Vex counts Monday as 1 and Sunday as 7. **Returns:** a number per bar **In Pine Script:** `dayofweek` **Example** ```vex plot(weekday) ``` ## new_day True on the first bar of each UTC day (and the first bar loaded). **Returns:** a yes/no per bar **In Pine Script:** `ta.change(time("D")) != 0` **Example** ```vex background(new_day ? #3b82f630 : none) ``` ## closed True once the bar is final; false on the bar still forming. Use "and closed" so a mark or alert only fires on finished bars (no repainting). **Returns:** a yes/no per bar **In Pine Script:** `barstate.isconfirmed` **Example** ```vex fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) mark(crosses_above(fast, slow) and closed, "B", where = below) ``` See also: [Marks, finished bars and alerts](/documentation/vex/marks-finished-bars-and-alerts) # Vex reference: BackQuant data | Name | What | | --- | --- | | [`gex`](#gex) | Net dealer gamma exposure (flow-signed, all expiries), in $ per 1% move. Crypto only. | | [`oi`](#oi) | Open interest, all venues, in $ (BTC, ETH, SOL, HYPE). | | [`funding`](#funding) | Funding rate across venues, in basis points per 8 hours (BTC, ETH, SOL, HYPE). | | [`liq_long`](#liq-long) | Long positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). | | [`liq_short`](#liq-short) | Short positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). | | [`iv`](#iv) | Implied volatility: DVOL, in percent (BTC and ETH). | | [`prem_calls`](#prem-calls) | Net call premium in the bar (bought minus sold), in $ (BTC and ETH). | | [`prem_puts`](#prem-puts) | Net put premium in the bar (bought minus sold), in $ (BTC and ETH). | | [`gamma_flip`](#gamma-flip) | The gamma flip (HVL): the price nearest spot where cumulative dealer gamma changes sign, from the same dealer book as gex (Flow, all expiries; empty with no crossing within 25%). Crypto only. | | [`call_wall`](#call-wall) | The call wall: the strike above the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. | | [`put_wall`](#put-wall) | The put wall: the strike below the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. | | [`etf_flow`](#etf-flow) | Net creations into the US spot ETFs, in $ per day (BTC, ETH, SOL, XRP; daily, lined up with the bars). | | [`rv`](#rv) | Realised volatility, in percent: what price has done, to set against iv (BTC and ETH, daily). | | [`rr25`](#rr25) | 30-day 25-delta risk reversal: call IV minus put IV, in vol points (above 0, calls are pricier). Crypto only. | | [`fly25`](#fly25) | 30-day 25-delta butterfly: how much the wings cost over ATM, in vol points. Crypto only. | | [`basis`](#basis) | The perp aggregate over the spot aggregate, in basis points (above 0, perps trade rich). Crypto. | | [`cb_premium`](#cb-premium) | Coinbase's USD price over Binance's USDT price, in percent (above 0, US buyers pay up). BTC, ETH, SOL. | | [`put_call`](#put-call) | Puts' open interest over calls' (above 1, more puts open). BTC and ETH, daily. | | [`short_share`](#short-share) | The share of the day's volume reported as short sales on FINRA venues, in percent. US equities, daily. | | [`gex_0dte`](#gex-0dte) | Net dealer gamma exposure, in $ per 1% move (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. | | [`gex_front`](#gex-front) | Net dealer gamma exposure, in $ per 1% move (flow-signed, the front (nearest) expiry). Crypto only. | | [`gex_week`](#gex-week) | Net dealer gamma exposure, in $ per 1% move (flow-signed, expiries within 7 days). Crypto only. | | [`gex_month`](#gex-month) | Net dealer gamma exposure, in $ per 1% move (flow-signed, expiries within 31 days). Crypto only. | | [`dex`](#dex) | Net dealer delta, in $ (flow-signed, all expiries). Crypto only. | | [`dex_0dte`](#dex-0dte) | Net dealer delta, in $ (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. | | [`dex_front`](#dex-front) | Net dealer delta, in $ (flow-signed, the front (nearest) expiry). Crypto only. | | [`dex_week`](#dex-week) | Net dealer delta, in $ (flow-signed, expiries within 7 days). Crypto only. | | [`dex_month`](#dex-month) | Net dealer delta, in $ (flow-signed, expiries within 31 days). Crypto only. | | [`vanna`](#vanna) | Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, all expiries). Crypto only. | | [`vanna_0dte`](#vanna-0dte) | Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. | | [`vanna_front`](#vanna-front) | Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, the front (nearest) expiry). Crypto only. | | [`vanna_week`](#vanna-week) | Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, expiries within 7 days). Crypto only. | | [`vanna_month`](#vanna-month) | Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, expiries within 31 days). Crypto only. | | [`charm`](#charm) | Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, all expiries). Crypto only. | | [`charm_0dte`](#charm-0dte) | Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. | | [`charm_front`](#charm-front) | Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, the front (nearest) expiry). Crypto only. | | [`charm_week`](#charm-week) | Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, expiries within 7 days). Crypto only. | | [`charm_month`](#charm-month) | Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, expiries within 31 days). Crypto only. | | [`vega`](#vega) | Net dealer vega, in $ per vol point (flow-signed, all expiries). Crypto only. | | [`vega_0dte`](#vega-0dte) | Net dealer vega, in $ per vol point (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. | | [`vega_front`](#vega-front) | Net dealer vega, in $ per vol point (flow-signed, the front (nearest) expiry). Crypto only. | | [`vega_week`](#vega-week) | Net dealer vega, in $ per vol point (flow-signed, expiries within 7 days). Crypto only. | | [`vega_month`](#vega-month) | Net dealer vega, in $ per vol point (flow-signed, expiries within 31 days). Crypto only. | ## gex Net dealer gamma exposure (flow-signed, all expiries), in $ per 1% move. Crypto only. **Returns:** a number per bar **Example** ```vex g = gex / 1e9 plot(g, "Net GEX ($bn)", style = bars, color = g > 0 ? up : down) level(0) ``` ## oi Open interest, all venues, in $ (BTC, ETH, SOL, HYPE). **Returns:** a number per bar **Example** ```vex plot(oi / 1e9, "OI ($bn)") ``` ## funding Funding rate across venues, in basis points per 8 hours (BTC, ETH, SOL, HYPE). **Returns:** a number per bar **Example** ```vex plot(funding, "Funding (bp / 8h)", style = bars, color = funding > 0 ? up : down) level(0) ``` ## liq_long Long positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). **Returns:** a number per bar **Example** ```vex plot(liq_long / 1e6, "Longs liquidated ($m)", style = bars, color = down) plot(-liq_short / 1e6, "Shorts liquidated ($m)", style = bars, color = up) ``` ## liq_short Short positions liquidated in the bar, in $ (BTC, ETH, SOL, HYPE). **Returns:** a number per bar **Example** ```vex plot(liq_short / 1e6, "Shorts liquidated ($m)", style = bars, color = up) ``` ## iv Implied volatility: DVOL, in percent (BTC and ETH). **Returns:** a number per bar **Example** ```vex plot(iv, "IV") plot(sma(iv, 30), "30-bar average") ``` ## prem_calls Net call premium in the bar (bought minus sold), in $ (BTC and ETH). **Returns:** a number per bar **Example** ```vex plot((prem_calls - prem_puts) / 1e6, "Net premium ($m)", style = bars, color = prem_calls > prem_puts ? up : down) ``` ## prem_puts Net put premium in the bar (bought minus sold), in $ (BTC and ETH). **Returns:** a number per bar **Example** ```vex plot(prem_puts) ``` ## gamma_flip The gamma flip (HVL): the price nearest spot where cumulative dealer gamma changes sign, from the same dealer book as gex (Flow, all expiries; empty with no crossing within 25%). Crypto only. Each bar has the flip of the dealer book in force at its close, from the same history as the chart's gamma levels: never a level from the future. Further back the history is coarser (the server spaces snapshots to fit the window). **Returns:** a number per bar **Example** ```vex name("Gamma flip", where = chart) plot(gamma_flip, "Gamma flip", color = orange, style = steps) background(close > gamma_flip ? #22c55e0d : #ef44440d) ``` See also: [call_wall](/documentation/vex/reference/backquant-data#call-wall), [put_wall](/documentation/vex/reference/backquant-data#put-wall), [gex](/documentation/vex/reference/backquant-data#gex) ## call_wall The call wall: the strike above the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. **Returns:** a number per bar **Example** ```vex name("Call wall", where = chart) plot(call_wall, "Call wall", color = green, style = steps) mark(crosses_above(close, call_wall) and closed, "C", where = below) ``` See also: [put_wall](/documentation/vex/reference/backquant-data#put-wall), [gamma_flip](/documentation/vex/reference/backquant-data#gamma-flip) ## put_wall The put wall: the strike below the price with the largest positive dealer gamma (Flow, all expiries). Crypto only. **Returns:** a number per bar **Example** ```vex name("Put wall", where = chart) plot(put_wall, "Put wall", color = red, style = steps) mark(crosses_below(close, put_wall) and closed, "P", where = above) ``` See also: [call_wall](/documentation/vex/reference/backquant-data#call-wall), [gamma_flip](/documentation/vex/reference/backquant-data#gamma-flip) ## etf_flow Net creations into the US spot ETFs, in $ per day (BTC, ETH, SOL, XRP; daily, lined up with the bars). **Returns:** a number per bar **Example** ```vex plot(etf_flow / 1e6, "ETF flow ($m)", style = bars, color = etf_flow > 0 ? up : down) plot(sum(etf_flow, 5) / 1e6, "5-day total ($m)", color = orange) ``` ## rv Realised volatility, in percent: what price has done, to set against iv (BTC and ETH, daily). **Returns:** a number per bar **Example** ```vex plot(iv - rv, "Vol premium (IV - RV)", style = bars, color = iv > rv ? up : down) level(0) ``` See also: [iv](/documentation/vex/reference/backquant-data#iv) ## rr25 30-day 25-delta risk reversal: call IV minus put IV, in vol points (above 0, calls are pricier). Crypto only. **Returns:** a number per bar **Example** ```vex plot(rr25, "25d risk reversal (30d)", style = bars, color = rr25 > 0 ? up : down) level(0) ``` See also: [fly25](/documentation/vex/reference/backquant-data#fly25), [iv](/documentation/vex/reference/backquant-data#iv) ## fly25 30-day 25-delta butterfly: how much the wings cost over ATM, in vol points. Crypto only. **Returns:** a number per bar **Example** ```vex plot(fly25, "25d butterfly (30d)") ``` See also: [rr25](/documentation/vex/reference/backquant-data#rr25) ## basis The perp aggregate over the spot aggregate, in basis points (above 0, perps trade rich). Crypto. **Returns:** a number per bar **Example** ```vex plot(basis, "Basis (bps)", style = bars, color = basis > 0 ? up : down) level(0) ``` ## cb_premium Coinbase's USD price over Binance's USDT price, in percent (above 0, US buyers pay up). BTC, ETH, SOL. **Returns:** a number per bar **Example** ```vex plot(cb_premium, "Coinbase premium (%)", style = bars, color = cb_premium > 0 ? up : down) level(0) ``` ## put_call Puts' open interest over calls' (above 1, more puts open). BTC and ETH, daily. **Returns:** a number per bar **Example** ```vex plot(put_call, "Put/call OI") level(1) ``` ## short_share The share of the day's volume reported as short sales on FINRA venues, in percent. US equities, daily. **Returns:** a number per bar **Example** ```vex plot(short_share, "Short % of volume") plot(sma(short_share, 20), "20-day average", color = orange) level(50) ``` ## gex_0dte Net dealer gamma exposure, in $ per 1% move (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. **Returns:** a number per bar **Example** ```vex plot(gex_0dte) ``` ## gex_front Net dealer gamma exposure, in $ per 1% move (flow-signed, the front (nearest) expiry). Crypto only. **Returns:** a number per bar **Example** ```vex plot(gex_front) ``` ## gex_week Net dealer gamma exposure, in $ per 1% move (flow-signed, expiries within 7 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(gex_week) ``` ## gex_month Net dealer gamma exposure, in $ per 1% move (flow-signed, expiries within 31 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(gex_month) ``` ## dex Net dealer delta, in $ (flow-signed, all expiries). Crypto only. **Returns:** a number per bar **Example** ```vex plot(dex) ``` ## dex_0dte Net dealer delta, in $ (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. **Returns:** a number per bar **Example** ```vex plot(dex_0dte) ``` ## dex_front Net dealer delta, in $ (flow-signed, the front (nearest) expiry). Crypto only. **Returns:** a number per bar **Example** ```vex plot(dex_front) ``` ## dex_week Net dealer delta, in $ (flow-signed, expiries within 7 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(dex_week) ``` ## dex_month Net dealer delta, in $ (flow-signed, expiries within 31 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(dex_month) ``` ## vanna Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, all expiries). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vanna) ``` ## vanna_0dte Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vanna_0dte) ``` ## vanna_front Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, the front (nearest) expiry). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vanna_front) ``` ## vanna_week Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, expiries within 7 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vanna_week) ``` ## vanna_month Net dealer vanna (VEX): how dealer delta moves when implied vol moves, in $ of delta per vol point (flow-signed, expiries within 31 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vanna_month) ``` ## charm Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, all expiries). Crypto only. **Returns:** a number per bar **Example** ```vex plot(charm) ``` ## charm_0dte Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. **Returns:** a number per bar **Example** ```vex plot(charm_0dte) ``` ## charm_front Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, the front (nearest) expiry). Crypto only. **Returns:** a number per bar **Example** ```vex plot(charm_front) ``` ## charm_week Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, expiries within 7 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(charm_week) ``` ## charm_month Net dealer charm (CEX): how dealer delta drifts as time passes, in $ of delta per day (flow-signed, expiries within 31 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(charm_month) ``` ## vega Net dealer vega, in $ per vol point (flow-signed, all expiries). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vega) ``` ## vega_0dte Net dealer vega, in $ per vol point (flow-signed, the front expiry while it expires within a day (0DTE), else empty). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vega_0dte) ``` ## vega_front Net dealer vega, in $ per vol point (flow-signed, the front (nearest) expiry). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vega_front) ``` ## vega_week Net dealer vega, in $ per vol point (flow-signed, expiries within 7 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vega_week) ``` ## vega_month Net dealer vega, in $ per vol point (flow-signed, expiries within 31 days). Crypto only. **Returns:** a number per bar **Example** ```vex plot(vega_month) ``` # Vex reference: Averages | Name | What | | --- | --- | | [`sma`](#sma) | Simple moving average of x over n bars. | | [`ema`](#ema) | Exponential moving average of x over n bars (seeded with the SMA). | | [`rma`](#rma) | Wilder's moving average (the one RSI and ATR use). | | [`wma`](#wma) | Weighted moving average: the newest bar weighs n, the oldest 1. | | [`dema`](#dema) | Double EMA: 2 x ema(x, n) - ema(ema(x, n), n), quicker to turn than an EMA. | | [`tema`](#tema) | Triple EMA: 3 x e1 - 3 x e2 + e3 for an EMA taken one, two and three times. | | [`t3`](#t3) | Tillson T3: six EMAs blended by the volume factor vf (0.7 as usual), smooth with little lag. | | [`alma`](#alma) | Arnaud Legoux moving average: Gaussian weights centred offset of the way from the oldest bar (0.85 leans to the newest), as wide as n / sigma. | | [`hma`](#hma) | Hull moving average: smooth and quick to turn. | | [`vwma`](#vwma) | Volume-weighted moving average. | ## sma ```vex sma(x, n) ``` Simple moving average of x over n bars. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.sma(source, length)` **Example** ```vex plot(sma(close, 20)) ``` See also: [ema](/documentation/vex/reference/averages#ema), [wma](/documentation/vex/reference/averages#wma) ## ema ```vex ema(x, n) ``` Exponential moving average of x over n bars (seeded with the SMA). **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.ema(source, length)` **Example** ```vex plot(ema(close, 21)) ``` See also: [sma](/documentation/vex/reference/averages#sma), [rma](/documentation/vex/reference/averages#rma) ## rma ```vex rma(x, n) ``` Wilder's moving average (the one RSI and ATR use). **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.rma(source, length)` **Example** ```vex gain = max(close - prev(close), 0) plot(rma(gain, 14), "Average gain") ``` See also: [rsi](/documentation/vex/reference/indicators#rsi), [atr](/documentation/vex/reference/indicators#atr) ## wma ```vex wma(x, n) ``` Weighted moving average: the newest bar weighs n, the oldest 1. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.wma(source, length)` **Example** ```vex plot(wma(close, 9)) ``` ## dema ```vex dema(x, n) ``` Double EMA: 2 x ema(x, n) - ema(ema(x, n), n), quicker to turn than an EMA. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.dema(source, length) (ta library)` **Example** ```vex plot(dema(close, 20)) ``` ## tema ```vex tema(x, n) ``` Triple EMA: 3 x e1 - 3 x e2 + e3 for an EMA taken one, two and three times. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.tema(source, length) (ta library)` **Example** ```vex plot(tema(close, 20)) ``` ## t3 ```vex t3(x, n, vf = 0.7) ``` Tillson T3: six EMAs blended by the volume factor vf (0.7 as usual), smooth with little lag. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting - `vf`: a number fixed when the script runs: a number or a setting; 0.7 when left out **Returns:** a number per bar **In Pine Script:** `ta.t3(source, length, volumeFactor) (ta library)` **Example** ```vex plot(t3(close, 5, 0.7), "T3") plot(ema(close, 5), "EMA", color = grey) ``` ## alma ```vex alma(x, n, offset = 0.85, sigma = 6) ``` Arnaud Legoux moving average: Gaussian weights centred offset of the way from the oldest bar (0.85 leans to the newest), as wide as n / sigma. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting - `offset`: a number fixed when the script runs: a number or a setting; 0.85 when left out - `sigma`: a number fixed when the script runs: a number or a setting; 6 when left out **Returns:** a number per bar **In Pine Script:** `ta.alma(source, length, offset, sigma)` **Example** ```vex plot(alma(close, 9, 0.85, 6)) ``` ## hma ```vex hma(x, n) ``` Hull moving average: smooth and quick to turn. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.hma(source, length)` **Example** ```vex name("Hull", where = chart) f = hma(close, 55) s = prev(f, 2) plot(f, "Hull", color = f > s ? #00ff00 : #ff0000) plot(s, "Hull 2", color = f > s ? #00ff00 : #ff0000) fill(f, s, color = f > s ? #00ff00 : #ff0000, opacity = 40) ``` ## vwma ```vex vwma(x, n) ``` Volume-weighted moving average. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.vwma(source, length)` **Example** ```vex plot(vwma(close, 20)) ``` # Vex reference: Windows | Name | What | | --- | --- | | [`highest`](#highest) | The highest x over the last n bars. | | [`lowest`](#lowest) | The lowest x over the last n bars. | | [`sum`](#sum) | The total of x over the last n bars. | | [`stdev`](#stdev) | Standard deviation of x over the last n bars (population). | | [`change`](#change) | How much x moved over n bars: x - prev(x, n). | | [`roc`](#roc) | Rate of change: percent move of x over n bars. | | [`total`](#total) | Running total of x from the first bar (empty values add nothing). | | [`median`](#median) | The middle value of x over the last n bars. | | [`dev`](#dev) | Mean absolute deviation of x from its n-bar average. | | [`percentrank`](#percentrank) | Percent of the previous n values of x at or below this one (0 to 100). | | [`linreg`](#linreg) | Linear regression: the least-squares line through the last n values of x, at this bar. | | [`correlation`](#correlation) | Correlation of a and b over the last n bars (-1 to 1). | | [`loop_score`](#loop-score) | The for-loop score: for each bar back from `from` to `to`, +1 if x is above it now, -1 if not (or if either is empty). | | [`prev`](#prev) | x as it was n bars ago (empty before the first bar, unless x has a starts line). | ## highest ```vex highest(x, n) ``` The highest x over the last n bars. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.highest(source, length)` **Example** ```vex plot(highest(high, 20)) ``` See also: [lowest](/documentation/vex/reference/windows#lowest), [donchian](/documentation/vex/reference/indicators#donchian) ## lowest ```vex lowest(x, n) ``` The lowest x over the last n bars. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.lowest(source, length)` **Example** ```vex plot(lowest(low, 20)) ``` See also: [highest](/documentation/vex/reference/windows#highest), [donchian](/documentation/vex/reference/indicators#donchian) ## sum ```vex sum(x, n) ``` The total of x over the last n bars. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `math.sum(source, length)` **Example** ```vex plot(sum(volume, 24)) ``` See also: [total](/documentation/vex/reference/windows#total), [count](/documentation/vex/reference/events#count) ## stdev ```vex stdev(x, n) ``` Standard deviation of x over the last n bars (population). **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.stdev(source, length)` **Example** ```vex plot(stdev(close, 20)) ``` ## change ```vex change(x, n = 1) ``` How much x moved over n bars: x - prev(x, n). **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 1 when left out **Returns:** a number per bar **In Pine Script:** `ta.change(source, length)` **Example** ```vex plot(change(close)) ``` ## roc ```vex roc(x, n) ``` Rate of change: percent move of x over n bars. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.roc(source, length)` **Example** ```vex plot(roc(close, 10)) ``` ## total ```vex total(x) ``` Running total of x from the first bar (empty values add nothing). **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `ta.cum(source)` **Example** ```vex plot(total(volume)) ``` ## median ```vex median(x, n) ``` The middle value of x over the last n bars. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.median(source, length)` **Example** ```vex plot(median(close, 20)) ``` ## dev ```vex dev(x, n) ``` Mean absolute deviation of x from its n-bar average. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.dev(source, length)` **Example** ```vex plot(dev(close, 20)) ``` ## percentrank ```vex percentrank(x, n) ``` Percent of the previous n values of x at or below this one (0 to 100). **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.percentrank(source, length)` **Example** ```vex plot(percentrank(volume, 100)) ``` ## linreg ```vex linreg(x, n) ``` Linear regression: the least-squares line through the last n values of x, at this bar. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.linreg(source, length, 0)` **Example** ```vex plot(linreg(close, 50)) ``` ## correlation ```vex correlation(a, b, n) ``` Correlation of a and b over the last n bars (-1 to 1). **Arguments** - `a`: a number per bar - `b`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.correlation(source1, source2, length)` **Example** ```vex plot(correlation(close, volume, 50)) ``` ## loop_score ```vex loop_score(x, from, to) ``` The for-loop score: for each bar back from `from` to `to`, +1 if x is above it now, -1 if not (or if either is empty). For each bar back from `from` to `to`: +1 when x is above that bar's value now, -1 when it isn't (or either is empty). It is the for-loop many Pine trend scores use, done without a loop. **Arguments** - `x`: a number per bar - `from`: a whole number, 0 to 5000, fixed: a number or a setting - `to`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `for i = from to to: score += x > x[i] ? 1 : -1` **Example** ```vex plot(loop_score(rsi(close, 14), 1, 45)) ``` See also: [Your own functions](/documentation/vex/your-own-functions) ## prev ```vex prev(x, n = 1) ``` x as it was n bars ago (empty before the first bar, unless x has a starts line). **Arguments** - `x`: a number per bar - `n`: a whole number, 0 to 5000, fixed: a number or a setting; 1 when left out **Returns:** a number per bar **In Pine Script:** `source[n]` **Example** ```vex plot(close - prev(close), "Change") plot(close - prev(close, 10), "10-bar change") ``` See also: [Looking back: prev](/documentation/vex/looking-back-prev), [Values that carry over: starts](/documentation/vex/values-that-carry-over-starts) # Vex reference: Indicators | Name | What | | --- | --- | | [`rsi`](#rsi) | Wilder's relative strength index, 0 to 100. | | [`atr`](#atr) | Average true range over n bars (Wilder smoothing). | | [`cci`](#cci) | Commodity channel index on hlc3. | | [`mfi`](#mfi) | Money flow index on hlc3 and volume, 0 to 100. | | [`bands`](#bands) | Bollinger bands: the SMA, and mult standard deviations above and below. | | [`macd`](#macd) | MACD: fast EMA minus slow EMA, its EMA signal, and the gap between them. | | [`stoch`](#stoch) | Stochastic: where the close sits in the n-bar range (0 to 100), smoothed, and its average. | | [`donchian`](#donchian) | Donchian channel: the highest high and lowest low of the last n bars, and their middle. | | [`pivot_high`](#pivot-high) | A pivot high: x of the bar `right` bars back when it is higher than the `left` bars before it and the `right` bars after it; empty elsewhere. It is given on the bar that confirms it (`right` bars later); plot it with offset = -right to draw it on the pivot bar. | | [`pivot_low`](#pivot-low) | A pivot low: x of the bar `right` bars back when it is lower than the `left` bars before it and the `right` bars after it; empty elsewhere. Given on the bar that confirms it. | | [`ichimoku`](#ichimoku) | Ichimoku: the conversion and base lines (the middle of the n-bar high-low range) and the leading spans as worked out on each bar. Draw the spans 25 bars ahead with offset = 25, and the lagging line as plot(close, offset = -25). | | [`keltner`](#keltner) | Keltner channel: an EMA, and mult ATRs above and below it. | | [`williams_r`](#williams-r) | Williams %R: where the close sits in the n-bar range, -100 at the low to 0 at the high. | | [`stochrsi`](#stochrsi) | Stochastic RSI: the stochastic of the RSI, smoothed, and its average. | | [`cmf`](#cmf) | Chaikin money flow: where closes sit in their bars, weighted by volume, over n bars (-1 to 1). | | [`dmi`](#dmi) | Directional movement: +DI, -DI and the ADX trend strength. | | [`supertrend`](#supertrend) | Supertrend on hl2: a trailing line under the price in an up trend (dir 1), over it in a down trend (dir -1). | | [`psar`](#psar) | Parabolic SAR: a stop that accelerates with the trend; dir 1 when it sits under the price. | ## rsi ```vex rsi(x, n) ``` Wilder's relative strength index, 0 to 100. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.rsi(source, length)` **Example** ```vex r = rsi(close, 14) plot(r, "RSI") level(70, 30) ``` ## atr ```vex atr(n) ``` Average true range over n bars (Wilder smoothing). **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.atr(length)` **Example** ```vex plot(atr(14)) ``` See also: [tr](/documentation/vex/reference/bar-values#tr), [keltner](/documentation/vex/reference/indicators#keltner) ## cci ```vex cci(n) ``` Commodity channel index on hlc3. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.cci(hlc3, length)` **Example** ```vex plot(cci(20)) ``` ## mfi ```vex mfi(n) ``` Money flow index on hlc3 and volume, 0 to 100. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `ta.mfi(hlc3, length)` **Example** ```vex plot(mfi(14)) ``` ## bands ```vex bands(x, n, mult = 2) ``` Bollinger bands: the SMA, and mult standard deviations above and below. Pine's ta.bb gives [middle, upper, lower]; here they are .mid, .upper and .lower. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting - `mult`: a number per bar; 2 when left out **Returns:** several parts: .upper .mid .lower **In Pine Script:** `ta.bb(source, length, mult)` **Example** ```vex name("Bollinger", where = chart) b = bands(close, 20, 2) plot(b.upper, "Upper") plot(b.mid, "Middle") plot(b.lower, "Lower") fill(b.upper, b.lower) ``` ## macd ```vex macd(x, fast = 12, slow = 26, signal = 9) ``` MACD: fast EMA minus slow EMA, its EMA signal, and the gap between them. **Arguments** - `x`: a number per bar - `fast`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 12 when left out - `slow`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 26 when left out - `signal`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 9 when left out **Returns:** several parts: .line .signal .hist **In Pine Script:** `ta.macd(source, fast, slow, signal)` **Example** ```vex m = macd(close, 12, 26, 9) plot(m.hist, "Histogram", style = bars, color = m.hist > 0 ? up : down) plot(m.line, "MACD", color = blue) plot(m.signal, "Signal", color = orange) level(0) ``` ## stoch ```vex stoch(n = 14, ksmooth = 1, dsmooth = 3) ``` Stochastic: where the close sits in the n-bar range (0 to 100), smoothed, and its average. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 14 when left out - `ksmooth`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 1 when left out - `dsmooth`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 3 when left out **Returns:** several parts: .k .d **In Pine Script:** `ta.sma(ta.stoch(close, high, low, n), ksmooth)` **Example** ```vex s = stoch(14, 1, 3) plot(s.k, "%K") plot(s.d, "%D") level(80, 20) ``` ## donchian ```vex donchian(n = 20) ``` Donchian channel: the highest high and lowest low of the last n bars, and their middle. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 20 when left out **Returns:** several parts: .upper .mid .lower **Example** ```vex d = donchian(20) plot(d.upper, "upper") plot(d.mid, "mid") plot(d.lower, "lower") ``` ## pivot_high ```vex pivot_high(x, left = 5, right = 5) ``` A pivot high: x of the bar `right` bars back when it is higher than the `left` bars before it and the `right` bars after it; empty elsewhere. It is given on the bar that confirms it (`right` bars later); plot it with offset = -right to draw it on the pivot bar. Like Pine's, the value comes on the bar that confirms the pivot, `right` bars after it, so it never repaints. `offset = -right` draws it on the pivot bar itself. **Arguments** - `x`: a number per bar - `left`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 5 when left out - `right`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 5 when left out **Returns:** a number per bar **In Pine Script:** `ta.pivothigh(source, leftbars, rightbars)` **Example** ```vex name("Pivots", where = chart) ph = pivot_high(high, 5, 5) pl = pivot_low(low, 5, 5) plot(ph, "Pivot high", style = dots, width = 3, color = red, offset = -5) plot(pl, "Pivot low", style = dots, width = 3, color = green, offset = -5) ``` See also: [pivot_low](/documentation/vex/reference/indicators#pivot-low), [plot](/documentation/vex/reference/drawing#plot) ## pivot_low ```vex pivot_low(x, left = 5, right = 5) ``` A pivot low: x of the bar `right` bars back when it is lower than the `left` bars before it and the `right` bars after it; empty elsewhere. Given on the bar that confirms it. **Arguments** - `x`: a number per bar - `left`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 5 when left out - `right`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 5 when left out **Returns:** a number per bar **In Pine Script:** `ta.pivotlow(source, leftbars, rightbars)` **Example** ```vex name("Pivot lows", where = chart) pl = pivot_low(low, 5, 5) plot(pl, "Pivot low", style = dots, width = 3, color = green, offset = -5) plot(when(not empty(pl), pl), "Last pivot low", style = steps, color = grey) ``` See also: [pivot_high](/documentation/vex/reference/indicators#pivot-high), [when](/documentation/vex/reference/events#when) ## ichimoku ```vex ichimoku(conversion = 9, base = 26, span_b = 52) ``` Ichimoku: the conversion and base lines (the middle of the n-bar high-low range) and the leading spans as worked out on each bar. Draw the spans 25 bars ahead with offset = 25, and the lagging line as plot(close, offset = -25). The spans are worked out on each bar and drawn 25 bars ahead with `offset = 25` (a displacement of 26 less one); the lagging line is the close drawn 25 bars back. **Arguments** - `conversion`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 9 when left out - `base`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 26 when left out - `span_b`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 52 when left out **Returns:** several parts: .conversion .base .span_a .span_b **Example** ```vex name("Ichimoku", where = chart) ich = ichimoku(9, 26, 52) plot(ich.conversion, "Conversion", color = blue) plot(ich.base, "Base", color = maroon) plot(ich.span_a, "Span A", color = green, offset = 25) plot(ich.span_b, "Span B", color = red, offset = 25) fill(ich.span_a, ich.span_b, opacity = 15) plot(close, "Lagging", color = teal, offset = -25) ``` See also: [plot](/documentation/vex/reference/drawing#plot), [fill](/documentation/vex/reference/drawing#fill), [donchian](/documentation/vex/reference/indicators#donchian) ## keltner ```vex keltner(x, n = 20, atr_n = 10, mult = 2) ``` Keltner channel: an EMA, and mult ATRs above and below it. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 20 when left out - `atr_n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 10 when left out - `mult`: a number per bar; 2 when left out **Returns:** several parts: .upper .mid .lower **Example** ```vex k = keltner(close, 20, 10, 2) plot(k.upper, "upper") plot(k.mid, "mid") plot(k.lower, "lower") ``` ## williams_r ```vex williams_r(n = 14) ``` Williams %R: where the close sits in the n-bar range, -100 at the low to 0 at the high. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 14 when left out **Returns:** a number per bar **In Pine Script:** `ta.wpr(length)` **Example** ```vex plot(williams_r(14)) ``` ## stochrsi ```vex stochrsi(x, rsi_n = 14, stoch_n = 14, ksmooth = 3, dsmooth = 3) ``` Stochastic RSI: the stochastic of the RSI, smoothed, and its average. **Arguments** - `x`: a number per bar - `rsi_n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 14 when left out - `stoch_n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 14 when left out - `ksmooth`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 3 when left out - `dsmooth`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 3 when left out **Returns:** several parts: .k .d **Example** ```vex s = stochrsi(close, 14, 14, 3, 3) plot(s.k, "k") plot(s.d, "d") ``` ## cmf ```vex cmf(n = 20) ``` Chaikin money flow: where closes sit in their bars, weighted by volume, over n bars (-1 to 1). **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 20 when left out **Returns:** a number per bar **Example** ```vex plot(cmf(20)) ``` ## dmi ```vex dmi(n = 14, smooth = 14) ``` Directional movement: +DI, -DI and the ADX trend strength. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 14 when left out - `smooth`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 14 when left out **Returns:** several parts: .plus .minus .adx **In Pine Script:** `ta.dmi(diLength, adxSmoothing)` **Example** ```vex m = dmi(14, 14) plot(m.plus, "+DI", color = green) plot(m.minus, "-DI", color = red) plot(m.adx, "ADX", color = blue) ``` ## supertrend ```vex supertrend(n = 10, factor = 3) ``` Supertrend on hl2: a trailing line under the price in an up trend (dir 1), over it in a down trend (dir -1). Pine's arguments are the other way round (factor first), and its direction is -1 in an up trend; here `.dir` is 1 up and -1 down. **Arguments** - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting; 10 when left out - `factor`: a number fixed when the script runs: a number or a setting; 3 when left out **Returns:** several parts: .line .dir **In Pine Script:** `ta.supertrend(factor, atrPeriod)` **Example** ```vex name("Supertrend", where = chart) st = supertrend(10, 3) plot(st.line, "Supertrend", color = st.dir > 0 ? up : down) ``` ## psar ```vex psar(start = 0.02, inc = 0.02, max = 0.2) ``` Parabolic SAR: a stop that accelerates with the trend; dir 1 when it sits under the price. **Arguments** - `start`: a number fixed when the script runs: a number or a setting; 0.02 when left out - `inc`: a number fixed when the script runs: a number or a setting; 0.02 when left out - `max`: a number fixed when the script runs: a number or a setting; 0.2 when left out **Returns:** several parts: .value .dir **In Pine Script:** `ta.sar(start, inc, max)` **Example** ```vex name("SAR", where = chart) sar = psar(0.02, 0.02, 0.2) plot(sar.value, "SAR", style = dots, color = sar.dir > 0 ? up : down) ``` # Vex reference: Events | Name | What | | --- | --- | | [`crosses_above`](#crosses-above) | True on the bar a goes from at or below b to above it. | | [`crosses_below`](#crosses-below) | True on the bar a goes from at or above b to below it. | | [`rising`](#rising) | True when x is above every value of the last n bars before it. | | [`falling`](#falling) | True when x is below every value of the last n bars before it. | | [`count`](#count) | How many of the last n bars cond was true on. | | [`bars_since`](#bars-since) | Bars since cond was last true (0 on a bar where it is true; empty before it ever was). | | [`when`](#when) | x on the most recent bar where cond was true (k = 1 for the one before that, ...). | ## crosses_above ```vex crosses_above(a, b) ``` True on the bar a goes from at or below b to above it. **Arguments** - `a`: a number per bar - `b`: a number per bar **Returns:** a yes/no per bar **In Pine Script:** `ta.crossover(a, b)` **Example** ```vex fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) mark(crosses_above(fast, slow), "B", where = below) ``` See also: [crosses_below](/documentation/vex/reference/events#crosses-below), [Marks, finished bars and alerts](/documentation/vex/marks-finished-bars-and-alerts) ## crosses_below ```vex crosses_below(a, b) ``` True on the bar a goes from at or above b to below it. **Arguments** - `a`: a number per bar - `b`: a number per bar **Returns:** a yes/no per bar **In Pine Script:** `ta.crossunder(a, b)` **Example** ```vex fast = ema(close, 9) slow = ema(close, 21) plot(fast) plot(slow) mark(crosses_below(fast, slow), "S", where = above) ``` See also: [crosses_above](/documentation/vex/reference/events#crosses-above), [Marks, finished bars and alerts](/documentation/vex/marks-finished-bars-and-alerts) ## rising ```vex rising(x, n) ``` True when x is above every value of the last n bars before it. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a yes/no per bar **In Pine Script:** `ta.rising(source, length)` **Example** ```vex plot(close) mark(rising(close, 3), where = below, shape = dot) ``` ## falling ```vex falling(x, n) ``` True when x is below every value of the last n bars before it. **Arguments** - `x`: a number per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a yes/no per bar **In Pine Script:** `ta.falling(source, length)` **Example** ```vex plot(close) mark(falling(close, 3), where = above, shape = dot) ``` ## count ```vex count(cond, n) ``` How many of the last n bars cond was true on. **Arguments** - `cond`: a yes/no per bar - `n`: a whole number of bars, 1 to 5000, fixed: a number or a setting **Returns:** a number per bar **In Pine Script:** `math.sum(cond ? 1 : 0, length)` **Example** ```vex plot(count(close > open, 10), "Up bars of the last 10") ``` ## bars_since ```vex bars_since(cond) ``` Bars since cond was last true (0 on a bar where it is true; empty before it ever was). **Arguments** - `cond`: a yes/no per bar **Returns:** a number per bar **In Pine Script:** `ta.barssince(condition)` **Example** ```vex plot(bars_since(crosses_above(close, ema(close, 20))), "Bars since the cross") ``` ## when ```vex when(cond, x, k = 0) ``` x on the most recent bar where cond was true (k = 1 for the one before that, ...). **Arguments** - `cond`: a yes/no per bar - `x`: a number per bar - `k`: a whole number, 0 to 5000, fixed: a number or a setting; 0 when left out **Returns:** a number per bar **In Pine Script:** `ta.valuewhen(condition, source, occurrence)` **Example** ```vex name("Last cross price", where = chart) plot(when(crosses_above(close, ema(close, 20)), close), "Price at the last cross", style = steps) ``` # Vex reference: Other bars | Name | What | | --- | --- | | [`timeframe`](#timeframe) | x worked out on another timeframe of this chart's market (1m, 2m, 5m, 15m, 30m, 1h, 2h, 4h, 1D, 1W). Each bar reads the value of the last bar of that timeframe that had closed by its own close, so history never repaints; the newest bar follows the one still forming. | | [`symbol`](#symbol) | x worked out on another market's bars (its own timeframe with timeframe =), lined up with this chart's bars the same way: no lookahead. Another market is drawn in its own pane. | ## timeframe ```vex timeframe("4h", x) ``` x worked out on another timeframe of this chart's market (1m, 2m, 5m, 15m, 30m, 1h, 2h, 4h, 1D, 1W). Each bar reads the value of the last bar of that timeframe that had closed by its own close, so history never repaints; the newest bar follows the one still forming. **Returns:** a number per bar **In Pine Script:** `request.security(syminfo.tickerid, "240", x)` **Example** ```vex name("4h EMA", where = chart) plot(ema(close, 20), "EMA 20") plot(timeframe("4h", ema(close, 20)), "EMA 20 on 4h", color = orange, style = steps) ``` See also: [Other timeframes and markets](/documentation/vex/other-timeframes-and-markets), [symbol](/documentation/vex/reference/other-bars#symbol) ## symbol ```vex symbol("ETHUSDT", x, timeframe = "4h") ``` x worked out on another market's bars (its own timeframe with timeframe =), lined up with this chart's bars the same way: no lookahead. Another market is drawn in its own pane. **Returns:** a number per bar **In Pine Script:** `request.security("ETHUSDT", timeframe.period, x)` **Example** ```vex name("BTC in ETH") eth = symbol("ETHUSDT", close) plot(close / eth, "BTC / ETH") ``` See also: [Other timeframes and markets](/documentation/vex/other-timeframes-and-markets), [timeframe](/documentation/vex/reference/other-bars#timeframe) # Vex reference: Maths | Name | What | | --- | --- | | [`max`](#max) | The largest of the values (empty values are skipped). | | [`min`](#min) | The smallest of the values (empty values are skipped). | | [`abs`](#abs) | x without its sign. | | [`sqrt`](#sqrt) | Square root (empty below 0). | | [`log`](#log) | Natural logarithm (empty at 0 and below). | | [`exp`](#exp) | e to the power x. | | [`round`](#round) | Nearest whole number. | | [`floor`](#floor) | Whole number at or below x. | | [`ceil`](#ceil) | Whole number at or above x. | | [`sign`](#sign) | 1 if x is above 0, -1 below, 0 at 0. | | [`pow`](#pow) | x to the power p. | | [`clamp`](#clamp) | x kept between lo and hi. | | [`fallback`](#fallback) | x, or y where x is empty. | | [`empty`](#empty) | True where x has no value (not enough history yet, or a division by zero). | ## max ```vex max(a, b, ...) ``` The largest of the values (empty values are skipped). **Arguments** - `a`: a number per bar - `b`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.max(a, b)` **Example** ```vex plot(max(close - open, 0)) ``` ## min ```vex min(a, b, ...) ``` The smallest of the values (empty values are skipped). **Arguments** - `a`: a number per bar - `b`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.min(a, b)` **Example** ```vex plot(min(open, close)) ``` ## abs ```vex abs(x) ``` x without its sign. **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.abs(x)` **Example** ```vex plot(abs(close - open)) ``` ## sqrt ```vex sqrt(x) ``` Square root (empty below 0). **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.sqrt(x)` **Example** ```vex plot(stdev(close, 20) * sqrt(365), "Scaled deviation") ``` ## log ```vex log(x) ``` Natural logarithm (empty at 0 and below). **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.log(x)` **Example** ```vex plot(log(close)) ``` ## exp ```vex exp(x) ``` e to the power x. **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.exp(x)` **Example** ```vex plot(exp(roc(close, 1) / 100)) ``` ## round ```vex round(x) ``` Nearest whole number. **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.round(x)` **Example** ```vex plot(round(rsi(close, 14)), "RSI, whole") ``` ## floor ```vex floor(x) ``` Whole number at or below x. **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.floor(x)` **Example** ```vex plot(floor(close / 100) * 100, "Round hundred below", style = steps) ``` ## ceil ```vex ceil(x) ``` Whole number at or above x. **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.ceil(x)` **Example** ```vex plot(ceil(close / 100) * 100, "Round hundred above", style = steps) ``` ## sign ```vex sign(x) ``` 1 if x is above 0, -1 below, 0 at 0. **Arguments** - `x`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.sign(x)` **Example** ```vex plot(sign(change(close))) ``` ## pow ```vex pow(x, p) ``` x to the power p. **Arguments** - `x`: a number per bar - `p`: a number per bar **Returns:** a number per bar **In Pine Script:** `math.pow(x, p)` **Example** ```vex plot(pow(change(close), 2), "Squared change") ``` ## clamp ```vex clamp(x, lo, hi) ``` x kept between lo and hi. **Arguments** - `x`: a number per bar - `lo`: a number per bar - `hi`: a number per bar **Returns:** a number per bar **Example** ```vex plot(clamp(rsi(close, 14), 30, 70), "RSI held to 30-70") ``` ## fallback ```vex fallback(x, y) ``` x, or y where x is empty. **Arguments** - `x`: a number per bar - `y`: a number per bar **Returns:** a number per bar **In Pine Script:** `nz(x, y)` **Example** ```vex plot(fallback(prev(close), close), "Previous close") ``` See also: [Empty values](/documentation/vex/empty-values), [empty](/documentation/vex/reference/maths#empty) ## empty ```vex empty(x) ``` True where x has no value (not enough history yet, or a division by zero). **Arguments** - `x`: a number per bar **Returns:** a yes/no per bar **In Pine Script:** `na(x)` **Example** ```vex r = rsi(close, 14) plot(empty(r) ? 50 : r, "RSI, 50 until ready") ``` See also: [Empty values](/documentation/vex/empty-values), [fallback](/documentation/vex/reference/maths#fallback) # Vex reference: Colors | Name | What | | --- | --- | | [`green`](#green) | green (#22c55e) | | [`red`](#red) | red (#ef4444) | | [`blue`](#blue) | blue (#3b82f6) | | [`orange`](#orange) | orange (#f97316) | | [`purple`](#purple) | purple (#a855f7) | | [`yellow`](#yellow) | yellow (#eab308) | | [`teal`](#teal) | teal (#14b8a6) | | [`pink`](#pink) | pink (#ec4899) | | [`grey`](#grey) | grey (#9ca3af) | | [`white`](#white) | white (#e5e7eb) | | [`aqua`](#aqua) | aqua (#06b6d4) | | [`lime`](#lime) | lime (#84cc16) | | [`fuchsia`](#fuchsia) | fuchsia (#d946ef) | | [`maroon`](#maroon) | maroon (#991b1b) | | [`navy`](#navy) | navy (#1e3a8a) | | [`olive`](#olive) | olive (#808000) | | [`silver`](#silver) | silver (#cbd5e1) | | [`black`](#black) | black (#0a0a0a) | | [`up`](#up) | Your candle color for up bars. | | [`down`](#down) | Your candle color for down bars. | ## green green (#22c55e) **Returns:** a color **Example** ```vex plot(close, color = green, width = 2) ``` ## red red (#ef4444) **Returns:** a color **Example** ```vex plot(close, color = red, width = 2) ``` ## blue blue (#3b82f6) **Returns:** a color **Example** ```vex plot(close, color = blue, width = 2) ``` ## orange orange (#f97316) **Returns:** a color **Example** ```vex plot(close, color = orange, width = 2) ``` ## purple purple (#a855f7) **Returns:** a color **Example** ```vex plot(close, color = purple, width = 2) ``` ## yellow yellow (#eab308) **Returns:** a color **Example** ```vex plot(close, color = yellow, width = 2) ``` ## teal teal (#14b8a6) **Returns:** a color **Example** ```vex plot(close, color = teal, width = 2) ``` ## pink pink (#ec4899) **Returns:** a color **Example** ```vex plot(close, color = pink, width = 2) ``` ## grey grey (#9ca3af) **Returns:** a color **Example** ```vex plot(close, color = grey, width = 2) ``` ## white white (#e5e7eb) **Returns:** a color **Example** ```vex plot(close, color = white, width = 2) ``` ## aqua aqua (#06b6d4) **Returns:** a color **Example** ```vex plot(close, color = aqua, width = 2) ``` ## lime lime (#84cc16) **Returns:** a color **Example** ```vex plot(close, color = lime, width = 2) ``` ## fuchsia fuchsia (#d946ef) **Returns:** a color **Example** ```vex plot(close, color = fuchsia, width = 2) ``` ## maroon maroon (#991b1b) **Returns:** a color **Example** ```vex plot(close, color = maroon, width = 2) ``` ## navy navy (#1e3a8a) **Returns:** a color **Example** ```vex plot(close, color = navy, width = 2) ``` ## olive olive (#808000) **Returns:** a color **Example** ```vex plot(close, color = olive, width = 2) ``` ## silver silver (#cbd5e1) **Returns:** a color **Example** ```vex plot(close, color = silver, width = 2) ``` ## black black (#0a0a0a) **Returns:** a color **Example** ```vex plot(close, color = black, width = 2) ``` ## up Your candle color for up bars. **Returns:** a color **Example** ```vex plot(close, color = up, width = 2) ``` ## down Your candle color for down bars. **Returns:** a color **Example** ```vex plot(close, color = down, width = 2) ```