Get started

Rules & mistakes

A script is plain JavaScript with a few rules.
Most errors are one of the mistakes below.

Rules

  1. Line 1 is //@version=2, then call indicator({ name, overlay }) once.
  2. A Series is an array with one value per bar: index 0 is the oldest bar, the last index the current one.
  3. Never use + - * / > < on whole series: use sub, add, mul, div, gt, lt… or map(i => …).
  4. Conditions (from crossover, gt, …) are series too: never write if (cond); pass them to strategy, or read cond[i] inside map.
  5. map(i => …) runs oldest bar first; variables outside it carry state from bar to bar.
  6. Bars without a value are NaN (e.g. before a period has filled); nz() turns them into 0.
  7. No clock (Date.now, new Date()), no randomness (Math.random), no network. A bar’s time is time[i] (ms, UTC).
  8. Never look ahead (i + 1): that value isn’t known on the bar.
  9. Other timeframes, symbols and market data come from request.bars / request.data; they are NaN until they arrive.
  10. Plain code only: no import, export or async/await.

Common mistakes

WhatNot thisThis
arithmetic on whole seriesconst body = close - openconst body = sub(close, open)
a condition inside ifif (ta.crossover(fast, slow)) …strategy({ long: ta.crossover(fast, slow), longExit: ta.crossunder(fast, slow) })
comparing whole seriesconst up = close > openconst up = gt(close, open)
the last bar’s indexclose[close.length]last(close) // or close[close.length - 1]
drawing a 0–100 oscillator over the candlesplot('RSI', ta.rsi(close, 14)) // with overlay: trueindicator({ name: 'RSI', overlay: false, range: [0, 100] })

Types

What the values are.
The full types are in funnest-script.d.ts.

types
/** One number per bar, oldest first (NaN = no value). Use sub/add/gt… or map, never + - > on it. */
interface Series extends Float64Array {}
/** True (1) / false (0) per bar. */
interface Cond extends Uint8Array {}
type Source = Series | Cond | number;
/** Where a condition is expected: a Cond, or a Series (non-zero = true). */
type Bool = Cond | Series;
type Color = string;
/** A color for each bar from its index (null: none there). */
type PerBarColor = (i: number) => Color | null | undefined;
type PlotStyle = 'line' | 'step' | 'area' | 'circles' | 'cross' | 'histogram' | 'columns';
type LineStyle = 'solid' | 'dotted' | 'dashed';
type Extend = 'none' | 'left' | 'right' | 'both';
type Size = 'tiny' | 'small' | 'normal' | 'large';
type Position = 'top-left' | 'top-center' | 'top-right' | 'middle-left' | 'middle-center' | 'middle-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
type Shape = 'triangleup' | 'triangledown' | 'arrowup' | 'arrowdown' | 'circle' | 'square' | 'diamond' | 'cross' | 'xcross' | 'flag';
/** Where a mark sits: over the bar's high, under its low, at a given price, or at the pane's top / bottom edge. */
type Place = 'above' | 'below' | 'price' | 'top' | 'bottom';
/** A period in the market's time zone: a day, a week (from Monday), a month, a year. */
type Period = 'D' | 'W' | 'M' | 'Y';
/** A bar length: minutes, or 1D / 1W / 1M. */
type Timeframe = '1' | '3' | '5' | '15' | '30' | '60' | '120' | '240' | '1D' | '1W' | '1M';
/** What compute's function gets: the requested bars (open, high, low, close, volume, time, bar.count) and ta / calc working on them. */
type RequestedBars = { open: Series; high: Series; low: Series; close: Series; volume: Series; time: Series; bar: { count: number }; ta: typeof ta; add: typeof add; sub: typeof sub; mul: typeof mul; div: typeof div; max: typeof max; min: typeof min; abs: typeof abs; avg: typeof avg; gt: typeof gt; lt: typeof lt; gte: typeof gte; lte: typeof lte; eq: typeof eq; and: typeof and; or: typeof or; not: typeof not; prev: typeof prev; last: typeof last; nz: typeof nz; fixnan: typeof fixnan; map: typeof map };
type DataName = 'funding' | 'openInterest' | 'longShortRatio' | 'topLongShortRatio' | 'kimchiPremium' | 'cvd' | 'fearGreed' | 'coinbasePremium' | 'dvol' | 'stablecoins';
/** Where an input is shown: a section (group), a shared row (inline), a note (tooltip). */
type InputOptions = { group?: string; inline?: string; tooltip?: string };
/** A partial target: at a % from the entry (or a price series), close this % of the position (left out on the last one: the rest). */
type Target = { at: number | Series; close?: number };

funnest-script.d.ts · llms.txt