Skip to content

Commit

Permalink
0.9.28 update
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Sep 2, 2023
1 parent fb5d6d8 commit 6c7dfb5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions czsc/data/ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def format_kline(kline: pd.DataFrame, freq: Freq) -> List[RawBar]:

for i, record in enumerate(records):
if freq == Freq.D:
vol = int(record['vol']*100)
amount = int(record.get('amount', 0)*1000)
vol = int(record['vol'] * 100) if record['vol'] > 0 else 0
amount = int(record.get('amount', 0) * 1000)
else:
vol = int(record['vol'])
vol = int(record['vol']) if record['vol'] > 0 else 0
amount = int(record.get('amount', 0))

# 将每一根K线转换成 RawBar 对象
Expand Down
2 changes: 0 additions & 2 deletions czsc/utils/corr.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,3 @@ def cross_sectional_ic(df, x_col='open', y_col='n1b', method='spearman', **kwarg
res['IC胜率'] = round(len(df[df['ic'] > 0]) / len(df), 4)
res['IC绝对值>2%占比'] = round(len(df[df['ic'].abs() > 0.02]) / len(df), 4)
return df, res


21 changes: 13 additions & 8 deletions czsc/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
author: zengbin93
email: zeng_bin8888@163.com
create_dt: 2023/4/19 23:27
describe:
describe: 绩效表现统计
"""
import numpy as np
import pandas as pd
Expand All @@ -19,7 +19,7 @@ def subtract_fee(df, fee=1):
if 'n1b' not in df.columns:
assert 'price' in df.columns, '当n1b列不存在时,price 列必须存在'
df['n1b'] = (df['price'].shift(-1) / df['price'] - 1) * 10000

df['date'] = df['dt'].dt.date
df['edge_pre_fee'] = df['pos'] * df['n1b']
df['edge_post_fee'] = df['pos'] * df['n1b']
Expand All @@ -41,25 +41,30 @@ def daily_performance(daily_returns):
[0.01, 0.02, -0.01, 0.03, 0.02, -0.02, 0.01, -0.01, 0.02, 0.01]
:return: dict
"""
if isinstance(daily_returns, list):
daily_returns = np.array(daily_returns)

daily_returns = np.array(daily_returns, dtype=np.float64)

if len(daily_returns) == 0 or np.std(daily_returns) == 0 or all(x == 0 for x in daily_returns):
return {"年化": 0, "夏普": 0, "最大回撤": 0, "卡玛": 0, "日胜率": 0}
return {"年化": 0, "夏普": 0, "最大回撤": 0, "卡玛": 0, "日胜率": 0, "年化波动率": 0, "非零覆盖": 0}

annual_returns = np.sum(daily_returns) / len(daily_returns) * 252
sharpe_ratio = np.mean(daily_returns) / np.std(daily_returns) * np.sqrt(252)
cum_returns = np.cumsum(daily_returns)
max_drawdown = np.max(np.maximum.accumulate(cum_returns) - cum_returns)
kama = annual_returns / max_drawdown if max_drawdown != 0 else 10
win_pct = len(daily_returns[daily_returns > 0]) / len(daily_returns)
return {
annual_volatility = np.std(daily_returns) * np.sqrt(252)
none_zero_cover = len(daily_returns[daily_returns != 0]) / len(daily_returns)

sta = {
"年化": round(annual_returns, 4),
"夏普": round(sharpe_ratio, 2),
"最大回撤": round(max_drawdown, 4),
"卡玛": round(kama, 2),
"日胜率": round(win_pct, 4),
"年化波动率": round(annual_volatility, 4),
"非零覆盖": round(none_zero_cover, 4),
}
return sta


def net_value_stats(nv: pd.DataFrame, exclude_zero: bool = False, sub_cost=True) -> dict:
Expand Down

0 comments on commit 6c7dfb5

Please sign in to comment.