Repository navigation
Expand file tree
/
Copy pathtest_reference.py
More file actions
75 lines (52 loc) · 3.08 KB
/
Copy pathtest_reference.py
File metadata and controls
75 lines (52 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Regression guard on the published result.
The README and the notebook quote specific numbers for EMA(5/8) on the bundled
AAPL sample. Those numbers are the whole argument of this repository, so they
are pinned here: if a change to the engine, the cost model or the signal moves
them, that is either a bug or a finding, and either way it should not pass
silently.
Unlike the other suites these *are* golden values. That is deliberate and
narrow -- they guard one documented worked example, not a general property.
"""
import pytest
from backtester import (CostModel, EMACrossover, buy_and_hold, compare_costs,
compute_metrics, load_sample, max_drawdown,
run_backtest, sharpe_ratio)
# From the notebook's saved outputs; see the reference table in CLAUDE.md.
BEFORE_COSTS = {"total_return": 0.1195, "sharpe": 0.2127, "max_drawdown": -0.4113,
"win_rate": 0.3136, "n_trades": 118, "ann_turnover_x": 46.5150}
AFTER_COSTS = {"total_return": 0.0673, "sharpe": 0.1703, "max_drawdown": -0.4297,
"win_rate": 0.3051, "n_trades": 118, "ann_turnover_x": 46.5500}
@pytest.fixture(scope="module")
def sample():
return load_sample("AAPL")
@pytest.fixture(scope="module")
def costs_table(sample):
return compare_costs(sample, EMACrossover(5, 8).generate_signals(sample))
def test_sample_data_is_the_documented_window(sample):
assert len(sample) == 1259
assert str(sample.index[0].date()) == "2013-02-08"
assert str(sample.index[-1].date()) == "2018-02-07"
assert list(sample.columns) == ["Open", "High", "Low", "Close", "Volume"]
@pytest.mark.parametrize("metric,expected", sorted(BEFORE_COSTS.items()))
def test_before_cost_metrics(costs_table, metric, expected):
assert costs_table.loc[metric, "before_costs"] == pytest.approx(expected, abs=5e-4)
@pytest.mark.parametrize("metric,expected", sorted(AFTER_COSTS.items()))
def test_after_cost_metrics(costs_table, metric, expected):
assert costs_table.loc[metric, "after_costs"] == pytest.approx(expected, abs=5e-4)
def test_fill_and_trade_counts(sample):
res = run_backtest(sample, EMACrossover(5, 8).generate_signals(sample), CostModel())
assert len(res.fills) == 119
assert len(res.trades) == 118
def test_final_equity_to_the_cent(sample):
res = run_backtest(sample, EMACrossover(5, 8).generate_signals(sample), CostModel())
assert compute_metrics(res)["final_equity"] == pytest.approx(106729.3577, abs=5e-3)
def test_buy_and_hold_beats_the_strategy_by_a_mile(sample):
"""The headline of the whole repository, asserted."""
bh = buy_and_hold(sample, CostModel())
net = run_backtest(sample, EMACrossover(5, 8).generate_signals(sample), CostModel())
assert bh.iloc[-1] / bh.iloc[0] - 1 == pytest.approx(1.3432, abs=5e-4)
assert sharpe_ratio(bh.pct_change().fillna(0.0), 252.0) == pytest.approx(0.85, abs=5e-3)
assert max_drawdown(bh) == pytest.approx(-0.3208, abs=5e-4)
# Worse return and a deeper drawdown than simply holding the asset.
assert net.equity.iloc[-1] < bh.iloc[-1]
assert max_drawdown(net.equity) < max_drawdown(bh)