Ảnh bìa bài viết về backtest crypto sai lệch do phí, funding và hold-out

Why your crypto backtest lies: fees, funding and the hold-out you never opened

Your strategy makes 0.17% per trade in the backtest. Sixty percent of trades win. You have been looking at the equity curve for a week and it only goes up.

Before you put money behind it, run three checks. Most strategies that look like this fail at least one of them, and each check takes a minute.

I’ll use a made-up example that ships with backtest-sanity, a small MIT-licensed tool I wrote for exactly this: 240 trades on a fake perpetual future, AAAUSDT. Every number below is real output from that tool on that file.

1. Costs: the edge you pay away

A backtest without costs measures the market, not your strategy. On a perpetual future you pay three things on every trade: the exchange fee on entry and exit, slippage on entry and exit, and funding for every funding event you hold through.

First, the trades as most backtests report them:

$ backtest-sanity costs examples/trades.csv --fee-bps 0
Average gross return per trade: +0.170%   win rate 61.7%

Now with a 5 bps taker fee and 2 bps slippage per side, plus the funding history:

$ backtest-sanity costs examples/trades.csv --fee-bps 5 --slippage-bps 2 --funding examples/funding.csv
Trades: 240
Average gross return per trade: +0.170%   win rate 61.7%
Fees and slippage per trade:     -0.140%
Funding per trade:               -0.008%
Average net return per trade:    +0.022%   win rate 50.8%
Share of the gross edge eaten by costs: 87%
Break-even fee per side: 6.08 bps (with your slippage and funding)

Costs ate 87% of the edge. The win rate fell from 61.7% to 50.8%, because many “winners” were smaller than the round-trip cost. And the break-even fee is 6.08 bps per side: one fee tier worse, or a little more slippage on a thin day, and the strategy loses money before anything else goes wrong.

Two habits help. Charge fees per side: entry and exit both pay. And look up the fee tier you actually pay, not the one on the marketing page.

2. The hold-out you never opened

The second lie is subtler. If you tuned the strategy on all your data, the backtest measures how well you fitted the past. The only defence is a hold-out period: a date chosen before you look at any result, after which you do not touch the parameters.

Here the hold-out starts on 1 June 2025:

$ backtest-sanity split examples/trades.csv --fee-bps 5 --slippage-bps 2 --funding examples/funding.csv --holdout-from 2025-06-01
Hold-out from 2025-06-01 (net of costs)
In sample      trades   201   mean +0.064%   win rate 53.7%   t 1.58
Out of sample  trades    39   mean -0.196%   win rate 35.9%   t -2.27
- The edge disappears out of sample: positive before the hold-out date, zero or negative after it.

The strategy that looked fine is negative once it meets data it was not tuned on. Even in sample, a t-statistic of 1.58 means the average is less than two standard errors from zero: not much to bet on.

The tool also warns when there are fewer than 30 trades after the hold-out date, because with so few trades nobody can tell an edge from luck. The rule that matters most is not in the code: pick the date first, write it down, and open the hold-out once.

3. Lookahead: the signal that knew the future

The third lie hides in the signal itself. A centred rolling window, a negative shift, or a z-score computed over the whole sample all use data that did not exist at the time of the trade. The backtest then looks great, and the live version cannot reproduce it.

The test is simple: compute the signal on the full history, then again on the history cut at bar t. A causal signal gives the same value at t both times.

import numpy as np
import pandas as pd
from backtest_sanity import check_lookahead

rng = np.random.default_rng(7)
close = 100 * np.exp(np.cumsum(0.01 * rng.standard_normal(500)))
bars = pd.DataFrame({"close": close},
                    index=pd.date_range("2025-01-01", periods=500, freq="h", tz="UTC"))

def above_average(d):          # causal: only past bars
    return (d["close"] > d["close"].rolling(20).mean()).astype(int)

def zscore(d):                 # leaky: mean and std of the whole sample
    return (d["close"] - d["close"].mean()) / d["close"].std()

print(check_lookahead(above_average, bars).summary())
print(check_lookahead(zscore, bars).summary())
No lookahead found at 25 checked bars.
Lookahead at 25 of 25 checked bars. First at 2025-01-03 02:00:00+00:00: -0.7376 with data up to that bar, 1.335 with the full history.

The z-score of the same bar is -0.74 with the data you had at the time and 1.34 with the full history. A strategy that buys on that value is trading on information from the future.

One limit, stated plainly: the test only sees a leak when future data changes the signal’s value at a checked bar. Passing it is evidence, not proof.

What a clean result means

Passing all three checks is necessary, not sufficient. It means the backtest is not lying in the three most common ways. It does not mean the strategy will make money.

The next steps are boring on purpose: a forward test on data the strategy has never seen, then days of paper trading on live prices, and only then a small amount of real money with a stop on the exchange for every position.

Try it

pip install git+https://github.com/kauriq/backtest-sanity

The repository has the example files, so every command above runs as shown. It needs only pandas and numpy. Issues and pull requests are welcome.

For the checks no tool can run for you, there is a free one-page checklist: Backtest honestly: 12 checks before you risk money (PDF).


I build Kauriq, self-hosted software that makes you prove an idea with backtests, forward tests and paper days before it trades your own account. One payment, full source. Leveraged futures can lose all your money quickly; this is software, not advice.

Frequently asked questions

Vì sao backtest crypto thường cho kết quả đẹp hơn thực tế?

Thường do bỏ qua chi phí, tối ưu tham số trên toàn bộ dữ liệu, hoặc tín hiệu vô tình dùng dữ liệu tương lai. Bài viết hướng dẫn kiểm tra ba lỗi này.

Chi phí nào cần tính khi backtest hợp đồng perpetual?

Gồm phí sàn khi vào và thoát lệnh, slippage hai chiều, và funding cho mỗi kỳ funding mà bạn giữ lệnh qua. Phí cần tính theo từng chiều và theo mức phí bạn thực trả.

Hold-out là gì và dùng thế nào?

Là một mốc thời gian chọn trước khi xem kết quả; sau mốc đó bạn không chỉnh tham số nữa và chỉ mở dữ liệu này một lần. Nếu dưới 30 lệnh sau mốc, công cụ sẽ cảnh báo vì khó phân biệt lợi thế với may mắn.

Lookahead được phát hiện như thế nào?

Tính tín hiệu trên toàn bộ lịch sử, rồi tính lại trên lịch sử cắt tại nến t. Tín hiệu đúng nhân quả sẽ cho cùng giá trị tại t. Qua bài kiểm tra này là bằng chứng chứ chưa phải chứng minh.

Qua cả ba bước kiểm tra thì có nên vào tiền thật ngay không?

Chưa. Bước tiếp theo là forward test, vài ngày paper trading trên giá thật, rồi mới dùng số tiền nhỏ kèm lệnh dừng lỗ trên sàn.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top