-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 0.9.42 start coding * update * 0.9.42 disk cache 新增源码变动识别和支持文件类型 * 0.9.42 update * 0.9.42 新增按年统计表现 * 0.9.42 计算最大新高间隔逻辑修复 * 0.9.42 update * 0.9.42 update * 0.9.42 add_macd 优先按计算好的值绘图 * 0.9.42 新增 tas_double_ma_V240208 * 0.9.42 新增 bar_trend_V240209 * 0.9.42 新增 features 模块 * 0.9.42 update * 0.9.42 update * 0.9.42 update
- Loading branch information
Showing
25 changed files
with
907 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,7 @@ | |
# streamlit 量化分析组件 | ||
from czsc.utils.st_components import ( | ||
show_daily_return, | ||
show_yearly_stats, | ||
show_splited_daily, | ||
show_monthly_return, | ||
show_correlation, | ||
|
@@ -126,10 +127,14 @@ | |
find_most_similarity, | ||
) | ||
|
||
__version__ = "0.9.41" | ||
from czsc.features.utils import ( | ||
is_event_feature, | ||
) | ||
|
||
__version__ = "0.9.42" | ||
__author__ = "zengbin93" | ||
__email__ = "[email protected]" | ||
__date__ = "20240114" | ||
__date__ = "20240121" | ||
|
||
|
||
def welcome(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
author: zengbin93 | ||
email: [email protected] | ||
create_dt: 2024/02/14 17:48 | ||
describe: 时序特征因子库 | ||
因子函数编写规范:https://s0cqcxuy3p.feishu.cn/wiki/A9yawT6o1il9SrkUoBNchtXjnBK | ||
""" | ||
|
||
from .ret import ( | ||
RET001, | ||
RET002, | ||
RET003, | ||
RET004, | ||
RET005, | ||
RET006, | ||
RET007, | ||
RET008, | ||
) | ||
|
||
from .vpf import ( | ||
VPF001, | ||
VPF002, | ||
VPF003, | ||
VPF004, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
""" | ||
用于计算未来收益相关的因子,含有未来信息,不可用于实际交易 | ||
通常用作模型训练、因子评价的标准 | ||
""" | ||
import numpy as np | ||
import pandas as pd | ||
|
||
|
||
def RET001(df, **kwargs): | ||
"""用 close 价格计算未来 N 根K线的收益率 | ||
参数空间: | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET001#{tag}' | ||
df[col] = df['close'].shift(-n) / df['close'] - 1 | ||
df[col] = df[col].fillna(0) | ||
|
||
|
||
def RET002(df, **kwargs): | ||
"""用 open 价格计算未来 N 根K线的收益率 | ||
参数空间: | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET002#{tag}' | ||
df[col] = df['open'].shift(-n - 1) / df['open'].shift(-1) - 1 | ||
df[col] = df[col].fillna(0) | ||
|
||
|
||
def RET003(df, **kwargs): | ||
"""未来 N 根K线的收益波动率 | ||
参数空间: | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
- n: int, 计算未来 N 根K线的收益波动率 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET003#{tag}' | ||
df['tmp'] = df['close'].pct_change() | ||
df[col] = df['tmp'].rolling(n).std().shift(-n) | ||
df[col] = df[col].fillna(0) | ||
df.drop(columns=['tmp'], inplace=True) | ||
|
||
|
||
def RET004(df, **kwargs): | ||
"""未来 N 根K线的最大收益盈亏比 | ||
注意: | ||
1. 约束盈亏比的范围是 [0, 10] | ||
2. 当未来 N 根K线内收益最小值为0时,会导致计算结果为无穷大,此时将结果设置为10 | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
- n: int, 计算未来 N 根K线的收益盈亏比 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET004#{tag}' | ||
df['max_ret'] = df['close'].rolling(n).apply(lambda x: x.max() / x[0] - 1, raw=True) | ||
df['min_ret'] = df['close'].rolling(n).apply(lambda x: x.min() / x[0] - 1, raw=True) | ||
df[col] = (df['max_ret'] / df['min_ret'].abs()).shift(-n) | ||
df[col] = df[col].fillna(0) | ||
df[col] = df[col].clip(0, 10) | ||
df.drop(columns=['max_ret', 'min_ret'], inplace=True) | ||
|
||
|
||
def RET005(df, **kwargs): | ||
"""未来 N 根K线的逐K胜率 | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
- n: int, 滚动窗口大小 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET005#{tag}' | ||
df['ret'] = df['close'].pct_change() | ||
df[col] = df['ret'].rolling(n).apply(lambda x: np.sum(x > 0) / n).shift(-n) | ||
df[col] = df[col].fillna(0) | ||
df.drop(columns=['ret'], inplace=True) | ||
|
||
|
||
def RET006(df, **kwargs): | ||
"""未来 N 根K线的逐K盈亏比 | ||
注意: | ||
1. 约束盈亏比的范围是 [0, 10] | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
- n: int, 滚动窗口大小 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET006#{tag}' | ||
df['ret'] = df['close'].pct_change() | ||
df['mean_win'] = df['ret'].rolling(n).apply(lambda x: np.sum(x[x > 0]) / np.sum(x > 0)) | ||
df['mean_loss'] = df['ret'].rolling(n).apply(lambda x: np.sum(x[x < 0]) / np.sum(x < 0)) | ||
df[col] = (df['mean_win'] / df['mean_loss'].abs()).shift(-n) | ||
df[col] = df[col].fillna(0) | ||
df[col] = df[col].clip(0, 10) | ||
df.drop(columns=['ret', 'mean_win', 'mean_loss'], inplace=True) | ||
|
||
|
||
def RET007(df, **kwargs): | ||
"""未来 N 根K线的最大跌幅 | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
- n: int, 滚动窗口大小 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET007#{tag}' | ||
df[col] = df['close'].rolling(n).apply(lambda x: np.min(x) / x[0] - 1, raw=True).shift(-n) | ||
df[col] = df[col].fillna(0) | ||
|
||
|
||
def RET008(df, **kwargs): | ||
"""未来 N 根K线的最大涨幅 | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- tag: str, 因子字段标记 | ||
- n: int, 滚动窗口大小 | ||
:return: None | ||
""" | ||
tag = kwargs.get('tag', 'A') | ||
n = kwargs.get('n', 5) | ||
|
||
col = f'F#RET008#{tag}' | ||
df[col] = df['close'].rolling(n).apply(lambda x: np.max(x) / x[0] - 1, raw=True).shift(-n) | ||
df[col] = df[col].fillna(0) | ||
|
||
|
||
def test_ret_functions(): | ||
from czsc.connectors import cooperation as coo | ||
|
||
df = coo.dc.pro_bar(code="000001.SZ", freq="day", sdt="2020-01-01", edt="2021-01-31") | ||
df['dt'] = pd.to_datetime(df['dt']) | ||
df.rename(columns={'code': 'symbol'}, inplace=True) | ||
|
||
RET001(df, tag='A') | ||
assert 'F#RET001#A' in df.columns | ||
|
||
RET002(df, tag='A') | ||
assert 'F#RET002#A' in df.columns | ||
|
||
RET003(df, tag='A') | ||
assert 'F#RET003#A' in df.columns | ||
|
||
RET004(df, tag='A') | ||
assert 'F#RET004#A' in df.columns | ||
|
||
RET005(df, tag='A') | ||
assert 'F#RET005#A' in df.columns | ||
|
||
RET006(df, tag='A') | ||
assert 'F#RET006#A' in df.columns | ||
|
||
RET007(df, tag='A') | ||
assert 'F#RET007#A' in df.columns | ||
|
||
RET008(df, tag='A') | ||
assert 'F#RET008#A' in df.columns |
Oops, something went wrong.