forked from waditu/czsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 0.9.46 start coding * 0.9.46 PSI 测试开发 * 0.9.46 update * 0.9.46 新增 optuna 超参分析 * 0.9.46 disk cache 增加默认path * 0.9.46 新增 rolling_tanh 函数 * 0.9.46 新增CCF因子函数 * 0.9.46 新增最大回撤分析组件 * 0.9.46 新增期货调仓函数
- Loading branch information
Showing
21 changed files
with
522 additions
and
27 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 |
---|---|---|
|
@@ -58,7 +58,7 @@ pip install [email protected]:waditu/czsc.git -U | |
|
||
直接从github指定分支安装最新版: | ||
``` | ||
pip install git+https://github.com/waditu/[email protected].41 -U | ||
pip install git+https://github.com/waditu/[email protected].46 -U | ||
``` | ||
|
||
从`pypi`安装: | ||
|
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 |
---|---|---|
|
@@ -45,6 +45,8 @@ | |
ExitsOptimize, | ||
) | ||
from czsc.utils import ( | ||
format_standard_kline, | ||
|
||
KlineChart, | ||
WordWriter, | ||
BarGenerator, | ||
|
@@ -81,6 +83,7 @@ | |
holds_performance, | ||
net_value_stats, | ||
subtract_fee, | ||
top_drawdowns, | ||
|
||
home_path, | ||
DiskCache, | ||
|
@@ -94,6 +97,9 @@ | |
DataClient, | ||
set_url_token, | ||
get_url_token, | ||
|
||
optuna_study, | ||
optuna_good_params, | ||
) | ||
|
||
# 交易日历工具 | ||
|
@@ -121,6 +127,8 @@ | |
show_stoploss_by_direction, | ||
show_cointegration, | ||
show_out_in_compare, | ||
show_optuna_study, | ||
show_drawdowns, | ||
) | ||
|
||
from czsc.utils.bi_info import ( | ||
|
@@ -144,12 +152,14 @@ | |
rolling_compare, | ||
rolling_scale, | ||
rolling_slope, | ||
rolling_tanh, | ||
feature_adjust, | ||
) | ||
|
||
__version__ = "0.9.45" | ||
__version__ = "0.9.46" | ||
__author__ = "zengbin93" | ||
__email__ = "[email protected]" | ||
__date__ = "20240308" | ||
__date__ = "20240318" | ||
|
||
|
||
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 |
---|---|---|
|
@@ -204,7 +204,14 @@ def is_trade_time(trade_time: Optional[str] = None): | |
|
||
|
||
def get_daily_backup(api: TqApi, **kwargs): | ||
"""获取每日账户中需要备份的信息""" | ||
"""获取每日账户中需要备份的信息 | ||
https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.objs.html?highlight=account#tqsdk.objs.Order | ||
https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.objs.html?highlight=account#tqsdk.objs.Position | ||
https://doc.shinnytech.com/tqsdk/latest/reference/tqsdk.objs.html?highlight=account#tqsdk.objs.Account | ||
:param api: TqApi, 天勤API实例 | ||
""" | ||
orders = api.get_order() | ||
trades = api.get_trade() | ||
position = api.get_position() | ||
|
@@ -229,3 +236,61 @@ def get_daily_backup(api: TqApi, **kwargs): | |
"account": account, | ||
} | ||
return backup | ||
|
||
|
||
def adjust_portfolio(api: TqApi, portfolio, account=None, **kwargs): | ||
"""调整账户组合 | ||
**注意:** 此函数会阻塞,直到调仓完成;使用前请仔细阅读 TargetPosTask 的源码和文档,确保了解其工作原理 | ||
:param api: TqApi, 天勤API实例 | ||
:param account: str, 天勤账户 | ||
:param portfolio: dict, 组合配置,key 为合约代码,value 为配置信息; 样例数据: | ||
{ | ||
"[email protected]": {"target_volume": 10, "price": "PASSIVE", "offset_priority": "今昨,开"}, | ||
"[email protected]": {"target_volume": 0, "price": "ACTIVE", "offset_priority": "今昨,开"}, | ||
"[email protected]": {"target_volume": 30, "price": "PASSIVE", "offset_priority": "今昨,开"} | ||
} | ||
:param kwargs: dict, 其他参数 | ||
""" | ||
symbol_infos = {} | ||
for symbol, conf in portfolio.items(): | ||
quote = api.get_quote(symbol) | ||
|
||
lots = conf.get("target_volume", 0) | ||
price = conf.get("price", "PASSIVE") | ||
offset_priority = conf.get("offset_priority", "今昨,开") | ||
|
||
# 踩坑记录:TargetPosTask 的 symbol 必须是合约代码 | ||
contract = quote.underlying_symbol if "@" in symbol else symbol | ||
target_pos = TargetPosTask(api, contract, price=price, offset_priority=offset_priority, account=account) | ||
target_pos.set_target_volume(int(lots)) | ||
symbol_infos[symbol] = {"quote": quote, "target_pos": target_pos, "lots": lots} | ||
|
||
while True: | ||
api.wait_update() | ||
|
||
completed = [] | ||
for symbol, info in symbol_infos.items(): | ||
quote = info["quote"] | ||
target_pos: TargetPosTask = info["target_pos"] | ||
lots = info["lots"] | ||
contract = quote.underlying_symbol if "@" in symbol else symbol | ||
|
||
logger.info(f"调整仓位:{quote.datetime} - {contract}; 目标持仓:{lots}手; 当前持仓:{target_pos._pos.pos}手") | ||
|
||
if target_pos._pos.pos == lots: | ||
completed.append(True) | ||
logger.info(f"调仓完成:{quote.datetime} - {contract}; {lots}手") | ||
else: | ||
completed.append(False) | ||
|
||
if all(completed): | ||
break | ||
|
||
if kwargs.get("close_api", True): | ||
api.close() | ||
|
||
return api |
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 |
---|---|---|
|
@@ -24,4 +24,8 @@ | |
VPF002, | ||
VPF003, | ||
VPF004, | ||
) | ||
|
||
from .tas import ( | ||
CCF | ||
) |
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,51 @@ | ||
""" | ||
技术指标因子 | ||
""" | ||
import inspect | ||
import hashlib | ||
import pandas as pd | ||
|
||
|
||
def CCF(df, **kwargs): | ||
"""使用 CZSC 库中的 factor 识别因子,主要用于识别缠论/形态因子 | ||
:param df: 标准K线数据,DataFrame结构 | ||
:param kwargs: 其他参数 | ||
- czsc_factor: dict, 缠论因子配置,样例: | ||
{ | ||
"signals_all": ["日线_D1_表里关系V230101_向上_任意_任意_0"], | ||
"signals_any": [], | ||
"signals_not": ["日线_D1_涨跌停V230331_涨停_任意_任意_0"], | ||
} | ||
- freq: str, default '日线',K线级别 | ||
- tag: str, default None,标签,用于区分不同的因子 | ||
:return: pd.DataFrame | ||
""" | ||
from czsc.objects import Factor | ||
from czsc.utils import format_standard_kline | ||
from czsc.traders.base import generate_czsc_signals | ||
from czsc.traders.sig_parse import get_signals_config | ||
|
||
czsc_factor = kwargs.get('czsc_factor', None) | ||
freq = kwargs.get('freq', '日线') | ||
assert czsc_factor is not None and isinstance(czsc_factor, dict), "factor 参数必须指定" | ||
tag = kwargs.get('tag', hashlib.sha256(f"{czsc_factor}_{freq}".encode()).hexdigest().upper()[:6]) | ||
|
||
factor_name = inspect.stack()[0][3] | ||
factor_col = f'F#{factor_name}#{tag}' | ||
|
||
czsc_factor = Factor.load(czsc_factor) | ||
signals_seq = czsc_factor.signals_all + czsc_factor.signals_any + czsc_factor.signals_not | ||
signals_config = get_signals_config([x.signal for x in signals_seq]) | ||
|
||
bars = format_standard_kline(df, freq=freq) | ||
dfs = generate_czsc_signals(bars, signals_config, init_n=300, sdt=bars[0].dt, df=True) | ||
dfs[factor_col] = dfs.apply(czsc_factor.is_match, axis=1).astype(int) | ||
|
||
df = pd.merge(df, dfs[['dt', factor_col]], on='dt', how='left') | ||
df[factor_col] = df[factor_col].fillna(0) | ||
return df |
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
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
Oops, something went wrong.