3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在聚宽(JoinQuant)量化交易平台中,get_current_tick 是用于获取最新 Tick 数据的核心 API。通过合理使用其 dt 参数,开发者可以轻松精准地获取历史指定时刻(如 10:00:00)最接近的一条 Tick 盘口快照数据。
get_current_tick(security, dt=None, df=False)
datetime.datetime 格式的时间对象。指定时表示返回离该指定时刻最近的一条 Tick 数据;默认为 None,即获取当前最新时刻的 Tick。False(返回 Tick 对象或字典),设置为 True 时返回 DataFrame 格式。注意:该 API 仅支持在回测及模拟交易环境(
run_daily、handle_data或handle_tick)中使用,不支持在投资研究环境中独立运行。
以下示例展示了如何在每日策略运行过程中,指定获取 2018-11-01 10:00:00 或策略当日 10:00:00 的历史 Tick 快照:
import datetime
def initialize(context):
# 设置基准与动态复权
set_benchmark('000300.XSHG')
set_option('use_real_price', True)
# 注册定时运行函数
run_daily(market_open, time='10:05')
def market_open(context):
# 1. 获取指定历史具体日期时刻的 Tick 快照
historical_dt = datetime.datetime(2018, 11, 1, 10, 0, 0)
tick_hist = get_current_tick('000001.XSHE', dt=historical_dt)
if tick_hist:
log.info("2018-11-01 10:00:00 临近价格: %s", tick_hist.current)
# 2. 获取回测/模拟当日 10:00:00 的 Tick 快照
current_date = context.current_dt.date()
today_10am = datetime.datetime.combine(current_date, datetime.time(10, 0, 0))
tick_today_10am = get_current_tick('000001.XSHE', dt=today_10am)
if tick_today_10am:
log.info("当日 10:00:00 临近 Tick 价格: %s, 成交量: %s", tick_today_10am.current, tick_today_10am.volume)
当 df=False 且传入单个标的时,返回一个 Tick 对象,包含以下主要盘口属性:
current:当前价 / 最新价datetime:该 Tick 发生的实际时间open / high / low:截至当前时刻的开盘价、最高价、最低价volume / money:截至当前时刻的累计成交量和成交额a1_p ~ a5_p / a1_v ~ a5_v:卖一至卖五的挂单价格与挂单量b1_p ~ b5_p / b1_v ~ b5_v:买一至买五的挂单价格与挂单量None,建议在代码中加入非空判断。'RB1909.XSGE'),不可使用主力合约(如 'RB9999.XSGE')或指数合约。