forked from waditu/czsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_fast_backtest.py
66 lines (55 loc) · 2.25 KB
/
ts_fast_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
# -*- coding: utf-8 -*-
"""
author: zengbin93
email: [email protected]
create_dt: 2021/12/12 22:00
"""
import os
import pandas as pd
from czsc.traders.ts_backtest import TsDataCache, TsStocksBacktest, freq_cn2ts
# from czsc.strategies import trader_example1 as strategy
from czsc.strategies import trader_strategy_a as strategy
# from examples import tactics
os.environ['czsc_verbose'] = "0" # 是否输出详细执行信息,包括一些用于debug的信息,0 不输出,1 输出
os.environ['czsc_min_bi_len'] = "6" # 通过环境变量设定最小笔长度,6 对应新笔定义,7 对应老笔定义
pd.set_option('mode.chained_assignment', None)
pd.set_option('display.max_rows', 1000)
pd.set_option('display.max_columns', 20)
data_path = r"C:\ts_data_czsc"
dc = TsDataCache(data_path, sdt='2000-01-01', edt='2022-02-18')
freq = freq_cn2ts[strategy('000001.SH')['base_freq']]
sdt = '20140101'
edt = "20211216"
init_n = 1000*4
def run_backtest(step_seq=('check', 'index', 'etfs', 'train', 'valid', 'stock')):
"""
:param step_seq: 回测执行顺序
:return:
"""
tsb = TsStocksBacktest(dc, strategy, init_n, sdt, edt)
for step in step_seq:
tsb.batch_backtest(step.lower())
# tsb.analyze_signals(step.lower())
tsb.analyze_results(step, 'long')
# tsb.analyze_results(step, 'short')
print(f"results saved into {tsb.res_path}")
def run_more_backtest(step, ts_codes):
"""指定在某个阶段多回测一些标的,最常见的需求是在 check 阶段多检查几个标的
:param step: 阶段名称
:param ts_codes: 新增回测标的列表
:return:
"""
tsb = TsStocksBacktest(dc, strategy, init_n, sdt, edt)
tsb.update_step(step, ts_codes)
tsb.batch_backtest(step.lower())
# tsb.analyze_signals(step.lower())
tsb.analyze_results(step, 'long')
# tsb.analyze_results(step, 'short')
print(f"results saved into {tsb.res_path}")
if __name__ == '__main__':
# run_more_backtest(step='check', ts_codes=['000002.SZ'])
run_backtest(step_seq=('index',))
# run_backtest(step_seq=('etfs',))
# run_backtest(step_seq=('index', 'train'))
# run_backtest(step_seq=('check', 'index', 'train'))
# run_backtest(step_seq=('check', 'index', 'train', 'valid'))