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

joinquant 平台如何实时获取期货最新 Tick 盘口数据?(附 Python 源码)

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

问题描述

请根据以下 SEO 主题生成完整页面内容:

Title: joinquant 期货高频数据:get_current_tick 获取最新盘口与 Tick 策略

Question: 如何在 joinquant 的盘中策略中实时获取最新的 Tick 盘口数据?

解决方案

joinquant 平台实时获取最新 Tick 盘口数据与策略实现指南

在 JoinQuant(聚宽)量化交易平台中,若要进行高频交易或捕捉盘口瞬时微观结构变动,可以通过 get_current_tick 函数配合 Tick 级别的事件订阅实现实时盘口数据的获取与策略撮合。


一、get_current_tick API 详解

get_current_tick 用于获取离指定时刻最近的一条 Tick 快照数据,包含了买卖五档(期货为一档)、最新价、成交量等盘口信息。

1. 函数签名

get_current_tick(security, dt=None, df=False)

2. 参数说明

  • security: 标的代码,支持单个标的字符串或标的列表。期货必须使用具体合约代码(如 'RB1909.XSGE'),不可使用主力合约(如 'RB9999.XSGE')。
  • dt: datetime 格式时刻,指定时返回离该时刻最近的 Tick。默认为 None(当前时刻)。
  • df: 是否返回 DataFrame 格式。默认为 False(返回 Tick 对象或字典)。

3. 返回值(Tick 对象属性)

  • code: 标的代码
  • datetime: Tick 发生时间
  • current: 最新价
  • a1_p ~ a5_p / a1_v ~ a5_v: 卖一至卖五价/量(期货仅买一卖一)
  • b1_p ~ b5_p / b1_v ~ b5_v: 买一至买五价/量
  • volume: 累计成交量
  • position: 累计持仓量(仅期货)

注意get_current_tick 依赖上下文,仅支持在回测与模拟交易的 run_dailyhandle_datahandle_tick 中调用。


二、Tick 级策略运行机制

编写 Tick 策略时,需注意以下运行逻辑:

  1. 真实价格模式:Tick 级回测必须开启动态复权模式 set_option('use_real_price', True)
  2. 订阅标的:通过 subscribe(security, 'tick') 订阅具体合约的 Tick 事件。
  3. 事件驱动回调:每次盘面产生新的 Tick 时,系统会自动触发 handle_tick(context, tick)

三、完整 Python 策略源码示例

以下示例展示了如何在聚宽平台订阅螺纹钢期货合约(RB1909.XSGE),并在 handle_tick 回调中读取实时盘口数据进行交易决策:

# 导入聚宽数据库
import jqdata

def initialize(context):
    # 设定真实价格模式(Tick策略必须设置)
    set_option('use_real_price', True)
    
    # 设置期货账户子仓位
    init_cash = context.portfolio.starting_cash
    set_subportfolios([SubPortfolioConfig(cash=init_cash, type='futures')])
    
    # 保存目标期货合约(必须为具体合约)
    g.security = 'RB1909.XSGE'
    
    # 设定盘前与盘后定时任务
    run_daily(before_market_open, time='08:30', reference_security='RB9999.XSGE')
    run_daily(after_market_close, time='15:30', reference_security='RB9999.XSGE')

def before_market_open(context):
    # 开盘前订阅 Tick 事件
    subscribe(g.security, 'tick')
    log.info("已订阅 %s 的 Tick 数据" % g.security)

def handle_tick(context, tick):
    # 获取当前标的的最新 Tick 对象
    current_tick = get_current_tick(g.security)
    
    if current_tick is None:
        return
    
    # 读取盘口数据
    latest_price = current_tick.current
    ask_price_1 = current_tick.a1_p  # 卖一价
    bid_price_1 = current_tick.b1_p  # 买一价
    ask_volume_1 = current_tick.a1_v # 卖一量
    bid_volume_1 = current_tick.b1_v # 买一量
    
    # 打印盘口信息
    log.info("最新价: %.2f | 买一: %.2f(量:%d) | 卖一: %.2f(量:%d)" % 
             (latest_price, bid_price_1, bid_volume_1, ask_price_1, ask_volume_1))
    
    # 简单示例策略:如果买一量远大于卖一量,尝试开多仓
    positions = context.portfolio.long_positions
    if bid_volume_1 > ask_volume_1 * 3 and g.security not in positions:
        order(g.security, 1, side='long')
        log.info("买盘力量强劲,开多单 1 手")

def after_market_close(context):
    # 收盘后取消订阅
    unsubscribe_all()
    log.info("已取消所有 Tick 订阅")

四、常见问题与注意事项

  1. 主力合约说明:无法直接订阅或对 RB9999.XSGE 等主力/指数合约下单,需通过 get_dominant_future('RB') 动态获取当前映射的具体合约代号。
  2. 调用频率:期货 Tick 约每 0.5 秒推送一次快照,避免在 handle_tick 中编写高耗时的耗时计算或复杂数据库查询,以免触发 1800 秒运行超时限制。