-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_backtest.py
88 lines (70 loc) · 2.64 KB
/
test_backtest.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
import lazyft.models.backtest
from lazyft import backtest
from lazyft.backtest.commands import create_commands
from lazyft.backtest.runner import BacktestRunner
from lazyft.command_parameters import BacktestParameters
param_id = "test"
# STRATEGY_WITH_ID = [Strategy(id=1)]
STRATEGIES = ["TestStrategy-test", "TestStrategy"]
config_name = "config.json"
days = 5
def get_commands(strategies):
cp = get_parameters(strategies)
commands = create_commands(cp, verbose=True)
return commands
def get_parameters(strategies):
cp = BacktestParameters(
strategies=strategies, config_path=config_name, days=days, download_data=False
)
return cp
def test_backtest_command_no_id():
cp = BacktestParameters(
strategies=STRATEGIES, config_path=config_name, days=days, download_data=True
)
commands = create_commands(cp, verbose=True)
runner = BacktestRunner(commands[1])
runner.execute()
if runner.error:
raise RuntimeError("Error in backtest runner")
assert bool(runner.report)
runner.save()
def test_backtest_command_with_id():
commands = get_commands(STRATEGIES)
runner = BacktestRunner(commands[0])
runner.execute()
if runner.error:
raise RuntimeError("Error in backtest runner")
assert bool(runner.report)
# runner.report.json_file.unlink(missing_ok=True)
runner.save()
# def test_id_pairlist():
# """Make sure a pairlist can be extracted from a previous Hyperopt run"""
# existing_report = get_hyperopt_repo().get_by_param_id(param_id)
# pairlist = existing_report.pairlist
# parameters = get_parameters(STRATEGY_WITH_ID)
# assert parameters.config['exchange']['name'] == existing_report.exchange
# parameters.pairs = []
# strategy, id = parameters.strategy_id_pairs[0]
# params_pairs = BacktestCommand(strategy, params=parameters, id=id).pairs
# assert set(params_pairs) == set(pairlist)
# def test_backtest_with_generated_pairlist():
# commands = backtest.create_commands(
# strategies=STRATEGY, interval='5m', config=config_name, days=days
# )
# runner = backtest.BacktestRunner(commands.pop())
# runner.execute()
# report = runner.generate_report()
# report.print_winners()
def test_save_backtesting_report():
commands = get_commands(STRATEGIES)
runner = BacktestRunner(commands[0])
runner.execute()
assert bool(runner.report)
runner.save()
def test_multi_runner():
commands = get_commands(STRATEGIES)
mr = backtest.BacktestMultiRunner(commands)
mr.execute()
assert any(mr.reports)
for r in mr.reports:
assert isinstance(r, lazyft.models.backtest.BacktestReport)