3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在聚宽(JoinQuant)平台中,可以通过使用 API 函数 get_ticks() 来获取商品期货及金融期货的历史 Tick 行情数据(支持 2010 年至今的数据)。
get_ticks(security, end_dt, start_dt=None, count=None, fields=['time', 'current', 'high', 'low', 'volume', 'money'], skip=True, df=False)
'AU1812.XSGE' 或 'RB2005.XSGE')。注意: Tick 接口需要使用具体合约代码,不支持直接传入主力合约或指数合约代码。若需获取主力合约 Tick,可结合 get_dominant_future() 先查询主力合约。'2018-07-03 15:30:00'。time, current, open, high, low, volume, money, position(持仓量), a1_p, a1_v(卖一价量), b1_p, b1_v(买一价量)。True,过滤无成交变化的 Tick;设为 False 则保留盘口变化的 Tick。False(返回 numpy.ndarray),设置为 True 时返回 pandas.DataFrame 格式。from jqdata import *
# 获取螺纹钢 RB1809 合约在指定时间段内的 Tick 数据
tick_df = get_ticks(
security='RB1809.XSGE',
start_dt='2018-07-02 21:00:00',
end_dt='2018-07-03 09:15:00',
fields=['time', 'current', 'volume', 'position', 'a1_p', 'a1_v', 'b1_p', 'b1_v'],
df=True
)
print(tick_df.head())
如果需要在策略运行中获取主力合约的 Tick 数据,可以按以下方式处理:
from jqdata import *
# 1. 获取当前品种的主力合约代码(例如黄金 AU)
dominant_symbol = get_dominant_future('AU', date='2018-07-02')
# 2. 根据主力合约获取 Tick 数据
ticks = get_ticks(dominant_symbol, end_dt='2018-07-02 23:00:00', count=10, df=True)
print(ticks)
在运行频率为 tick 的策略中,可以使用 handle_tick 函数配合 get_current_tick 或 subscribe 监听盘口:
# 运行频率设置为 tick
def initialize(context):
# 设定子账户为期货类型
set_subportfolios([SubPortfolioConfig(cash=context.portfolio.starting_cash, type='futures')])
g.symbol = 'RB1909.XSGE'
def before_trading_start(context):
# 订阅 Tick 事件
subscribe(g.symbol, 'tick')
def handle_tick(context, tick):
# 获取当前最新 Tick 数据
current_tick = get_current_tick(g.symbol)
if current_tick:
log.info(f"最新价: {current_tick.current}, 买一价: {current_tick.b1_p}, 卖一价: {current_tick.a1_p}")
def after_trading_end(context):
unsubscribe_all()
set_option('use_real_price', True))。b1_p/b1_v, a1_p/a1_v)数据,频次为 0.5 秒一次切片。RB9999.XSGE(主力连续)或 RB8888.XSGE(品种指数)直接调用 get_ticks 或下单。