Flowscript
Flowscript in five minutes
Flowscript is ftw.trade's language for strategies and sources. Small, deterministic and made for AIs to write, with errors that name the line and the fix. Read a whole strategy, line by line.
Flowscript is a small language for trading. A program reads time series, computes values over them, and fires actions when conditions turn true. It runs once per closed bar.
- It always ends. No loops, no recursion and no mutation, so a step can't hang or run away with your cash.
- It's deterministic. The same bars give the same decisions, so a backtest and a live run agree, and ftw.trade can check that they keep agreeing.
- It explains its errors. Every compile error has a stable code (
FS0412), names its line and column, and suggests a fix. Warnings flag lines that probably don't do what they seem to (awhilethat buys every bar, a condition that is never true). - It's made for AIs. A model writes it, checks it, reads the errors and revises, without a human in the loop.
A whole strategy#
Flowscript
// params: fastLen=12, slowLen=26, size=100
const close = bars.close(market, interval);
const fast = ema(close, fastLen);
const slow = ema(close, slowLen);
metric("fast", fast);
metric("slow", slow);
when (crossover(fast, slow) && !inPosition()) { paper.buy({ quote: size }); }
when (inPosition() && crossunder(fast, slow)) { paper.sell({}); }Line by line:
// params:names the strategy's settings. Each becomes a constant you can change per run and search in an optimization.bars.close(market, interval)is the series of closing prices. ftw.trade suppliesmarketandintervalfrom the run.ema(close, fastLen)is an exponential moving average. Everyconstis a series, one value per bar, andfast[1]would be the previous bar's value.metric("fast", fast)publishes the value each bar, so you can chart it and see why a trade did or didn't happen.when (…) { … }fires on the bar its condition turns true, once per crossing.paper.buy({ quote: size })spendssizeat the bar's close. On a project's wallets the same order trades them.
Where Flowscript runs#
| Where | What |
|---|---|
| Strategies | A program per bar over a market, backtested and run |
| Sources | A program per event over a stream, websocket, API or contract event, publishing typed events of its own |
| Workflows | A strategy as a step or stage, handing its metrics to later steps |
| Your machine | ftw-trade local compiles, tests and backtests with the same compiler |
Write it with the editor or your AI#
The Strategies page has an editor with completions, inline errors and a backtest beside it. Or ask the assistant, Claude or ChatGPT: they check each draft with check_strategy (compile only, nothing saved) and fix what it names. Pine Script strategies translate automatically.
Learn more#
Values, series and naThe few rules of the language.→Triggers and orderswhen, while, and every order a strategy can place.→Indicators and models40+ indicators, market profile and trained models.→ModulesData, venues and protocols a strategy imports.→Tests and risk limitsProve what it does, and cage what it may do.→Full referenceEvery function and rule.→