# 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:` is a comment listing the strategy's settings and their defaults. The comment itself declares nothing: the settings are saved with the strategy (the editor's Arguments, or `params` in `save_strategy` and `check_strategy`), and the code alone, without them, does not compile. 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

| 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

```cards
- [Values, series and na](https://docs.ftw.trade/flowscript/language): The few rules of the language.
- [Triggers and orders](https://docs.ftw.trade/flowscript/triggers-and-orders): when, while, and every order a strategy can place.
- [Indicators and models](https://docs.ftw.trade/flowscript/indicators): 40+ indicators, market profile and trained models.
- [Modules](https://docs.ftw.trade/flowscript/modules): Data, venues and protocols a strategy imports.
- [Tests and risk limits](https://docs.ftw.trade/flowscript/tests-and-limits): Prove what it does, and cage what it may do.
- [Full reference](https://docs.ftw.trade/flowscript/reference): Every function and rule.
```
