🚀 AI 一键生成 joinquant 策略代码
立即体验
👉 AI 生成代码 👈

为什么 JoinQuant 实盘中 get_price 取不到当天的实时价格?

joinquant | 发布时间: 2026/5/29
以下内容由 EasyQuant 生成。

问题描述

get_price实盘取不到当时的价格?

get_price(stock_list,frequency=daily,fields=['close','high','low','money'], count=1, end_date=today, panel=False)
@JoinQuant-Supercritical 以前用上面这一句在实盘中拿到的是当天的数据,close是当时的实时价格。但最近几天开始,拿到的是昨天的数据,close是昨天的收盘价。请帮忙给看看。

解决方案

在 JoinQuant (聚宽) 平台中,使用 get_price 获取日线级别(frequency='daily''1d')数据时,默认情况下是不包含当天未收盘的数据的。如果您传入的 end_date 只有日期(例如 today),系统会默认时间为 00:00:00,因此返回的最后一条数据实际上是昨天的收盘数据。

如果您之前能取到,可能是因为当时传入的时间参数带有具体的时分秒,或者平台底层数据接口有过微调以规范日线数据的对齐方式。为了在实盘或模拟盘中稳定、准确地获取当前的实时价格,建议使用以下几种专门为此设计的 API:

解决方案一:使用 get_current_data()(推荐)

如果您只需要获取当前的最新价、涨跌停价等切片数据,最标准且高效的方法是使用 get_current_data()。这个 API 专门用于获取当前单位时间(当天/当前分钟)的实时状态。

def handle_data(context, data):
    # 获取当前所有标的的数据字典
    current_data = get_current_data()
    
    for stock in stock_list:
        # 获取最新价
        current_price = current_data[stock].last_price
        print(f"{stock} 的最新价为: {current_price}")

解决方案二:使用 get_bars() 并设置 include_now=True

如果您不仅需要最新价,还需要当天的开盘价、最高价、最低价等,可以使用 get_bars 函数,并将 include_now 参数设置为 True。这样可以获取包含当前未结束的 Bar 的数据。

def handle_data(context, data):
    # 获取包含当天的日线数据
    bars = get_bars(stock_list, count=1, unit='1d', 
                    fields=['date', 'open', 'high', 'low', 'close'], 
                    include_now=True, df=True)
    
    print(bars)

解决方案三:修改 get_price 的参数(获取分钟数据)

如果您坚持使用 get_price,并且想要获取到当前时刻的价格,您需要将频率改为分钟级别(frequency='1m'),并且将 end_date 精确到当前时刻(context.current_dt)。

def handle_data(context, data):
    # 获取截止到当前分钟的数据
    df = get_price(stock_list, frequency='1m', 
                   fields=['close', 'high', 'low', 'money'], 
                   count=1, end_date=context.current_dt, panel=False)
    
    print(df)

总结
在量化策略编写中,严格区分“历史已收盘数据”和“当前实时切片数据”是非常重要的。获取历史日线请用 historyattribute_historyget_price(日频);获取当前最新价请务必使用 get_current_data(),这样可以有效避免未来函数和数据对齐带来的困扰。