From aedb0a15efd4bcce90b19f6ec4948a3cac8185ee Mon Sep 17 00:00:00 2001 From: zengbin93 Date: Sun, 8 Oct 2023 23:29:21 +0800 Subject: [PATCH] 0.9.31 update --- czsc/strategies.py | 7 ++---- czsc/utils/bar_generator.py | 50 ++++--------------------------------- 2 files changed, 7 insertions(+), 50 deletions(-) diff --git a/czsc/strategies.py b/czsc/strategies.py index 98bd8305e..8e26bf387 100644 --- a/czsc/strategies.py +++ b/czsc/strategies.py @@ -94,11 +94,8 @@ def init_bar_generator(self, bars: List[RawBar], **kwargs): freqs = self.sorted_freqs if bg is None: - if base_freq in ["日线", "周线", "月线", "季线", "年线"]: - market = "默认" - else: - uni_times = sorted(list({x.dt.strftime("%H:%M") for x in bars})) - _, market = check_freq_and_market(uni_times, freq=base_freq) + uni_times = sorted(list({x.dt.strftime("%H:%M") for x in bars})) + _, market = check_freq_and_market(uni_times, freq=base_freq) sdt = pd.to_datetime(kwargs.get("sdt", "20200101")) n = int(kwargs.get("n", 500)) diff --git a/czsc/utils/bar_generator.py b/czsc/utils/bar_generator.py index 3dc401d78..dc7899e7b 100644 --- a/czsc/utils/bar_generator.py +++ b/czsc/utils/bar_generator.py @@ -114,7 +114,7 @@ def freq_end_date(dt, freq: Union[Freq, AnyStr]): return dt -def freq_end_time_V230921(dt: datetime, freq: Union[Freq, AnyStr], market="A股") -> datetime: +def freq_end_time(dt: datetime, freq: Union[Freq, AnyStr], market="A股") -> datetime: """A股与期货市场精确的获取 dt 对应的K线周期结束时间 :param dt: datetime @@ -140,42 +140,6 @@ def freq_end_time_V230921(dt: datetime, freq: Union[Freq, AnyStr], market="A股" return freq_end_date(dt.date(), freq) -def freq_end_time(dt: datetime, freq: Union[Freq, AnyStr]) -> datetime: - """获取 dt 对应的K线周期结束时间 - - :param dt: datetime - :param freq: Freq - :return: datetime - """ - if not isinstance(freq, Freq): - freq = Freq(freq) - dt = dt.replace(second=0, microsecond=0) - - if freq in [Freq.F1, Freq.F5, Freq.F15, Freq.F30, Freq.F60]: - m = int(str(freq.value).strip("分钟")) - if m < 60: - if (dt.hour == 15 and dt.minute == 0) or (dt.hour == 11 and dt.minute == 30): - return dt - - delta_m = dt.minute % m - if delta_m != 0: - dt += timedelta(minutes=m - delta_m) - return dt - - else: - dt_span = { - 60: ["01:00", "2:00", "3:00", '10:30', "11:30", "14:00", "15:00", "22:00", "23:00", "23:59"], - } - for v in dt_span[m]: - hour, minute = v.split(":") - edt = dt.replace(hour=int(hour), minute=int(minute)) - if dt <= edt: - return edt - - # 处理 日、周、月、季、年 的结束时间 - return freq_end_date(dt.date(), freq) - - def resample_bars(df: pd.DataFrame, target_freq: Union[Freq, AnyStr], raw_bars=True, **kwargs): """将df中的K线序列转换为目标周期的K线序列 @@ -200,13 +164,10 @@ 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) - if base_freq in [Freq.D.value, Freq.W.value, Freq.M.value, Freq.S.value, Freq.Y.value]: - market = kwargs.get('market', '默认') - else: - uni_times = df['dt'].apply(lambda x: x.strftime("%H:%M")).unique().tolist() - _, market = check_freq_and_market(uni_times, freq=base_freq) + 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) - df['freq_edt'] = df['dt'].apply(lambda x: freq_end_time_V230921(x, target_freq, market)) + df['freq_edt'] = df['dt'].apply(lambda x: freq_end_time(x, target_freq, market)) dfk1 = df.groupby('freq_edt').agg( {'symbol': 'first', 'dt': 'last', 'open': 'first', 'close': 'last', 'high': 'max', 'low': 'min', 'vol': 'sum', 'amount': 'sum', 'freq_edt': 'last'}) @@ -276,8 +237,7 @@ def _update_freq(self, bar: RawBar, freq: Freq) -> None: :param freq: 目标周期 :return: """ - # freq_edt = freq_end_time(bar.dt, freq) - freq_edt = freq_end_time_V230921(bar.dt, freq, self.market) + freq_edt = freq_end_time(bar.dt, freq, self.market) if not self.bars[freq.value]: bar_ = RawBar(symbol=bar.symbol, freq=freq, dt=freq_edt, id=0, open=bar.open,