金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 146|回复: 3

关于Python程序history_bars_date的获取昨日收盘价问题-4

[复制链接]

12

主题

49

帖子

49

积分

Rank: 1

等级: 新手上路

注册:
2025-4-8
曾用名:
发表于 2025-6-10 22:16 | 显示全部楼层 |阅读模式
老师好!我用如下程序获取获某期货的昨日收盘价,目前报如下错误,请帮忙指正可能的错误,非常感谢!21:18:16 > 无法获取 M00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 RB00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 FG00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 C00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 HC00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 SA00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 TA00 的当日收盘价,可能是数据缺失
21:18:16 > 无法获取 V00 的当日收盘价,可能是数据缺失


源程序如下:
def get_previous_day_close(context, contract, current_time):
    """获取前一交易日下午15:00的收盘价"""
    try:
        previous_day = current_time.date() - datetime.timedelta(days=1)
        previous_str = previous_day.strftime('%Y-%m-%d') + ' 15:00:00'
        bars = history_bars_date(contract, previous_str, previous_str, '1d', ['close'], skip_suspended=True, include_now=False)
        if bars is None or len(bars) < 1:
            print(f"无法获取 {contract} 的前一日收盘价,可能是数据缺失")
            return None
        if not all(isinstance(bar, dict) and 'close' in bar for bar in bars):
            print(f"合约 {contract} 的 bars 数据格式不正确")
            return None
        previous_close = bars[-1]['close']
        return previous_close
    except Exception as e:
        print(f"获取 {contract} 的前一日收盘价失败: {str(e)}")
        return None


回复

使用道具 举报

12

主题

49

帖子

49

积分

Rank: 1

等级: 新手上路

注册:
2025-4-8
曾用名:
 楼主| 发表于 2025-6-11 11:00 | 显示全部楼层
这是针对以上程序的进一步论证。以下是针对history_bars和history_bar_date进行联合使用的程序,目前只有history_bars返回值好使,但是返回的收盘价却是不准的。
def get_previous_day_close(context, contract, current_time):
    """获取前一交易日下午15:00的收盘价"""
    try:
        previous_day = current_time.date() - datetime.timedelta(days=1)
        current_day = current_time.date()
        previous_str = previous_day.strftime('%Y-%m-%d') + '00:00:00'
        current_str = current_day.strftime('%Y-%m-%d') + '00:00:00'
        bars = history_bars_date(contract, previous_str, current_str, '1d', ['close'], skip_suspended=True, include_now=False)
        
        if bars is None:
            print(f"无法获取 {contract} 的前一日收盘价,数据为 None")
            return None
        
        if isinstance(bars, np.ndarray):
            if bars.shape[0] >= 1:
                previous_close = float(bars[-1, 0] if bars.ndim == 2 else bars[-1])
            else:
                fb = history_bars(contract, 2, '1d', ['close'],skip_suspended=True, include_now=False)
                if isinstance(fb, np.ndarray) and fb.shape[0] >= 2:
                    return float(fb[-2,0] if fb.ndim==2 else fb[-2])
                if isinstance(fb, list) and len(fb) >= 2 and isinstance(fb[-2], dict):
                    return float(fb[-2]['close'])               
                print(f"无法获取 {contract} 的前一日收盘价,数组长度不足")
                return None
        elif isinstance(bars, list):
            if len(bars) >= 1 and isinstance(bars[-1], dict) and 'close' in bars[-1]:
                previous_close = float(bars[-1]['close'])
            else:
                print(f"无法获取 {contract} 的前一日收盘价,列表数据不符合要求")
                return None
        else:
            print(f"无法获取 {contract} 的前一日收盘价,数据类型未知: {type(bars)}")
            return None
        
        return previous_close
    except Exception as e:
        print(f"获取 {contract} 的前一日收盘价失败: {str(e)}")
        return None
回复

使用道具 举报

12

主题

49

帖子

49

积分

Rank: 1

等级: 新手上路

注册:
2025-4-8
曾用名:
 楼主| 发表于 2025-6-11 11:00 | 显示全部楼层
请老师帮忙指正!
回复

使用道具 举报

12

主题

49

帖子

49

积分

Rank: 1

等级: 新手上路

注册:
2025-4-8
曾用名:
 楼主| 发表于 2025-6-11 11:02 | 显示全部楼层
目前反映一个问题,就是history_bars返回值不准和history_bar_date无数据。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 微信登录

本版积分规则

手机版|小黑屋|上海金之塔信息技术有限公司 ( 沪ICP备13035422号 )

GMT+8, 2025-6-30 08:17 , Processed in 0.127454 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表