-
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.36 start coding * 0.9.35 fix bug * 0.9.36 优化策略持仓权重发布 * 0.9.36 fix resample_bars * 0.9.36 remove retry * 0.9.36 update * 0.9.36 新增协作成员专属数据接口 * 0.9.36 新增 risk_free_returns 函数
- Loading branch information
Showing
14 changed files
with
313 additions
and
46 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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
from czsc import sensors | ||
from czsc import aphorism | ||
from czsc.analyze import CZSC | ||
from czsc.objects import Freq, Operate, Direction, Signal, Factor, Event, RawBar, NewBar, Position | ||
from czsc.objects import Freq, Operate, Direction, Signal, Factor, Event, RawBar, NewBar, Position, ZS | ||
from czsc.strategies import CzscStrategyBase, CzscJsonStrategy | ||
from czsc.sensors import holds_concepts_effect, CTAResearch, EventMatchSensor | ||
from czsc.sensors.feature import FixedNumberSelector, FeatureAnalyzeBase | ||
|
@@ -60,6 +60,8 @@ | |
update_bbars, | ||
update_tbars, | ||
update_nbars, | ||
risk_free_returns, | ||
|
||
CrossSectionalPerformance, | ||
cross_sectional_ranker, | ||
cross_sectional_ic, | ||
|
@@ -110,10 +112,10 @@ | |
feture_cross_layering, | ||
) | ||
|
||
__version__ = "0.9.35" | ||
__version__ = "0.9.36" | ||
__author__ = "zengbin93" | ||
__email__ = "[email protected]" | ||
__date__ = "20231104" | ||
__date__ = "20231112" | ||
|
||
|
||
|
||
|
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,107 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
author: zengbin93 | ||
email: [email protected] | ||
create_dt: 2023/11/15 20:45 | ||
describe: CZSC开源协作团队内部使用数据接口 | ||
""" | ||
import os | ||
import czsc | ||
import pandas as pd | ||
from czsc import RawBar, Freq | ||
|
||
# 首次使用需要打开一个python终端按如下方式设置 token | ||
# czsc.set_url_token(token='your token', url='http://zbczsc.com:9106') | ||
|
||
cache_path = os.getenv("CZSC_CACHE_PATH", os.path.expanduser("~/.quant_data_cache")) | ||
dc = czsc.DataClient(url='http://zbczsc.com:9106', cache_path=cache_path) | ||
|
||
|
||
def format_kline(kline: pd.DataFrame, freq: Freq): | ||
"""格式化K线数据 | ||
:param kline: K线数据,格式如下: | ||
========== ========= ====== ======= ====== ===== =========== =========== | ||
dt code open close high low vol amount | ||
========== ========= ====== ======= ====== ===== =========== =========== | ||
2022-01-04 600520.SH 20.54 21.12 21.17 20.33 2.1724e+06 1.94007e+07 | ||
2022-01-05 600520.SH 21.17 20.73 21.29 20.52 1.8835e+06 1.67258e+07 | ||
2022-01-06 600520.SH 20.56 21.17 21.57 18.69 3.4227e+06 3.11461e+07 | ||
2022-01-07 600520.SH 21.5 20.61 21.5 20.61 2.51741e+06 2.24819e+07 | ||
2022-01-10 600520.SH 20.4 21.69 21.69 20.4 4.80894e+06 4.39598e+07 | ||
========== ========= ====== ======= ====== ===== =========== =========== | ||
:return: 格式化后的K线数据 | ||
""" | ||
bars = [] | ||
for i, row in kline.iterrows(): | ||
bar = RawBar(symbol=row['code'], id=i, freq=freq, dt=row['dt'], | ||
open=row['open'], close=row['close'], high=row['high'], | ||
low=row['low'], vol=row['vol'], amount=row['amount']) | ||
bars.append(bar) | ||
return bars | ||
|
||
|
||
def get_symbols(name, **kwargs): | ||
"""获取指定分组下的所有标的代码 | ||
:param name: 分组名称,可选值:'A股指数', 'ETF', '股票', '期货主力' | ||
:param kwargs: | ||
:return: | ||
""" | ||
if name == "股票": | ||
data = dc.stock_basic(nobj=1, status=1) | ||
return data['code'].tolist() | ||
|
||
if name == "ETF": | ||
raise NotImplementedError | ||
|
||
if name == "A股指数": | ||
raise NotImplementedError | ||
|
||
if name == "期货主力": | ||
kline = dc.future_klines(trade_date="20231101") | ||
return kline['code'].unique().tolist() | ||
|
||
raise ValueError(f"{name} 分组无法识别,获取标的列表失败!") | ||
|
||
|
||
def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs): | ||
"""获取 CZSC 库定义的标准 RawBar 对象列表 | ||
:param symbol: 标的代码 | ||
:param freq: 周期,支持 Freq 对象,或者字符串,如 | ||
'1分钟', '5分钟', '15分钟', '30分钟', '60分钟', '日线', '周线', '月线', '季线', '年线' | ||
:param sdt: 开始时间 | ||
:param edt: 结束时间 | ||
:param fq: 除权类型,可选值:'前复权', '后复权', '不复权' | ||
:param kwargs: | ||
:return: | ||
""" | ||
freq = czsc.Freq(freq) | ||
|
||
if symbol.endswith(".SH") or symbol.endswith(".SZ"): | ||
fq_map = {"前复权": "qfq", "后复权": "hfq", "不复权": None} | ||
adj = fq_map.get(fq, None) | ||
if freq.value.endswith('分钟'): | ||
df = dc.pro_bar(code=symbol, sdt=sdt, edt=edt, freq='min', adj=adj) | ||
df = df[~df['dt'].str.endswith("09:30:00")].reset_index(drop=True) | ||
else: | ||
df = dc.pro_bar(code=symbol, sdt=sdt, edt=edt, freq='day', adj=adj) | ||
df.rename(columns={'code': 'symbol'}, inplace=True) | ||
df['dt'] = pd.to_datetime(df['dt']) | ||
return czsc.resample_bars(df, target_freq=freq) | ||
|
||
if symbol.endswith("9001"): | ||
if freq.value.endswith('分钟'): | ||
df = dc.future_klines(code=symbol, sdt=sdt, edt=edt, freq='1m') | ||
else: | ||
df = dc.future_klines(code=symbol, sdt=sdt, edt=edt, freq='1d') | ||
df.rename(columns={'code': 'symbol'}, inplace=True) | ||
df['amount'] = df['vol'] * df['close'] | ||
df = df[['symbol', 'dt', 'open', 'close', 'high', 'low', 'vol', 'amount']].copy().reset_index(drop=True) | ||
df['dt'] = pd.to_datetime(df['dt']) | ||
return czsc.resample_bars(df, target_freq=freq) | ||
|
||
raise ValueError(f"symbol {symbol} 无法识别,获取数据失败!") |
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.