Every field of a strategy's JSON document — what it means, the values it takes, its default and what the validator refuses — with whole examples that validate.
A strategy is one JSON document. The editor (Laboratory › Strategies › +) opens on a new one; Validate checks it field by field and Save as the next version files it. An agent or a program sends the same document to strategy_validate and strategy_save.
Three rules hold for the whole file:
- Strict reading. A key Bentrix does not know, or a value it does not know, makes the whole document unreadable rather than half-read — a typo is never silently ignored.
- Optional means
null. A field marked optional below may benullor left out; it is written back asnull. - Every refusal names its field. Validate lists them all at once —
exits.first_target,sizing.mode— with the reason, so one pass fixes everything.
01The new document the editor opens on
{
"schema": 1,
"name": "",
"version": 1,
"made_ms": 0,
"made_by": "person",
"markets": [],
"interval": "H1",
"studies": {},
"rules": {},
"entry": {"kind": "market", "cancel_on_opposite": false},
"exits": {},
"sizing": {"mode": "lots", "value": 0.01},
"sessions": {"desks": []},
"limits": {"one_position_per_market": true},
"notes": ""
}
It does not run yet, on purpose: it has no name, no market and no rule. Fill those three and it does.
02The header
| Field | Type | Meaning |
|---|---|---|
schema | number | the document's form: 1 for a document of rules, 2 for a script (below). Anything else is refused |
name | text | the strategy's name — its identity. The file name is made from it, so it must keep at least one letter or digit |
version | number | from 1. Saving files the next version itself; a saved version never changes |
made_ms | number | when this version was written, in milliseconds since 1970 (UTC), as its writer records it; 0 is accepted |
made_by | person or agent | who wrote this version |
markets | list of text | the markets it trades, by the symbol the workbench holds them under — at least one, none twice |
interval | text | the interval the rules are read at — each candle's close: M1, M5, M15, H1, H4, D1 … or a custom one such as M90 |
notes | text | yours; never read by Bentrix |
03studies — the indicators the rules read
A map from a key of your choice to an indicator, written exactly as a chart files it — each indicator's page gives its entry under From an agent or a program.
"studies": {
"fast": {"kind": "ema", "convention": "talib", "applied": "close", "settings": {"shape": "period", "period": 20}},
"slow": {"kind": "ema", "convention": "talib", "applied": "close", "settings": {"shape": "period", "period": 50}},
"atr": {"kind": "atr", "convention": "mt5", "applied": "close", "settings": {"shape": "period", "period": 14}}
}
- A key is lower-case letters, digits and
_, starting with a letter, at most 32 characters.priceis reserved. - A study may have a timeframe of its own (
"timeframe": "H4"), never finer than the document'sinterval; it is read at its last closed candle —"wait_close": falseis refused. - Refused too: an indicator computed on another indicator's line, one computed on several prices at once, and the few that look at later candles — Fractals, ZigZag and Ichimoku's Chikou line.
04rules — when to open and when to close
Four slots, each a condition or null:
| Slot | When it holds at a candle's close |
|---|---|
buy | open a long |
sell | open a short |
close_buys | close the longs this run opened |
close_sells | close the shorts this run opened |
At least one of buy and sell must be written. A condition is built from these nodes:
| Node | Holds when | Example |
|---|---|---|
cond | a condition the indicator itself declares holds: <key>:<token> | {"cond": "rsi:level.cross_up:30"} |
compare | one value against another: above, below, cross_up or cross_down | {"compare": {"cross_up": ["fast", "slow"]}} |
all | every child holds | {"all": [ … , … ]} |
any | at least one child holds | {"any": [ … , … ]} |
not | its child does not | {"not": {"cond": "adx:level.below:20"}} |
within | its child held at one of the last bars candles, this one included | {"within": {"bars": 3, "of": { … }}} |
since | its child has held at some candle so far | {"since": {"of": { … }}} |
bars_in_trade | this run's position on the slot's side was opened at least n candles ago — close slots only | {"bars_in_trade": 48} |
A value in a compare is one of:
- a number —
30,1.085; - a price of the candle —
price.open,price.high,price.low,price.close; - an indicator's line — its key alone for its main line (
rsi), or<key>.<line>(macd.signal,adx.plus_di,bollinger.band_mid).
Comparing two numbers is refused — it decides nothing. Which tokens and lines each indicator offers is on its own page, under In a strategy. Two limits worth knowing: an indicator with two lines of one name — Bollinger's upper and lower bands are both a band_edge — cannot be named line by line, so use its declared conditions (bollinger:band.cross_above_upper); and a slot nests at most 8 levels deep and holds at most 64 leaves.
all of two or more events (crossings, turns) is accepted with a warning: events must happen on the very same candle, which is rarer than it looks — within usually says what was meant.
05entry — how the order enters
| Field | Type | Meaning |
|---|---|---|
entry.kind | market, limit, stop, stop_limit | a market order at the close, or a pending one |
entry.offset | distance | a pending order's distance from entry.from — toward the better price for a limit, the worse for a stop. Required for a pending order, refused for a market one |
entry.from | text | what the offset is measured from: close, or an indicator's line (fast, bollinger.band_mid). Required for a pending order, refused for a market one |
entry.limit_offset | distance | a stop-limit's limit, this far from its stop price. Required for stop_limit only, refused otherwise |
entry.expires_candles | number, optional | a pending order not filled is cancelled at the close this many candles later; at least 1. Pending orders only |
entry.cancel_on_opposite | true or false | a pending buy is cancelled when sell holds, and the reverse. true for pending orders only |
A distance is one of three objects:
| Distance | Means | Example |
|---|---|---|
points | points of the market's own point | {"points": 150} |
atr | a multiple of an ATR study's main line — study names its key | {"atr": {"study": "atr", "times": 1.5}} |
r | a multiple of the stop's distance — for targets only, and only with a stop | {"r": 2} |
Every number in a distance is positive.
06exits — how a position ends
All optional; {} is a position closed by the close slots alone.
| Field | Type | Meaning |
|---|---|---|
exits.stop | distance | the stop, set on the order at the venue. Not in r |
exits.target | distance | the target, set on the order at the venue |
exits.first_target | object | close part of the position early: exits.first_target.at (a distance, r allowed) and exits.first_target.close_pct (more than 0 and less than 100). Needs a target |
exits.breakeven | object | once the quote is exits.breakeven.after_points (at least 1) in profit, move the stop to the open plus exits.breakeven.plus_points — once |
exits.trail | object | a trailing stop exits.trail.points behind the quote, moved by exits.trail.step points or more; both positive |
07sizing — how large
| Field | Type | Meaning |
|---|---|---|
sizing.mode | lots, risk_pct, risk_money | a fixed volume; a per cent of the balance at risk at the stop; an amount of the account's money at risk at the stop |
sizing.value | number | the lots, the per cent (more than 0, at most 100) or the money; always positive |
risk_pct and risk_money need exits.stop — the risk is measured at the stop.
08sessions — when it may enter
All of it limits entries only: exits, breakeven and the trail run at any hour.
| Field | Type | Meaning |
|---|---|---|
sessions.window | object, optional | enter only from sessions.window.from to sessions.window.to (HH:MM on the workbench's clock) on sessions.window.days (mon … sun, at least one, none twice). A to before from crosses midnight |
sessions.desks | list | enter only while one of these desks is open: sydney, tokyo, london, new_york. Empty: any time |
sessions.flat_at | HH:MM, optional | close everything this run holds at this time each day |
sessions.news | object, optional | no entry around economic releases, from your MetaTrader's calendar: sessions.news.before_min minutes before to sessions.news.after_min after (each at most 1,440, together more than 0); only releases rated at least sessions.news.min_importance (0 none to 3 high; absent: all); only sessions.news.events when any are named; of sessions.news.currencies (three letters each), or the market's own when empty |
09limits
| Field | Type | Meaning |
|---|---|---|
limits.one_position_per_market | true or false | at most one position per market |
limits.max_open | number, optional | positions and pending orders this run holds across its markets, at least 1 |
limits.per_day | object, optional | limits.per_day.max_trades: entries a day; limits.per_day.stop_after_losses: after this many losing deals in a day, no more entries that day. Each at least 1 |
limits.max_bars | number, optional | how many candles of the rule interval the run lasts; absent, the workbench's days |
10A strategy written as a script
A document of schema 2 carries an Assay script in place of studies, rules, entry and exits (which keep their empty forms); sessions and limits still apply.
| Field | Meaning |
|---|---|
script | the script itself |
script.language | assay |
script.source | the script's whole text, beginning //@version=5 or 6 |
script.inputs | its inputs by title, where they are not the script's defaults (what a batch varies) |
The Assay editor's Save as a strategy writes this form for you.
11Examples
Each of these is checked with strategy_validate against a running Bentrix whenever the Docs are built. Copy one into the editor, set markets to a market of your workbench, and Validate.
A moving-average cross, a stop of 1.5 ATR, a target of twice the stop, 1 % of the balance at risk:
{
"schema": 1,
"name": "ema cross",
"version": 1,
"made_ms": 0,
"made_by": "person",
"markets": [
"EURUSD"
],
"interval": "H1",
"studies": {
"fast": {
"kind": "ema",
"convention": "talib",
"applied": "close",
"settings": {
"shape": "period",
"period": 20
}
},
"slow": {
"kind": "ema",
"convention": "talib",
"applied": "close",
"settings": {
"shape": "period",
"period": 50
}
},
"atr": {
"kind": "atr",
"convention": "mt5",
"applied": "close",
"settings": {
"shape": "period",
"period": 14
}
}
},
"rules": {
"buy": {
"compare": {
"cross_up": [
"fast",
"slow"
]
}
},
"sell": {
"compare": {
"cross_down": [
"fast",
"slow"
]
}
},
"close_buys": null,
"close_sells": null
},
"entry": {
"kind": "market",
"offset": null,
"from": null,
"limit_offset": null,
"expires_candles": null,
"cancel_on_opposite": false
},
"exits": {
"stop": {
"atr": {
"study": "atr",
"times": 1.5
}
},
"target": {
"r": 2
},
"first_target": null,
"breakeven": null,
"trail": null
},
"sizing": {
"mode": "risk_pct",
"value": 1
},
"sessions": {
"window": null,
"desks": [],
"flat_at": null
},
"limits": {
"one_position_per_market": true,
"max_open": null,
"per_day": null,
"max_bars": null
},
"notes": ""
}
A pullback bought with a limit order half an ATR under the close, only when the RSI recovered from 30 within the last three candles and the price is above its 200 average; the order lapses after five candles:
{
"schema": 1,
"name": "rsi pullback limit",
"version": 1,
"made_ms": 0,
"made_by": "person",
"markets": [
"EURUSD"
],
"interval": "H1",
"studies": {
"rsi": {
"kind": "rsi",
"convention": "talib",
"applied": "close",
"settings": {
"shape": "period",
"period": 14
}
},
"trend": {
"kind": "ema",
"convention": "talib",
"applied": "close",
"settings": {
"shape": "period",
"period": 200
}
},
"atr": {
"kind": "atr",
"convention": "mt5",
"applied": "close",
"settings": {
"shape": "period",
"period": 14
}
}
},
"rules": {
"buy": {
"all": [
{
"within": {
"bars": 3,
"of": {
"cond": "rsi:level.cross_up:30"
}
}
},
{
"compare": {
"above": [
"price.close",
"trend"
]
}
}
]
},
"sell": null,
"close_buys": {
"any": [
{
"compare": {
"cross_down": [
"rsi",
50
]
}
},
{
"bars_in_trade": 24
}
]
},
"close_sells": null
},
"entry": {
"kind": "limit",
"offset": {
"atr": {
"study": "atr",
"times": 0.5
}
},
"from": "close",
"limit_offset": null,
"expires_candles": 5,
"cancel_on_opposite": true
},
"exits": {
"stop": {
"atr": {
"study": "atr",
"times": 2
}
},
"target": {
"r": 1.5
},
"first_target": null,
"breakeven": null,
"trail": null
},
"sizing": {
"mode": "lots",
"value": 0.1
},
"sessions": {
"window": null,
"desks": [],
"flat_at": null
},
"limits": {
"one_position_per_market": true,
"max_open": null,
"per_day": null,
"max_bars": null
},
"notes": ""
}
A London-hours breakout of the Donchian channel, half closed at the first target, the stop to breakeven after 150 points, then trailed; no entry from 30 minutes before to 30 after a high-impact release, at most three trades a day and none after two losses, everything closed at 20:00:
{
"schema": 1,
"name": "london breakout",
"version": 1,
"made_ms": 0,
"made_by": "person",
"markets": [
"GBPUSD"
],
"interval": "M15",
"studies": {
"channel": {
"kind": "donchian",
"convention": "custom",
"applied": "close",
"settings": {
"shape": "period",
"period": 20
}
}
},
"rules": {
"buy": {
"cond": "channel:band.cross_above_upper"
},
"sell": {
"cond": "channel:band.cross_below_lower"
},
"close_buys": {
"cond": "channel:band.mid_cross_down"
},
"close_sells": {
"cond": "channel:band.mid_cross_up"
}
},
"entry": {
"kind": "market",
"offset": null,
"from": null,
"limit_offset": null,
"expires_candles": null,
"cancel_on_opposite": false
},
"exits": {
"stop": {
"points": 300
},
"target": {
"points": 600
},
"first_target": {
"at": {
"points": 300
},
"close_pct": 50
},
"breakeven": {
"after_points": 150,
"plus_points": 10
},
"trail": {
"points": 250,
"step": 20
}
},
"sizing": {
"mode": "risk_money",
"value": 50
},
"sessions": {
"window": {
"from": "08:00",
"to": "16:00",
"days": [
"mon",
"tue",
"wed",
"thu",
"fri"
]
},
"desks": [
"london"
],
"flat_at": "20:00",
"news": {
"min_importance": 3,
"events": [],
"currencies": [],
"before_min": 30,
"after_min": 30
}
},
"limits": {
"one_position_per_market": true,
"max_open": 1,
"per_day": {
"max_trades": 3,
"stop_after_losses": 2
},
"max_bars": null
},
"notes": "London breakout, M15."
}
A script strategy, schema 2:
{
"schema": 2,
"name": "sma cross script",
"version": 1,
"made_ms": 0,
"made_by": "person",
"markets": [
"EURUSD"
],
"interval": "H1",
"studies": {},
"rules": {
"buy": null,
"sell": null,
"close_buys": null,
"close_sells": null
},
"entry": {
"kind": "market",
"offset": null,
"from": null,
"limit_offset": null,
"expires_candles": null,
"cancel_on_opposite": false
},
"exits": {
"stop": null,
"target": null,
"first_target": null,
"breakeven": null,
"trail": null
},
"sizing": {
"mode": "lots",
"value": 0.1
},
"sessions": {
"window": null,
"desks": [],
"flat_at": null
},
"limits": {
"one_position_per_market": true,
"max_open": null,
"per_day": null,
"max_bars": null
},
"notes": "",
"script": {
"language": "assay",
"source": "//@version=5\nstrategy(\"SMA cross\")\nfast = ta.sma(close, 10)\nslow = ta.sma(close, 30)\nif ta.crossover(fast, slow)\n strategy.entry(\"L\", strategy.long)\nif ta.crossunder(fast, slow)\n strategy.close(\"L\")\n"
}
}
12From an agent or a program
strategy_validate takes {"document": …} and answers runnable (true or false), refusals (each a field and why) and warnings; it saves nothing. strategy_save validates and files the next version. The SDK guide the strategy engine runs a document end to end in nine languages — validate, save, run on a workbench, read the report.