Skip to content

Commit

Permalink
0.9.31 update
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Oct 10, 2023
1 parent bc1d813 commit 6e41c31
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
7 changes: 5 additions & 2 deletions czsc/utils/bar_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def check_freq_and_market(time_seq: List[AnyStr], freq: Optional[AnyStr] = None)
if freq in ['日线', '周线', '月线', '季线', '年线']:
return freq, "默认"

if freq == '1分钟':
time_seq.extend(['14:57', '14:58', '14:59', '15:00'])

time_seq = sorted(list(set(time_seq)))
assert len(time_seq) >= 2, "time_seq长度必须大于等于2"

Expand Down Expand Up @@ -127,8 +130,8 @@ def freq_end_time(dt: datetime, freq: Union[Freq, AnyStr], market="A股") -> dat
edt = dt.replace(hour=int(h), minute=int(m))
return edt

if not ("15:00" > hm > "09:00") and market == "期货":
dt = next_trading_date(dt, n=1)
# if not ("15:00" > hm > "09:00") and market == "期货":
# dt = next_trading_date(dt, n=1)

return freq_end_date(dt.date(), freq)

Expand Down
24 changes: 17 additions & 7 deletions examples/develop/index_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ def index_composition_by_base_point(klines, weights=None, base_point=1000):
"""
klines["dt"] = pd.to_datetime(klines["dt"])

data = []
for _, kline in klines.groupby("symbol"):
kline = kline.sort_values("dt", ascending=True).reset_index(drop=True)
kline["returns"] = kline["close"].pct_change()
kline["returns"] = kline["returns"].fillna(0)
data.append(kline)
klines = pd.concat(data, ignore_index=True)
if 'returns' not in klines.columns.tolist():
data = []
for _, kline in klines.groupby("symbol"):
kline = kline.sort_values("dt", ascending=True).reset_index(drop=True)
kline["returns"] = kline["close"].pct_change()
kline["returns"] = kline["returns"].fillna(0)
data.append(kline)
klines = pd.concat(data, ignore_index=True)

returns = klines.pivot_table(index="dt", columns="symbol", values="returns")

if weights is None:
Expand Down Expand Up @@ -93,6 +95,8 @@ def test_index_composition_by_base_point():
for symbol in symbols:
bars = get_raw_bars(symbol=symbol, freq="1分钟", sdt="20210104 09:31", edt="20210107 13:10")
df = pd.DataFrame(bars)
df['returns'] = df['close'].pct_change()
df = df.dropna(subset=['returns'])
df.drop(columns=["freq", "cache", 'id'], inplace=True)
data.append(df)
klines = pd.concat(data, ignore_index=True)
Expand All @@ -106,6 +110,12 @@ def test_index_composition_by_base_point():
index2 = index_composition_by_base_point(klines=klines.copy(), weights=weights)
assert int(index2['vol'].iloc[-1]) == 200930300

weights = pd.DataFrame({"dt": ['2020-01-03 10:10:00', '2021-01-04 10:54:00', '2021-01-05 12:10:00']})
for symbol in symbols:
weights[symbol] = [random.uniform(0.1, 0.3) for _ in range(3)]
index2 = index_composition_by_base_point(klines=klines.copy(), weights=weights)
assert int(index2['vol'].iloc[-1]) == 200930300

try:
weights = pd.DataFrame({"dt": ['2021-01-04 10:10:00', '2021-01-04 10:54:00', '2021-01-05 12:10:00']})
for symbol in symbols:
Expand Down
8 changes: 5 additions & 3 deletions examples/test_offline/test_resample_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ def test_read():

def test_check_freq_and_market():
from czsc.utils.bar_generator import check_freq_and_market
gruop_name = "期货主力"
# gruop_name = "中证500成分股"
# gruop_name = "期货主力"
gruop_name = "中证500成分股"
files = Path(fr"D:\CZSC投研数据\{gruop_name}").glob("*.parquet")
for file in files:
df = pd.read_parquet(file)
time_seq = sorted(list({x.strftime("%H:%M") for x in df['dt']}))
x_freq, market = check_freq_and_market(time_seq=time_seq)
x_freq, market = check_freq_and_market(time_seq=time_seq, freq='1分钟')
print(file.stem, x_freq, market)
if market == "默认":
print(time_seq)


def get_future_times():
Expand Down
4 changes: 1 addition & 3 deletions test/test_bar_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def test_check_freq_and_market():
assert check_freq_and_market(time_seq) == ('120分钟', '期货')

time_seq = [
'09:25',
'09:30',
'09:31',
'09:32',
'09:33',
Expand Down Expand Up @@ -257,7 +255,7 @@ def test_check_freq_and_market():
'14:58',
'15:00',
]
assert check_freq_and_market(time_seq) == ('1分钟', 'A股')
assert check_freq_and_market(time_seq, freq='1分钟') == ('1分钟', 'A股')

for key, values in freq_market_times.items():
assert check_freq_and_market(values) == (key.split("_")[0], key.split("_")[1])
Expand Down

0 comments on commit 6e41c31

Please sign in to comment.