FlowScript language reference
FlowScript is FlowSense’s own small, safe language for chart indicators and signals: the grammar, every built-in with its cost, the three-valued rule for missing data, the limits and eight example scripts. FlowScript is part of Pro Max (in the app: the Strategies tab → FlowScript); this page is open to everyone.
A first script
indicator("Two averages", overlay=true)
fast = input.int(10, "Fast", min=2, max=200)
slow = input.int(50, "Slow", min=2, max=500)
plot(ema(close, fast), "Fast", color=color.green)
plot(ema(close, slow), "Slow", color=color.red)
signal(crossover(ema(close, fast), ema(close, slow)), "long", "Fast crosses above slow")
Each line is one statement. A name is set once (fast = …) and is a whole series (one value a bar) or a single number. Operators work bar by bar. A window length or a lookback (close[3]) must be a fixed whole number, or an input.int with min= and max= inside 1..500 (0..500 for a lookback), so the cost of a script is known before it runs.
Missing data: the three-valued rule
nameans “not available” (the first bars of an average, a gap). Arithmetic with na gives na.- A comparison with na (
< <= > >= == !=) is neither true nor false: it is na.not nais na. andis false if either side is false, else na if either is na, else true.oris true if either side is true, else na if either is na, else false.x == nais refused — usena(x), which is always true or false.nz(x, v)replaces na with v.signal(cond, …)marks a bar only when cond is strictly true there and strictly false on the bar before;bgcolorshades only where cond is strictly true. So no marker, shading or alert is ever driven by missing data.- Every value at a bar uses only that bar and earlier bars — a script never looks ahead.
Grammar
program = { line } ;
line = [ stmt ] [ comment ] NEWLINE ;
stmt = header | assign | tuple_assign | out_call ;
header = "indicator" "(" STRING { "," named } ")" ; (* optional; the first line; overlay=true|false *)
assign = IDENT "=" expr ; (* each name is assigned once *)
tuple_assign = "[" IDENT { "," IDENT } "]" "=" call ; (* macd and bb give three series *)
out_call = call ; (* plot, hline, bgcolor and signal stand alone *)
expr = ternary ;
ternary = binary [ "?" expr ":" expr ] ;
binary = unary { BINOP unary } ;
unary = ( "-" | "+" | "not" ) unary | postfix ;
postfix = primary { "[" expr "]" } ; (* lookback: a whole number 0..500 *)
primary = NUMBER | STRING | COLOR | "true" | "false" | "na" | call | name | "(" expr ")" ;
call = name "(" [ arg { "," arg } ] ")" ; (* at most 8 values *)
arg = [ IDENT "=" ] expr ;
name = IDENT [ "." IDENT ] ; (* a dot only after input. and color. *)
BINOP, low to high: or and == != < <= > >= + - * / %
IDENT = letter or _ then letters, digits, _ (ASCII, at most 32; a leading __ is refused)
NUMBER = digits [ . digits ] [ e [+|-] digits ] (at most 24 characters)
STRING = "…" on one line, \" and \\ only (at most 120 characters)
COLOR = #rrggbb or #rrggbbaa
comment = // to the end of the line
Keywords: indicator and or not true false na. Every keyword, built-in, series name and the names of JavaScript’s object internals (constructor, __proto__, toString, …) are reserved and cannot be assigned.
Built-ins
Cost is counted in steps a bar (the site refuses a script whose cost on 5,000 bars passes 5,000,000 steps) and slots a bar (at most 3,000,000 in all).
| Name | Use | Returns | What it does | Cost |
|---|---|---|---|---|
| sma | sma(src, len) | series | Simple moving average over len bars; na while any bar in the window is na. | 4 steps/bar, 1 slot/bar |
| ema | ema(src, len) | series | Exponential moving average, seeded with the simple average of the first len bars (as the site chart). | 4 steps/bar, 1 slot/bar |
| rma | rma(src, len) | series | Wilder moving average (alpha 1/len), seeded with the simple average. | 4 steps/bar, 1 slot/bar |
| wma | wma(src, len) | series | Linearly weighted moving average (newest bar weight len). | 4 steps/bar, 1 slot/bar |
| sum | sum(src, len) | series | Rolling sum over len bars. | 4 steps/bar, 1 slot/bar |
| rsi | rsi(src, len) | series | Relative strength index, Wilder smoothing (as the site chart). | 6 steps/bar, 3 slots/bar |
| atr | atr(len) | series | Average true range: true range smoothed with rma(len). | 6 steps/bar, 2 slots/bar |
| macd | macd(src, fast=12, slow=26, sig=9) | [series, series, series] | Returns [macd, signal, histogram]; the signal line is seeded like the site chart MACD. | 14 steps/bar, 5 slots/bar |
| bb | bb(src, len=20, mult=2) | [series, series, series] | Bollinger bands: returns [middle, upper, lower]; population standard deviation. | len+6 steps/bar, 4 slots/bar |
| stdev | stdev(src, len) | series | Population standard deviation over len bars (two-pass per window). | len+2 steps/bar, 2 slots/bar |
| highest | highest(src, len) | series | Highest value over len bars. | 4 steps/bar, 2 slots/bar |
| lowest | lowest(src, len) | series | Lowest value over len bars. | 4 steps/bar, 2 slots/bar |
| change | change(src, len=1) | series | src minus src len bars ago. | 1 steps/bar, 1 slot/bar |
| ref | ref(src, n) | same shape as the input | The value n bars ago (same as src[n]). | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| crossover | crossover(a, b) | series<bool> | True on the bar a moves above b; na unless a, b and both previous values exist. | 2 steps/bar, 1 slot/bar |
| crossunder | crossunder(a, b) | series<bool> | True on the bar a moves below b; na unless a, b and both previous values exist. | 2 steps/bar, 1 slot/bar |
| barssince | barssince(cond) | series | Bars since cond was last strictly true; na before the first time. | 2 steps/bar, 1 slot/bar |
| na | na(x) | bool (series if x is) | True where x has no value. Never na itself. | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| nz | nz(x, v=0) | same shape as the input | x, with na replaced by v (default 0; use nz(c, false) on a condition). | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| abs | abs(x) | same shape as the input | Absolute value. | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| round | round(x, digits=0) | same shape as the input | Rounded to digits decimals (half away from zero). | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| max | max(a, b) | same shape as the input | The larger of a and b (na if either is na). | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| min | min(a, b) | same shape as the input | The smaller of a and b (na if either is na). | 1 steps/bar, 1 slot/bar (0 on single numbers) |
| avg | avg(a, b, … up to 8) | same shape as the input | Average of 2 to 8 values (na if any is na). | 1 step/bar per value, 1 slot/bar (0 on single numbers) |
| plot | plot(series, title, color=, style="line"|"dashed"|"dotted"|"histogram", width=1..4) | output | Draws a line (or histogram). At most 12. | 1 step/bar (2 with a changing colour) |
| hline | hline(value, title, color=, style=) | output | A horizontal level. At most 8. | 0 |
| bgcolor | bgcolor(cond, color) | output | Shades bars where cond is strictly true (alpha at most 25%). At most 2. | 1 step/bar |
| signal | signal(cond, "long"|"short", note) | output | Marks the bar where cond turns strictly true after a strictly false bar. At most 8. | 1 step/bar |
| indicator | indicator(title, overlay=true) | header | Optional first line: the title and whether plots sit on the price chart (overlay=true) or in a pane. | 0 |
| input.int | input.int(default, title, min=, max=, step=) | int | A whole-number setting. Used as a length it needs min= and max= within 1..500. | 0 |
| input.float | input.float(default, title, min=, max=, step=) | float | A number setting. | 0 |
| input.bool | input.bool(default, title) | bool | An on/off setting. | 0 |
| input.source | input.source(close, title) | series | A choice of price series. | 1 step/bar |
| open | open | series | The bar open. | 0 |
| high | high | series | The bar high. | 0 |
| low | low | series | The bar low. | 0 |
| close | close | series | The bar close. | 0 |
| volume | volume | series | The bar volume. | 0 |
| time | time | series | Bar open time (unix seconds). | 0 |
| hl2 | hl2 | series | The bar hl2. | 1 step/bar once |
| hlc3 | hlc3 | series | The bar hlc3. | 1 step/bar once |
| ohlc4 | ohlc4 | series | The bar ohlc4. | 1 step/bar once |
| bar_index | bar_index | series | Bar number, 0 = first bar. | 1 step/bar once |
| color.green | color.green | color | #00e5a0 | 0 |
| color.red | color.red | color | #ff4567 | 0 |
| color.orange | color.orange | color | #ff9f43 | 0 |
| color.yellow | color.yellow | color | #ffcc44 | 0 |
| color.blue | color.blue | color | #5b9fff | 0 |
| color.purple | color.purple | color | #c084ff | 0 |
| color.teal | color.teal | color | #2dd4bf | 0 |
| color.gray | color.gray | color | #9aa3b2 | 0 |
| color.white | color.white | color | #ffffff | 0 |
| color.black | color.black | color | #000000 | 0 |
Limits
| Limit | Value |
|---|---|
| Script size | 20,480 bytes · 1,000 lines · 8,000 tokens · 4,000 parse nodes |
| Nesting | 32 levels · tree height 64 |
| Statements · names · inputs · values per call | 300 · 64 · 16 · 8 |
| Outputs | 12 plots · 8 levels · 2 shadings · 8 signals (the latest 500 marks each) |
| Text | name 32 · number 24 · text 120 · note 80 · title 60 characters |
| Windows and lookbacks | 1..500 · 0..500 |
| A run | 5,000 bars · 5,000,000 steps · 3,000,000 slots · 250 ms in your browser |
| Values drawn | |value| up to 1e12 (beyond: left out and flagged) |
| Your scripts | 25 kept · 1.5 MB in all · up to 6 on one chart |
| FlowGo alerts on a signal | daily or 15-minute bar · cooldown at least 1 hour · 10 FlowScript alerts |
The eight FlowSense scripts
1. EMA ribbon INDICATOR
Three EMAs on the price. The trend is cleanest when they stack in order: fast over mid over slow (up) or the reverse (down).
indicator("EMA ribbon", overlay=true)
f = input.int(8, "Fast", min=2, max=200)
m = input.int(21, "Mid", min=2, max=300)
s = input.int(50, "Slow", min=2, max=500)
plot(ema(close, f), "Fast", color=color.green)
plot(ema(close, m), "Mid", color=color.yellow)
plot(ema(close, s), "Slow", color=color.red, width=2)Checks out · 21 steps a bar · 3 settings
2. RSI bands INDICATOR
RSI in its own pane with 70 / 30 lines; the background shades while it is stretched past either line.
indicator("RSI bands", overlay=false)
len = input.int(14, "Length", min=2, max=100)
r = rsi(close, len)
plot(r, "RSI", color=color.purple)
hline(70, "Overbought", color=color.red, style="dashed")
hline(30, "Oversold", color=color.green, style="dashed")
bgcolor(r > 70 or r < 30, color.gray)Checks out · 17 steps a bar · 1 settings
3. BB squeeze SIGNAL
Bollinger band width within 5% of its 120-bar low on the bar before, then a close outside the band. A signal fires on the bar that breaks out.
indicator("BB squeeze", overlay=true)
[mid, up, lo] = bb(close, 20, 2)
w = (up - lo) / mid
tight = w <= lowest(w, 120) * 1.05
plot(up, "Upper", color=color.gray)
plot(lo, "Lower", color=color.gray)
signal(tight[1] and close > up, "long", "Squeeze breaks up")
signal(tight[1] and close < lo, "short", "Squeeze breaks down")Checks out · 50 steps a bar · 0 settings
4. Chandelier stop SIGNAL
The highest high minus a multiple of ATR, as a trailing stop for a long. A signal fires on the close under the stop.
indicator("Chandelier stop", overlay=true)
n = input.int(22, "Lookback", min=5, max=200)
k = input.float(3.0, "ATR multiple", min=0.5, max=10)
stop = highest(high, n) - k * atr(n)
plot(stop, "Long stop", color=color.orange, style="dotted")
signal(crossunder(close, stop), "short", "Closed under the stop")Checks out · 22 steps a bar · 2 settings
5. MACD turn SIGNAL
The MACD histogram coloured by its sign; a signal fires when MACD crosses up through its signal line below zero.
indicator("MACD turn", overlay=false)
[m, s, h] = macd(close, 12, 26, 9)
plot(h, "Histogram", color=h >= 0 ? color.green : color.red, style="histogram")
plot(m, "MACD", color=color.blue)
plot(s, "Signal", color=color.orange)
hline(0, "Zero", color=color.gray)
signal(crossover(m, s) and m < 0, "long", "MACD turns up below zero")Checks out · 31 steps a bar · 0 settings
6. Volume surge SIGNAL
An up bar on at least twice the 20-bar average volume (the multiple is a setting).
indicator("Volume surge", overlay=true)
mult = input.float(2.0, "Times average", min=1.2, max=10)
surge = volume > mult * sma(volume, 20) and close > open
signal(surge, "long", "Volume surge on an up bar")Checks out · 15 steps a bar · 1 settings
7. Donchian breakout SIGNAL
The prior high and low over the Channel setting (20 bars by default); a signal fires on a close above the channel high or below the channel low.
indicator("Donchian breakout", overlay=true)
len = input.int(20, "Channel", min=5, max=250)
hi = highest(high, len)[1]
lo = lowest(low, len)[1]
plot(hi, "Upper", color=color.teal)
plot(lo, "Lower", color=color.teal)
signal(close > hi, "long", "Close above the prior channel high")
signal(close < lo, "short", "Close below the prior channel low")Checks out · 22 steps a bar · 1 settings
8. Trend pullback SIGNAL
Above a rising 200 EMA, the price dips to the 21 EMA and closes back above it, with no such dip in the 3 bars before.
indicator("Trend pullback", overlay=true)
src = input.source(close, "Source")
fast = ema(src, 21)
slow = ema(src, 200)
up = src > slow and slow > slow[5]
touch = low <= fast and close > fast
plot(fast, "EMA 21", color=color.yellow)
plot(slow, "EMA 200", color=color.gray, width=2)
signal(up and touch and barssince(touch[1]) > 3, "long", "Pullback to the 21 in an uptrend")Checks out · 31 steps a bar · 1 settings
FlowScript 1.0.0 · educational — not financial advice. The bars are the market data feed’s regular-session bars. © FlowSense.