Backtest a strategy
run_backtestBacktests a trading rule on this server's price history: one backtesting.py Strategy class, one symbol, one bar size, in a study. Use it to test, backtest or evaluate a trading rule or strategy. Returns summary figures (return, drawdown, Sharpe, win rate, trades, a grade, whether it beat buy-and-hold) and the run's trades (when each entered and left, which exit ended it, its side, win or loss), never prices or bars; a figure may be withheld, with the reason. Waits up to 90 s, then answers status "running" with a run_id for get_result.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"study_id": "<study_id>",
"code": "<code>",
"symbol": "<symbol>",
"timeframe": "<timeframe>"
},
"name": "run_backtest"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | One backtesting.py Strategy class (describe_data has the rules). | |
| costs | No | The costs: two fractions of the trade value, or a list of costs each in its own unit. | |
| label | No | A short text shown with the run. | |
| symbol | Yes | One ticker ("QQQ", "BTCUSD", "EURUSD"). | |
| window | No | Dates ({from, to}; `to` may be "latest") or the last N days, months or years ({last: {n, unit}}). Left out: the standard history, the newest bars counted back from the newest bar, cut at the start of the hold-out. | |
| study_id | Yes | The id create_study returned. | |
| timeframe | Yes | ||
| account_usd | No | The starting account, in US dollars. | |
| stated_rules | No | Rules the trades are checked against, one per kind. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| next | No | ||
| basis | No | ||
| grade | No | ||
| lines | No | ||
| market | No | ||
| reason | No | ||
| run_id | No | ||
| status | Yes | ||
| window | No | ||
| message | No | ||
| metrics | No | ||
| notices | No | ||
| version | No | ||
| headline | No | ||
| held_out | No | ||
| settings | No | ||
| study_id | No | ||
| warnings | No | ||
| retryable | No | ||
| cost_table | No | Every cost the run charged: each component (commission, slippage, spread) with its amount, unit and basis, what it comes to per round trip, when it is taken, and whether it is yours (caller) or the market's default; the round trip summed by unit (pips on a currency pair, the costs stated in pips). Costs you send replace the market's defaults side by side: a commission replaces the default commission, a slippage or a spread replaces the default slippage, a side you leave out is charged at the market's default, and nothing is added on top. | |
| disclaimer | No | The hypothetical-performance line, on a result (a run, or a study's runs): the same text the app shows with every result. | |
| look_ahead | No | ||
| trade_list | No | The run's trades in the order they were made, a page at a time: when each entered and left (a day on daily bars; a time on the `clock` on shorter ones; "open at the end" for a trade still open when the data ended), its side, whether it won or lost after costs, and which exit ended it (a word: stop, exit rule, target, time limit…). No prices, levels, sizes or returns. | |
| buy_and_hold | No | ||
| replayed_from | No | ||
| grade_withheld | No | ||
| runs_left_today | No | ||
| hold_out_estimate | No | ||
| poll_after_seconds | No | ||
| stated_rule_breaks | No |