시작하기

규칙과 실수

스크립트는 자바스크립트 그대로예요.
규칙 몇 가지만 지키면 돼요.
오류는 대부분 아래 실수 중 하나예요.

규칙

  1. 첫 줄은 //@version=2, 그다음 indicator({ name, overlay })를 한 번 불러요.
  2. 시리즈는 봉마다 값이 하나씩 있는 배열이에요. 0번이 가장 오래된 봉, 마지막이 지금 봉이에요.
  3. 시리즈끼리 + - * / > < 를 쓰면 오류예요. sub, add, mul, div, gt, lt 같은 함수나 map(i => …)를 쓰세요.
  4. 조건(crossover, gt 등의 결과)도 시리즈예요. if (조건) 처럼 쓰지 말고 strategy·plotshape·map 안에서 쓰세요.
  5. map(i => …)은 오래된 봉부터 차례로 불려요. 바깥 변수로 봉 사이의 상태를 들고 갈 수 있어요.
  6. 값이 없는 봉은 NaN이에요(기간보다 앞쪽 봉 등). nz()로 0으로 바꿀 수 있어요.
  7. 현재 시각(Date.now, new Date()), 난수(Math.random), 네트워크는 쓸 수 없어요. 봉 시각은 time[i]예요.
  8. 다음 봉 데이터를 미리 보는 계산(i + 1)은 하지 마세요. 실제로는 알 수 없는 값이에요.
  9. 다른 시간봉 · 종목 · 시장 데이터는 request.bars · request.data로 받아요. 처음 받는 동안은 NaN이에요.
  10. 코드만 쓰세요. import, export, async/await는 없어요.

자주 하는 실수

무엇이렇게 말고이렇게
시리즈끼리 바로 계산const body = close - openconst body = sub(close, open)
조건을 if에 넣기if (ta.crossover(fast, slow)) …strategy({ long: ta.crossover(fast, slow), longExit: ta.crossunder(fast, slow) })
시리즈 비교const up = close > openconst up = gt(close, open)
마지막 봉 인덱스close[close.length]last(close) // or close[close.length - 1]
0~100 지표를 캔들 위에 그리기plot('RSI', ta.rsi(close, 14)) // with overlay: trueindicator({ name: 'RSI', overlay: false, range: [0, 100] })

타입

값의 종류예요.
전체 타입은 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