Download the exact captured file
---
name: backtest
description: >-
Backtests a rule-based trading strategy on Chord's stored candles: supertrend, EMA cross, RSI mean reversion,
Donchian breakout or buy and hold, long, short or both, with optional stop-loss, take-profit, leverage and fee.
Covers any Hyperliquid perp (BTC, ETH, SOL, HYPE, kPEPE...) and FX and metals from Dukascopy (EURUSD, USDJPY, gold,
silver and more), on 1m to 1d bars. Returns total return, CAGR, max drawdown, Sharpe, win rate, profit factor and
exposure next to buy and hold, the assumptions, a trade summary or every trade, and the equity curve as CSV. Use
when the user asks to backtest or test a strategy or indicator, how supertrend or an EMA cross did on a coin or a
currency, or to compare a rule with holding. Not for stocks or on-chain tokens without a perp, live signals, or
advice on what to trade. Read only; never trades.
license: MIT
compatibility: Needs python3 and HTTPS to api.getchord.xyz. Set CHORD_API_KEY (free at chordsi.xyz/me).
metadata:
author: Chord (getchord.xyz)
homepage: https://chordsi.xyz/s/backtest
version: "1.0.0"
---
# Backtest
One bar-close engine over two local stores: Hyperliquid perp candles and Dukascopy FX / metals candles. The signal is
computed on a closed bar and filled at the next bar's open; one position at a time, all in; the fee is charged on
entry and on exit. It is simple on purpose and says so in every answer.
## Run it
```bash
python3 scripts/backtest.py [SYMBOL] [--strategy S] [--interval I] [--days N] [--side both|long|short]
[strategy params] [--fee-bps X] [--stop-pct X] [--take-pct X] [--leverage X] [--trades] [--csv PATH]
```
- `SYMBOL` (default BTC): any Hyperliquid perp id (`BTC`, `ETH`, `SOL`, `HYPE`, `kPEPE`; `BTCUSDT` and `BTC-PERP` are
read as BTC), or `EURUSD`, `GBPUSD`, `USDJPY`, `USDCHF`, `AUDUSD`, `USDCAD`, `USDSEK`, `NZDUSD`, `EURJPY`, `GBPJPY`,
`EURGBP`, `XAUUSD` (or `gold`), `XAGUSD` (or `silver`). No stocks.
- `--strategy` and its params (defaults in brackets; a param of another strategy is refused):
- `supertrend` (default): long above the line, short below. `--atr` [10] bars, `--mult` [3].
- `ema_cross`: long while the fast EMA is above the slow one. `--fast` [20], `--slow` [50].
- `rsi`: long under `--lower` [30], short over `--upper` [70], flat again when RSI crosses 50. `--length` [14].
- `donchian`: long on a close above the last `--length` [20] bars' high, short below their low.
- `buy_hold`: long from the first bar to the last (the baseline).
- `--interval`: `1m`, `5m`, `15m`, `30m`, `1h` (default), `4h`, `1d`. `--days`: 1 to 3650 back from now (default 365).
- `--side`: `both` (default), `long` (shorts become flat) or `short`.
- `--fee-bps`: per side, default 4.5 on perps and 1 on FX. `--stop-pct` / `--take-pct`: % from the entry price.
`--leverage`: 0.1 to 20 (default 1).
- `--trades`: every trade the API returns (the newest 200) in place of the summary. `--csv PATH`: the equity curve.
How far back the stores reach (December 2026; they grow every day, and `window` in the answer is the truth):
Hyperliquid 1d from August 2020 for BTC and ETH (later coins from their listing), 4h from mid 2024, 1h about 9 months,
15m and 30m about 4 months, 1m and 5m about 2.5 months. FX: 1h and up from December 2016, 1m to 30m about 15 months.
A test needs at least 50 bars.
Example: `python3 scripts/backtest.py BTC --strategy supertrend --interval 4h --days 730`
## Read the answer
- `window`: the bars actually used, `fromUtc` to `toUtc`. A `note` says when the store held less than asked.
- `strategyResult`: `totalReturnPct`, `cagrPct` (null under three months), `maxDrawdownPct` (on the marked equity),
`sharpe` (per-bar returns, annualised, no risk-free rate), `trades`, `winRatePct`, `profitFactor` (gross wins over
gross losses), `avgWinPct` / `avgLossPct` per trade, `exposurePct` (share of bars in a position), `feesPaidPct`.
- `buyAndHold`: the same window held long, `totalReturnPct` and `maxDrawdownPct`. `versusBuyAndHoldPts` = strategy
return minus buy and hold, in percentage points.
- `assumptions`: fill at next open, fee per side, no funding, no spread or slippage, stop assumed first when a bar
touches both levels. Always pass them on.
- `tradeSummary`: per side trades, win rate and average return; exit reasons (`signal`, `stop`, `take`, `end` = still
open at the last bar); average hours held; best and worst trade; the last five. Its `basis` says when it covers only
the newest 200 trades (the metrics always count all).
- `csv`: the file written (about 300 points: time, strategy equity and buy and hold equity, both from 1.0).
## How to answer the user
1. Say what was tested in one line: symbol, strategy and params, interval, side, the real window.
2. Give the return and the max drawdown next to buy and hold for the same window, then trades, win rate and Sharpe.
3. Always state the assumptions: fees as used, no funding (perps pay or receive funding every hour), no slippage.
Funding can turn a small edge on a long-held perp position into a loss.
4. One window and one parameter set prove little; offer to test another window, interval or symbol. A good past
result is not a prediction. Never say the strategy will make money or tell the person to trade it.
## Cost
One call per test. 300 calls a month are free; then $0.006 a call from your ChordSI credit. If the script exits with
code 4, show the person the message: their credit is empty or a limit was reached, and chordsi.xyz/me is where they top
up. Exit 2 with `INVALID_PARAMS` means the symbol, interval or window cannot be tested; the message says why.
## Chord endpoints it calls
- `GET /v1/backtest?symbol=&interval=&strategy=&days=&side=&atr=&mult=&fast=&slow=&length=&lower=&upper=&feeBps=&stopPct=&takePct=&leverage=`