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 (a while that 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:

  1. // params: names the strategy's settings. Each becomes a constant you can change per run and search in an optimization.
  2. bars.close(market, interval) is the series of closing prices. ftw.trade supplies market and interval from the run.
  3. ema(close, fastLen) is an exponential moving average. Every const is a series, one value per bar, and fast[1] would be the previous bar's value.
  4. metric("fast", fast) publishes the value each bar, so you can chart it and see why a trade did or didn't happen.
  5. when (…) { … } fires on the bar its condition turns true, once per crossing.
  6. paper.buy({ quote: size }) spends size at the bar's close. On a project's wallets the same order trades them.

Where Flowscript runs#

WhereWhat
StrategiesA program per bar over a market, backtested and run
SourcesA program per event over a stream, websocket, API or contract event, publishing typed events of its own
WorkflowsA strategy as a step or stage, handing its metrics to later steps
Your machineftw-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#