Skip to content

Commit

Permalink
0.8.30 update (waditu#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Oct 16, 2022
1 parent 05bb05a commit 17412af
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
9 changes: 5 additions & 4 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
__email__ = "zeng_bin8888@163.com"
__date__ = "20220912"

print(f"CZSC环境变量:"
f"czsc_min_bi_len = {envs.get_min_bi_len()}; "
f"czsc_max_bi_num = {envs.get_max_bi_num()}; "
f"czsc_bi_change_th = {envs.get_bi_change_th()}")

if envs.get_welcome():
print(f"欢迎使用CZSC!当前版本标识为 {__version__}@{__date__}\n")
aphorism.print_one()

print(f"CZSC环境变量:"
f"czsc_min_bi_len = {envs.get_min_bi_len()}; "
f"czsc_max_bi_num = {envs.get_max_bi_num()}; "
f"czsc_bi_change_th = {envs.get_bi_change_th()}")


if get_dir_size(home_path) > pow(1024, 3) and envs.get_verbose():
print(f"{home_path} 目录缓存超过1GB,请适当清理。调用 czsc.empty_cache_path 可以直接清空缓存")
4 changes: 3 additions & 1 deletion czsc/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
"""
import click
import pandas as pd
from czsc.aphorism import print_one


@click.group()
def czsc():
"""CZSC 命令行工具"""
pass


@czsc.command()
def aphorism():
"""随机输出一条缠中说禅良言警句"""
from czsc.aphorism import print_one

print_one()


2 changes: 1 addition & 1 deletion czsc/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_verbose(verbose=None):

def get_welcome():
"""welcome - 是否输出版本标识和缠中说禅博客摘记"""
v = True if os.environ.get('czsc_welcome', '1') in valid_true else False
v = True if os.environ.get('czsc_welcome', '0') in valid_true else False
return v


Expand Down
12 changes: 6 additions & 6 deletions examples/ts_fast_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
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 对应老笔定义
Expand All @@ -18,8 +16,11 @@
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')

# data_path 是 TS_CACHE 缓存数据文件夹所在目录
dc = TsDataCache(data_path=r"C:\ts_data_czsc", refresh=False, sdt="20120101", edt="20221001")

# 获取策略的基础K线周期,回测开始时间 sdt,回测结束时间 edt,初始化K线数量init_n
freq = freq_cn2ts[strategy('000001.SH')['base_freq']]
sdt = '20140101'
edt = "20211216"
Expand All @@ -35,11 +36,11 @@ def run_backtest(step_seq=('check', 'index', 'etfs', 'train', 'valid', 'stock'))
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 阶段多检查几个标的
Expand All @@ -50,7 +51,6 @@ def run_more_backtest(step, ts_codes):
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}")
Expand Down
36 changes: 24 additions & 12 deletions examples/ts_trade_replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
create_dt: 2022/5/6 15:58
describe: 请描述文件用途
"""
from ts_fast_backtest import dc
from czsc.data import freq_cn2ts
from czsc.strategies import trader_strategy_a as strategy
import pandas as pd
from czsc.data import TsDataCache, freq_cn2ts
from czsc.utils import BarGenerator
from czsc.traders.utils import trade_replay
from czsc.strategies import trader_strategy_a as strategy


tactic = strategy("000001.SH")
# data_path 是 TS_CACHE 缓存数据文件夹所在目录
dc = TsDataCache(data_path=r"C:\ts_data_czsc", refresh=False, sdt="20120101", edt="20221001")

# 获取单个品种的基础周期K线
tactic = strategy("000001.SZ")
base_freq = tactic['base_freq']
bars = dc.pro_bar_minutes('000001.SZ', "20150101", "20220101", freq=freq_cn2ts[base_freq],
asset="I", adj="hfq", raw_bar=True)
res_path = r"C:\ts_data_czsc\trade_replay_test4"
bars = dc.pro_bar_minutes('000001.SZ', "20150101", "20220101", freq=freq_cn2ts[base_freq], asset="E", adj="hfq")

# 设置回放快照文件保存目录
res_path = r"C:\ts_data_czsc\replay_trader_strategy_a"


# 拆分基础周期K线,一部分用来初始化BarGenerator,随后的K线是回放区间
start_date = pd.to_datetime("20200101")
bg = BarGenerator(base_freq, freqs=tactic['freqs'])
bars1 = [x for x in bars if x.dt <= start_date]
bars2 = [x for x in bars if x.dt > start_date]
for bar in bars1:
bg.update(bar)


if __name__ == '__main__':
bg = BarGenerator(base_freq, freqs=tactic['freqs'])
bars1 = bars[:24000]
bars2 = bars[24000:]
for bar in bars1:
bg.update(bar)
trade_replay(bg, bars2, strategy, res_path)


0 comments on commit 17412af

Please sign in to comment.