Skip to content

Commit

Permalink
0.9.49 确定了一批 deprecated 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Apr 30, 2024
1 parent e1733ee commit 572aa03
Show file tree
Hide file tree
Showing 12 changed files with 623 additions and 432 deletions.
1 change: 1 addition & 0 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
show_drawdowns,
show_rolling_daily_performance,
show_event_return,
show_psi,
)

from czsc.utils.bi_info import (
Expand Down
117 changes: 65 additions & 52 deletions czsc/connectors/cooperation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# 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)
dc = czsc.DataClient(url="http://zbczsc.com:9106", cache_path=cache_path)


def format_kline(kline: pd.DataFrame, freq: Freq):
Expand All @@ -41,9 +41,18 @@ def format_kline(kline: pd.DataFrame, freq: Freq):
"""
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'])
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

Expand All @@ -61,26 +70,26 @@ def get_symbols(name, **kwargs):
return symbols

if name == "ETF":
df = dc.etf_basic(v=2, fields='code,name', ttl=3600 * 6)
df = dc.etf_basic(v=2, fields="code,name", ttl=3600 * 6)
dfk = dc.pro_bar(trade_date="2024-04-02", asset="e", v=2)
df = df[df['code'].isin(dfk['code'])].reset_index(drop=True)
df = df[df["code"].isin(dfk["code"])].reset_index(drop=True)
symbols = [f"{row['code']}#ETF" for _, row in df.iterrows()]
return symbols

if name == "A股指数":
# 指数 https://s0cqcxuy3p.feishu.cn/wiki/KuSAweAAhicvsGk9VPTc1ZWKnAd
df = dc.index_basic(v=2, market='SSE,SZSE', ttl=3600 * 6)
df = dc.index_basic(v=2, market="SSE,SZSE", ttl=3600 * 6)
symbols = [f"{row['code']}#INDEX" for _, row in df.iterrows()]
return symbols

if name == "南华指数":
df = dc.index_basic(v=2, market='NH', ttl=3600 * 6)
symbols = [row['code'] for _, row in df.iterrows()]
df = dc.index_basic(v=2, market="NH", ttl=3600 * 6)
symbols = [row["code"] for _, row in df.iterrows()]
return symbols

if name == "期货主力":
kline = dc.future_klines(trade_date="20240402", ttl=3600 * 6)
return kline['code'].unique().tolist()
return kline["code"].unique().tolist()

if name.upper() == "ALL":
symbols = get_symbols("股票") + get_symbols("ETF")
Expand All @@ -90,12 +99,12 @@ def get_symbols(name, **kwargs):
raise ValueError(f"{name} 分组无法识别,获取标的列表失败!")


def get_min_future_klines(code, sdt, edt, freq='1m'):
def get_min_future_klines(code, sdt, edt, freq="1m"):
"""分段获取期货1分钟K线后合并"""
# dates = pd.date_range(start=sdt, end=edt, freq='1M')
dates = pd.date_range(start=sdt, end=edt, freq='120D')
dates = pd.date_range(start=sdt, end=edt, freq="120D")

dates = [d.strftime('%Y%m%d') for d in dates] + [sdt, edt]
dates = [d.strftime("%Y%m%d") for d in dates] + [sdt, edt]
dates = sorted(list(set(dates)))

rows = []
Expand All @@ -108,25 +117,25 @@ def get_min_future_klines(code, sdt, edt, freq='1m'):
rows.append(df)

df = pd.concat(rows, ignore_index=True)
df.rename(columns={'code': 'symbol'}, inplace=True)
df['dt'] = pd.to_datetime(df['dt'])
df = df.drop_duplicates(subset=['dt', 'symbol'], keep='last')
df.rename(columns={"code": "symbol"}, inplace=True)
df["dt"] = pd.to_datetime(df["dt"])
df = df.drop_duplicates(subset=["dt", "symbol"], keep="last")

if code in ['SFIC9001', 'SFIF9001', 'SFIH9001']:
if code in ["SFIC9001", "SFIF9001", "SFIH9001"]:
# 股指:仅保留 09:31 - 11:30, 13:01 - 15:00
dt1 = datetime.strptime("09:31:00", "%H:%M:%S")
dt2 = datetime.strptime("11:30:00", "%H:%M:%S")
c1 = (df['dt'].dt.time >= dt1.time()) & (df['dt'].dt.time <= dt2.time())
c1 = (df["dt"].dt.time >= dt1.time()) & (df["dt"].dt.time <= dt2.time())

dt3 = datetime.strptime("13:01:00", "%H:%M:%S")
dt4 = datetime.strptime("15:00:00", "%H:%M:%S")
c2 = (df['dt'].dt.time >= dt3.time()) & (df['dt'].dt.time <= dt4.time())
c2 = (df["dt"].dt.time >= dt3.time()) & (df["dt"].dt.time <= dt4.time())

df = df[c1 | c2].copy().reset_index(drop=True)
return df


def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs):
def get_raw_bars(symbol, freq, sdt, edt, fq="前复权", **kwargs):
"""获取 CZSC 库定义的标准 RawBar 对象列表
:param symbol: 标的代码
Expand All @@ -142,47 +151,51 @@ def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs):
>>> df = coo.get_raw_bars(symbol="000001.SH#INDEX", freq="日线", sdt="2001-01-01", edt="2021-12-31", fq='后复权', raw_bars=False)
"""
freq = czsc.Freq(freq)
raw_bars = kwargs.get('raw_bars', True)
ttl = kwargs.get('ttl', -1)
raw_bars = kwargs.get("raw_bars", True)
ttl = kwargs.get("ttl", -1)

if "SH" in symbol or "SZ" in symbol:
fq_map = {"前复权": "qfq", "后复权": "hfq", "不复权": None}
adj = fq_map.get(fq, None)

code, asset = symbol.split("#")

if freq.value.endswith('分钟'):
df = dc.pro_bar(code=code, sdt=sdt, edt=edt, freq='min', adj=adj, asset=asset[0].lower(), v=2, ttl=ttl)
df = df[~df['dt'].str.endswith("09:30:00")].reset_index(drop=True)
df.rename(columns={'code': 'symbol'}, inplace=True)
df['dt'] = pd.to_datetime(df['dt'])
return czsc.resample_bars(df, target_freq=freq, raw_bars=raw_bars, base_freq='1分钟')
if freq.value.endswith("分钟"):
df = dc.pro_bar(code=code, sdt=sdt, edt=edt, freq="min", adj=adj, asset=asset[0].lower(), v=2, ttl=ttl)
df = df[~df["dt"].str.endswith("09:30:00")].reset_index(drop=True)
df.rename(columns={"code": "symbol"}, inplace=True)
df["dt"] = pd.to_datetime(df["dt"])
return czsc.resample_bars(df, target_freq=freq, raw_bars=raw_bars, base_freq="1分钟")

else:
df = dc.pro_bar(code=code, sdt=sdt, edt=edt, freq='day', adj=adj, asset=asset[0].lower(), v=2, ttl=ttl)
df.rename(columns={'code': 'symbol'}, inplace=True)
df['dt'] = pd.to_datetime(df['dt'])
df = dc.pro_bar(code=code, sdt=sdt, edt=edt, freq="day", adj=adj, asset=asset[0].lower(), v=2, ttl=ttl)
df.rename(columns={"code": "symbol"}, inplace=True)
df["dt"] = pd.to_datetime(df["dt"])
return czsc.resample_bars(df, target_freq=freq, raw_bars=raw_bars)

if symbol.endswith("9001"):
# https://s0cqcxuy3p.feishu.cn/wiki/WLGQwJLWQiWPCZkPV7Xc3L1engg
if fq == "前复权":
logger.warning("期货主力合约暂时不支持前复权,已自动切换为后复权")

freq_rd = '1m' if freq.value.endswith('分钟') else '1d'
if freq.value.endswith('分钟'):
df = get_min_future_klines(code=symbol, sdt=sdt, edt=edt, freq='1m')
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, raw_bars=raw_bars, base_freq='1分钟')
freq_rd = "1m" if freq.value.endswith("分钟") else "1d"
if freq.value.endswith("分钟"):
df = get_min_future_klines(code=symbol, sdt=sdt, edt=edt, freq="1m")
if "amount" not in df.columns:
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, raw_bars=raw_bars, base_freq="1分钟")

else:
df = dc.future_klines(code=symbol, sdt=sdt, edt=edt, freq=freq_rd, ttl=ttl)
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'])
df.rename(columns={"code": "symbol"}, inplace=True)
if "amount" not in df.columns:
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, raw_bars=raw_bars)

if symbol.endswith(".NH"):
Expand All @@ -194,9 +207,9 @@ def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs):


@czsc.disk_cache(path=cache_path, ttl=-1)
def stocks_daily_klines(sdt='20170101', edt="20240101", **kwargs):
def stocks_daily_klines(sdt="20170101", edt="20240101", **kwargs):
"""获取全市场A股的日线数据"""
adj = kwargs.get('adj', 'hfq')
adj = kwargs.get("adj", "hfq")
sdt = pd.to_datetime(sdt).year
edt = pd.to_datetime(edt).year
years = [str(year) for year in range(sdt, edt + 1)]
Expand All @@ -208,18 +221,18 @@ def stocks_daily_klines(sdt='20170101', edt="20240101", **kwargs):
res.append(kline)

dfk = pd.concat(res, ignore_index=True)
dfk['dt'] = pd.to_datetime(dfk['dt'])
dfk = dfk.sort_values(['code', 'dt'], ascending=True).reset_index(drop=True)
if kwargs.get('exclude_bj', True):
dfk = dfk[~dfk['code'].str.endswith(".BJ")].reset_index(drop=True)
dfk["dt"] = pd.to_datetime(dfk["dt"])
dfk = dfk.sort_values(["code", "dt"], ascending=True).reset_index(drop=True)
if kwargs.get("exclude_bj", True):
dfk = dfk[~dfk["code"].str.endswith(".BJ")].reset_index(drop=True)

nxb = kwargs.get('nxb', [1, 2, 5])
nxb = kwargs.get("nxb", [1, 2, 5])
if nxb:
rows = []
for _, dfg in tqdm(dfk.groupby('code'), desc="计算NXB收益率", ncols=80, colour='green'):
czsc.update_nbars(dfg, numbers=nxb, move=1, price_col='open')
for _, dfg in tqdm(dfk.groupby("code"), desc="计算NXB收益率", ncols=80, colour="green"):
czsc.update_nbars(dfg, numbers=nxb, move=1, price_col="open")
rows.append(dfg)
dfk = pd.concat(rows, ignore_index=True)

dfk = dfk.rename(columns={'code': 'symbol'})
dfk = dfk.rename(columns={"code": "symbol"})
return dfk
58 changes: 38 additions & 20 deletions czsc/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
create_dt: 2022/2/24 16:17
describe: 数据工具
"""

from deprecated import deprecated
from .ts_cache import TsDataCache
from . import ts
from .base import *


@deprecated(version="1.0.0", reason="不再推荐使用,后续随整个 data 模块一起删除")
def get_symbols(dc: TsDataCache, step):
"""获取择时策略投研不同阶段对应的标的列表
Expand All @@ -19,31 +20,48 @@ def get_symbols(dc: TsDataCache, step):
:return:
"""
stocks = dc.stock_basic()
stocks_ = stocks[stocks['list_date'] < '2010-01-01'].ts_code.to_list()
stocks_ = stocks[stocks["list_date"] < "2010-01-01"].ts_code.to_list()
stocks_map = {
"index": ['000905.SH', '000016.SH', '000300.SH', '000001.SH', '000852.SH',
'399001.SZ', '399006.SZ', '399376.SZ', '399377.SZ', '399317.SZ', '399303.SZ'],
"index": [
"000905.SH",
"000016.SH",
"000300.SH",
"000001.SH",
"000852.SH",
"399001.SZ",
"399006.SZ",
"399376.SZ",
"399377.SZ",
"399317.SZ",
"399303.SZ",
],
"stock": stocks.ts_code.to_list(),
"check": ['000001.SZ'],
"check": ["000001.SZ"],
"train": stocks_[:200],
"valid": stocks_[200:600],
"etfs": ['512880.SH', '518880.SH', '515880.SH', '513050.SH', '512690.SH',
'512660.SH', '512400.SH', '512010.SH', '512000.SH', '510900.SH',
'510300.SH', '510500.SH', '510050.SH', '159992.SZ', '159985.SZ',
'159981.SZ', '159949.SZ', '159915.SZ'],
"etfs": [
"512880.SH",
"518880.SH",
"515880.SH",
"513050.SH",
"512690.SH",
"512660.SH",
"512400.SH",
"512010.SH",
"512000.SH",
"510900.SH",
"510300.SH",
"510500.SH",
"510050.SH",
"159992.SZ",
"159985.SZ",
"159981.SZ",
"159949.SZ",
"159915.SZ",
],
}

asset_map = {
"index": "I",
"stock": "E",
"check": "E",
"train": "E",
"valid": "E",
"etfs": "FD"
}
asset_map = {"index": "I", "stock": "E", "check": "E", "train": "E", "valid": "E", "etfs": "FD"}
asset = asset_map[step]
symbols = [f"{ts_code}#{asset}" for ts_code in stocks_map[step]]
return symbols



Loading

0 comments on commit 572aa03

Please sign in to comment.