Skip to content

Commit

Permalink
09.48 新增滚动日收益指标计算
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Apr 18, 2024
1 parent 011f5c0 commit 3adb903
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 318 deletions.
2 changes: 2 additions & 0 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
SignalAnalyzer,
SignalPerformance,
daily_performance,
rolling_daily_performance,
weekly_performance,
holds_performance,
net_value_stats,
Expand Down Expand Up @@ -130,6 +131,7 @@
show_out_in_compare,
show_optuna_study,
show_drawdowns,
show_rolling_daily_performance,
)

from czsc.utils.bi_info import (
Expand Down
48 changes: 38 additions & 10 deletions czsc/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
from .plotly_plot import KlineChart
from .trade import cal_trade_price, update_nbars, update_bbars, update_tbars, risk_free_returns, resample_to_daily
from .cross import CrossSectionalPerformance, cross_sectional_ranker
from .stats import daily_performance, net_value_stats, subtract_fee, weekly_performance, holds_performance, top_drawdowns
from .stats import (
daily_performance,
net_value_stats,
subtract_fee,
weekly_performance,
holds_performance,
top_drawdowns,
rolling_daily_performance,
)
from .signal_analyzer import SignalAnalyzer, SignalPerformance
from .cache import home_path, get_dir_size, empty_cache_path, DiskCache, disk_cache, clear_cache
from .index_composition import index_composition
Expand All @@ -27,8 +35,27 @@
from .optuna import optuna_study, optuna_good_params


sorted_freqs = ['Tick', '1分钟', '2分钟', '3分钟', '4分钟', '5分钟', '6分钟', '10分钟', '12分钟',
'15分钟', '20分钟', '30分钟', '60分钟', '120分钟', '日线', '周线', '月线', '季线', '年线']
sorted_freqs = [
"Tick",
"1分钟",
"2分钟",
"3分钟",
"4分钟",
"5分钟",
"6分钟",
"10分钟",
"12分钟",
"15分钟",
"20分钟",
"30分钟",
"60分钟",
"120分钟",
"日线",
"周线",
"月线",
"季线",
"年线",
]


def x_round(x: Union[float, int], digit: int = 4) -> Union[float, int]:
Expand Down Expand Up @@ -56,9 +83,9 @@ def get_py_namespace(file_py: str, keys: list = []) -> dict:
:param keys: 指定需要的对象名称
:return: namespace
"""
text = open(file_py, 'r', encoding='utf-8').read()
code = compile(text, file_py, 'exec')
namespace = {"file_py": file_py, 'file_name': os.path.basename(file_py).split('.')[0]}
text = open(file_py, "r", encoding="utf-8").read()
code = compile(text, file_py, "exec")
namespace = {"file_py": file_py, "file_name": os.path.basename(file_py).split(".")[0]}
exec(code, namespace)
if keys:
namespace = {k: v for k, v in namespace.items() if k in keys}
Expand All @@ -82,11 +109,11 @@ def import_by_name(name):
:param name: 模块名,如:'czsc.objects.Factor'
:return: 模块对象
"""
if '.' not in name:
if "." not in name:
return __import__(name)

# 从右边开始分割,分割成模块名和函数名
module_name, function_name = name.rsplit('.', 1)
module_name, function_name = name.rsplit(".", 1)
module = __import__(module_name, globals(), locals(), [function_name])
return vars(module)[function_name]

Expand Down Expand Up @@ -143,11 +170,12 @@ def create_grid_params(prefix: str = "", multiply=3, **kwargs) -> dict:
else:
key = str(i).zfill(multiply)

row['version'] = f"{prefix}{key}"
row["version"] = f"{prefix}{key}"
params[f"{prefix}@{key}"] = row
return params


def print_df_sample(df, n=5):
from tabulate import tabulate
print(tabulate(df.head(n).values, headers=df.columns, tablefmt='rst'))

print(tabulate(df.head(n).values, headers=df.columns, tablefmt="rst"))
Loading

0 comments on commit 3adb903

Please sign in to comment.