ForgeScript is AlphaBuilder's strategy language — the same strategy your block stack describes, written as text. Every block has a ForgeScript form and every ForgeScript line maps to a block, so you can move between the two freely: build in blocks, refine in script, or write script from scratch. Save (Ctrl+Enter) compiles the script back into the stack. Anything the compiler doesn't recognise comes back as a line-numbered error with a fix — nothing is ever silently skipped.
Design goals: readable by a trader who has never coded, exact enough for an engine to run, and lossless against the block view.
// comments start with // strategy("BTC-USDT-SWAP,ETH-USDT-SWAP", timeframe="4h", direction="long") universe_catalyst("gainers") // optional: seed today's movers entry_signal = crossover(close, sma(close, 200)) and volume > sma(volume, 30) * 1.5 confirm = htf.close > htf.sma(close, 200) // optional extra gate exit_signal = crossunder(close, sma(close, 50)) size = risk_percent(1) // or portfolio_percent(10) / cash(5000) size = min(size, cash(50000)) // optional position cap fill = "next_open" // or "limit" / "same_close" stop_loss_atr(2, period=14) // or stop_loss(8%) take_profit_r(2) // or take_profit(40%) partial_take(50%) trailing_stop(12%) max_hold(30) daily_stop(losers=3, loss=3%) halt_if_drawdown(15%) pyramid(max_adds=3, unless_drawdown=10%): on_gain(5%) -> add(25% of original) on_gain(10%) -> add(25% of original) on entry_signal: enter(size, fill) on exit_signal: close()
Only strategy(...) and entry_signal are required, plus at least one way out of a
trade (an exit_signal, a stop, or a take-profit). Everything else is optional — a risk line that
is absent means that feature is off.
| statement | meaning |
|---|---|
| entry_signal = … | The WHEN block — what has to become true to open a trade. Conditions joined with and (all must hold) or or (any) — not both in one signal. |
| confirm = … | The AND block — an extra filter every entry must also pass. Omit the line to clear it. |
| exit_signal = … | The EXIT block — a rule-based close, filled at the next bar's open like any signal. |
| operand | meaning |
|---|---|
| open high low close volume | The current bar of the traded instrument. |
| 42 · 1.5 · 70 | Any bare number. |
| sma(close, 200) | An indicator call — period as the numeric argument. Multi-output indicators take a suffix: bollinger(close, 20).upper, macd.signal. |
| sma(volume, 30) | Average volume — the one indicator that reads volume instead of price. |
| htf.close · htf.sma(close, 200) | The SAME instrument on the higher timeframe set in the Universe block ("Regime timeframe"). HTF values only exist after the higher bar has closed — no lookahead. |
| bench.close · bench.sma(close, 200) | ANOTHER instrument — the benchmark symbol from the Universe block. For "only trade when SPY/BTC is above its 200-day". |
| … * 1.5 | A multiplier on the right side: volume > sma(volume, 30) * 1.5 = volume 50% above normal. |
entry_signal = a and b and c // all must hold entry_signal = a or b // any may hold entry_signal = (a or b) and c // parentheses group; and binds tighter than or entry_signal = if X then A else B // when X holds, A decides; otherwise B decides entry_signal = if X then A // no else → false when X does not hold entry_signal = if htf.close > htf.sma(close, 200) then close > sma(close, 50) else close > sma(close, 200) // looser entry in a bull regime, stricter otherwise
Any of these forms nest to any depth. if reads as a rule that chooses which rule applies —
the classic use is a regime switch: one entry rule in a bull market, a different one otherwise. Nested
logic shows in the Blocks view as an IF / GROUP card; edit it here in code.
| form | meaning |
|---|---|
| a > b · a < b · a >= b · a <= b | Level comparisons, evaluated on every bar. |
| crossover(a, b) | True only on the bar where a moves from at-or-below to above b. |
| crossunder(a, b) | The mirror — from at-or-above to below. |
| a > a[1] | Rising — the value is higher than on the previous bar. a < a[1] is falling. |
Callable in any operand position. Outputs lists the suffixes for multi-output indicators.
| statement | meaning |
|---|---|
| size = portfolio_percent(10) | Commit 10% of the (reinvested) portfolio per trade. |
| size = risk_percent(1) | Size the position so that a stop-out loses exactly 1% of the portfolio. Requires a stop — the stop distance defines the size. |
| size = cash(5000) | A fixed dollar amount per trade. |
| size = min(size, cash(50000)) | Cap any position at $50,000 regardless of the rule above. |
| statement | meaning |
|---|---|
| fill = "next_open" | Default and honest: the signal evaluates at a bar's close and fills at the NEXT bar's open. |
| fill = "limit" | A limit order below the signal close (offset and expiry set in the HOW block). Only fills if a later bar trades through it. |
| fill = "same_close" | Fills at the very close that generated the signal. Optimistic — results flag it. |
| statement | meaning |
|---|---|
| stop_loss(8%) | Fixed stop 8% from the average entry. |
| stop_loss_atr(2, period=14) | Volatility stop: 2× the 14-bar ATR at entry. Wide in wild markets, tight in quiet ones. |
| trailing_stop(12%) | Trails 12% behind the best price seen since entry. Combines with a fixed/ATR stop — whichever is tighter is live. |
| take_profit(40%) | Fixed target 40% from entry. |
| take_profit_r(2) | R-multiple target: 2× the stop distance. Needs a stop, because R is the stop distance. |
| partial_take(50%) | At the target, close 50% and let the rest run on the stop/trail/exit rule. |
| max_hold(30) | Force out after 30 bars if nothing has resolved — dead trades tie up capital. |
| daily_stop(losers=3, loss=3%) | Circuit breakers: stop entering for the rest of the UTC day after 3 losing trades, or once the day is −3% from where it started. Either part may be used alone. |
| halt_if_drawdown(15%) | Portfolio kill-switch: once peak-to-trough drawdown reaches 15%, close everything and take no trades for the rest of the test. |
pyramid(max_adds=3, unless_drawdown=10%):
on_gain(5%) -> add(25% of original)
on_gain(10%) -> add(25% of original)
Each rung fires once, in order, when the position's gain from the average entry reaches the threshold.
Every add re-anchors the average entry — the stop and target move with it, which is both the point and the
risk. unless_drawdown vetoes adds while the portfolio is down more than that from its peak.
No lookahead. Signals are evaluated on bar close and fill at the next bar's open. Higher-timeframe
values become visible only after the higher bar closes. Breakout levels (highest(close, 20))
exclude the current bar — today can break yesterday's range, not its own.
Worst case when ambiguous. If one bar's range touches both the stop and the target, the stop is taken, and the count of such bars is reported with every result.
Warm-up. No trades until every referenced indicator has a value — a 200-bar SMA means the first 200 bars are observation only.
Multi-symbol universes. Capital splits equally into per-symbol sleeves that cannot borrow from each other. Catalyst symbols (today's gainers/losers) are today's list tested backwards — survivorship bias by construction, labelled on every result.
End of data. A position still open when the period ends is closed at the last bar and flagged — never silently dropped.