Backtests
How backtests fill orders, charge costs, avoid reading the future, and judge a run, a scan, a walk forward and rolling windows.
A backtest tests a trading rule on a listing's history. The backtest panel and the agent's scripts run it through one engine, on your computer and on the bars Solyx already reads. Rules are data rather than code, so the engine, not the script, decides every fill.
No look-ahead
Read the rule at a bar's close. A crossing means the previous bar was at
or below and this bar is above. highest and lowest look only at the bars
before the current one.
Act at the next bar's open, moved against you by the slippage. On bars shorter than a day, a session's first bar fills after the gap from the close before.
A rule can never see the bar it trades on.
Prices
Indicators, stops and the buy-and-hold comparison read adjusted prices, rescaled to each bar's own terms, so a dividend never looks like a crash. Orders fill at the prices that actually traded, and shares held are scaled on a split or ex-dividend day, the dividend bought back in less the tax withheld from it; stops and buy and hold take that tax off too. Bars shorter than a day take each session's reference from the daily bars.
A strategy's own lines
A strategy you wrote hands the engine lines of its own, worked out from the run's bars adjusted on the first bar's terms, so a later split or dividend changes no earlier value. A bar's own prices and the indicators are on other terms, so a rule compares such a line only with another of the strategy's lines or with a value. The engine still fills every order at prices as traded and charges every cost.
To tell whether the code read later bars, the engine runs its first set of numbers again over the first fifth, two fifths, three and four of the bars, and compares each line with the whole run's. Where one differs, the run warns from the first bar it does.
Fills
- Long only. A buy spends the cash on whole shares, keeping room for its commission; a sale sells everything.
- Slippage is a number of ticks from the open, 1 unless set otherwise, up to 20.
- Locked limits. A Taiwan daily bar whose high equals its low and that closed above its reference is taken as locked limit-up, so no buy fills; one that closed below it is locked limit-down and fills no sale. A shorter bar is locked when it traded at one price, its session's limit.
Costs
| Cost | Default |
|---|---|
| Commission | 0.1425% each way, at least NT$20 |
| Tax on sale | 0.3% for stocks; 0.1% for codes starting 00, 01 or 02 (ETFs, REITs and ETNs) |
| Slippage | 1 tick |
A holding's shares are multiplied by on the day it goes ex. Tax is withheld from no more cash than the reference made up for, so a payout the source's references miss is neither bought back in nor taxed. Commission and tax rates can be set up to 1%, and the share withheld up to all of it. The starting capital is 1,000,000 unless set otherwise.
What it reports
A run can be split at a date into an in-sample and an out-of-sample stretch, so a rule tuned on one is judged on the other. Each stretch, and the whole span, reports:
| Figure | Meaning |
|---|---|
| Return | The end value against the start |
| Buy and hold | The same stretch held throughout |
| Annual return | ; none for runs under 28 days |
| Max drawdown | The deepest fall from a peak |
| Holding's drawdown | The deepest fall holding would have taken, from the prior close |
| Sharpe ratio | The account's return per unit of its swings, a year's worth |
| p-value | How often random orderings of the bars did as well as the rule's timing |
| Trades | Closed trades; a position still open is not counted |
| Win rate | The share of closed trades that gained |
| Payoff ratio | The average win against the average loss |
| Time held | The share of bars spent holding |
The account is valued at each session's close, so a run on minute bars grows with its sessions, not its bars. Fewer than 20 closed trades in a stretch say little, and the panel and the agent both say so.
Sharpe ratio
The mean of the account's returns from one session's close to the next, over their standard deviation, scaled to a year at the pace the stretch traded:
where is the number of session returns and days the stretch's calendar days. There is none for a stretch under 28 days, or one whose value never moved.
p-value
The p-value tests the rule's timing alone. Each bar's two moves, from the close before to its open and from its open to its close, are shuffled into 1,000 random orders, while the run's holding and costs stay where they were. Each ordering is scored, as the run is, by the mean over the deviation of its bars' returns:
Below 0.05, the timing beat chance. The orderings are seeded, so a run gives the same p-value each time. A stretch that held nothing has none.
Scans
A scan runs every setup of a grid, each on its own over the same bars, and scores each by its in-sample Sharpe ratio, or its whole span's where there is no split. It picks the plateau: the setup whose neighbourhood, itself and the setups beside it, diagonals included, that have a Sharpe ratio, has the highest mean. A setup that stands out from weak neighbours is more likely luck than one in a field of good ones.
A scan runs at most 400 setups, reading at most 64 series between them, and each way of the panel's grid takes 1 to 20 values.
Walk forward
A walk forward picks on the past alone. With training and test sessions, window trains on sessions to and tests on the after them; the last test runs to the newest bar.
- Training is two fifths of the sessions unless set, and testing a quarter of the training.
- Each window picks its grid's plateau by Sharpe ratio over its training alone.
- The tests run as one record, each under its window's pick, and a holding carries over from one test to the next. That record is the out-of-sample stretch.
- At most 100 windows: test more sessions at a time for a longer span.
Well under 1 says the picks were fitted to their training.
Rolling windows
Rolling windows run one strategy from many starts. With windows of months that start every months:
- Starts. The first window starts on the first session of the month after the one the bars start in, so a close comes before it. Each next one starts months later. A month without a session starts none.
- Ends. A window runs to its last session before the month months after its start. Only windows the bars run past are taken, so each is as long as the next.
- Each window holds nothing at the close before its first session, where its rules are first read, so its first fill can come at that session's open. Its figures and buying and holding's are measured from that close.
- Indicators are worked out once over every bar read, not from each window's start, so a window's rules read what a single run over all the bars reads. Only the earliest windows can start while a long indicator is still warming up.
What the windows came to together:
| Figure | Meaning |
|---|---|
| Starts that beat holding | The share of windows whose return, after costs, beat buying and holding through them, before costs |
| Starts that gained | The share of windows whose return was above zero |
| Spread | The 10th percentile, median and 90th percentile of the windows' returns, holding's and the first less the second, by nearest rank |
| Deepest fall in a window | The deepest drawdown any window took |
| Thin windows | Windows that closed fewer than 20 trades |
| Periods apart | How many windows fit end to end over the months they span |
with the starts counted in months. Windows that overlap share their bars, so their shares rest on about that many periods, not on the count of windows. For the same reason no window's timing is tested, and none has a p-value.
Limits
Daily and longer bars reach back 10 years, shorter ones 3. A run reads at most 300,000 bars. Rolling windows run up to 60 months in the panel and 120 in the agent's scripts, and start 1 to 12 months apart. A rule nests all and any at most 3 deep, each holding at most 8 conditions; a strategy's entry and exit make at most 64 comparisons and read at most 32 series between them. A strategy's own lines hold at most 4,000,000 values a run between them, every line's bars added up, apart from those series. The agent's scripts reach further back than the app's own history for at most 5 listings a script.