Skip to content

Commit

Permalink
0.9.36 fix resample_bars
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Nov 13, 2023
1 parent 5eb27e3 commit 6bc5846
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
19 changes: 18 additions & 1 deletion czsc/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@


def remove_include(k1: NewBar, k2: NewBar, k3: RawBar):
"""去除包含关系:输入三根k线,其中k1和k2为没有包含关系的K线,k3为原始K线"""
"""去除包含关系:输入三根k线,其中k1和k2为没有包含关系的K线,k3为原始K线
处理逻辑如下:
1. 首先,通过比较k1和k2的高点(high)的大小关系来确定direction的值。如果k1的高点小于k2的高点,
则设定direction为Up;如果k1的高点大于k2的高点,则设定direction为Down;如果k1和k2的高点相等,
则创建一个新的K线k4,与k3具有相同的属性,并返回False和k4。
2. 接下来,判断k2和k3之间是否存在包含关系。如果存在,则根据direction的值进行处理。
- 如果direction为Up,则选择k2和k3中的较大高点作为新K线k4的高点,较大低点作为低点,较大高点所在的时间戳(dt)作为k4的时间戳。
- 如果direction为Down,则选择k2和k3中的较小高点作为新K线k4的高点,较小低点作为低点,较小低点所在的时间戳(dt)作为k4的时间戳。
- 如果direction的值不是Up也不是Down,则抛出ValueError异常。
3. 根据上述处理得到的高点、低点、开盘价(open_)、收盘价(close),计算新K线k4的成交量(vol)和成交金额(amount),
并将k2中除了与k3时间戳相同的元素之外的其他元素与k3一起作为k4的元素列表(elements)。
4. 返回一个布尔值和新的K线k4。如果k2和k3之间存在包含关系,则返回True和k4;否则返回False和k4,其中k4与k3具有相同的属性。
"""
if k1.high < k2.high:
direction = Direction.Up
elif k1.high > k2.high:
Expand Down
7 changes: 5 additions & 2 deletions czsc/utils/bar_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ def resample_bars(df: pd.DataFrame, target_freq: Union[Freq, AnyStr], raw_bars=T
target_freq = Freq(target_freq)

base_freq = kwargs.get('base_freq', None)
uni_times = df['dt'].head(2000).apply(lambda x: x.strftime("%H:%M")).unique().tolist()
_, market = check_freq_and_market(uni_times, freq=base_freq)
if target_freq.value.endswith("分钟"):
uni_times = df['dt'].head(2000).apply(lambda x: x.strftime("%H:%M")).unique().tolist()
_, market = check_freq_and_market(uni_times, freq=base_freq)
else:
market = "默认"

df['freq_edt'] = df['dt'].apply(lambda x: freq_end_time(x, target_freq, market))
dfk1 = df.groupby('freq_edt').agg(
Expand Down
9 changes: 9 additions & 0 deletions examples/test_offline/test_qmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
sys.path.insert(0, '../..')
import czsc
from czsc.connectors import qmt_connector as qmc

czsc.welcome()

df = qmc.get_raw_bars(symbol="000001.SZ", freq="日线", sdt="20210101", edt="20210131")
df = qmc.get_raw_bars(symbol="000001.SZ", freq="1分钟", sdt="20210101", edt="20210131")

0 comments on commit 6bc5846

Please sign in to comment.