基于Python DataFrame筛选午后等于或接近早盘最高价的时刻
解决方案
核心思路
要确认早盘(9:30-12:00)最高价在午后(12:00-16:30)是否重现或接近(低于2%以内),需分两步筛选数据:
- 先提取午后时段的DataFrame子集
- 再筛选出价格符合阈值条件的记录,提取对应时刻
代码实现
假设你的self.minute_olhc_df包含datetime类型的时间列(命名为time)和价格列(以最高价列high为例,若你用收盘价close可自行替换),代码如下:
# 1. 筛选午后12:00至16:30的数据集 afternoon_df = self.minute_olhc_df[ (self.minute_olhc_df['time'] >= '12:00:00') & (self.minute_olhc_df['time'] <= '16:30:00') ] # 2. 计算价格阈值:早盘最高价的98%(低于2%以内的下限) price_threshold = morning_high_price * 0.98 # 3. 筛选价格满足条件的记录:价格 >= 阈值(包含等于早盘最高价的情况) target_records = afternoon_df[afternoon_df['high'] >= price_threshold] # 4. 提取所有符合条件的时刻 target_times = target_records['time'] # 可选:打印结果查看 print("午后符合条件的时刻:") print(target_times.tolist())
关键细节说明
- 确保时间列是
datetime类型:如果你的time列是字符串,先转换为datetime:self.minute_olhc_df['time'] = pd.to_datetime(self.minute_olhc_df['time']) - 价格列选择:若你关注的是收盘价而非最高价,只需将代码中的
high替换为close即可 - 阈值逻辑:
price >= morning_high_price * 0.98既包含了价格等于早盘最高价的情况,也覆盖了低于最高价2%以内的区间(即价格在早盘最高价的98%~100%之间)
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

