3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在期货量化交易策略中,**结算价(futures_sett_price)与持仓量(futures_positions)**是评估市场流动性、计算账户权益以及捕捉主力资金动向的核心数据。JoinQuant 提供了专门的接口 get_extras 来帮助开发者高效提取这些额外行情数据。
get_extras 用于获取多只标的在指定时间段内的特定额外数据(如基金净值、ST状态、期货结算价及持仓量等)。
get_extras(info, security_list, start_date='2015-01-01', end_date='2015-12-31', df=True, count=None)
| 参数 | 类型 | 说明 |
|---|---|---|
| info | str |
想要查询的信息类型: • 'futures_sett_price': 期货结算价• 'futures_positions': 期货持仓量 |
| security_list | list / str |
股票或期货合约代码列表(例如:['RB1909.XSGE', 'CU1909.XSGE']) |
| start_date | str / date |
开始日期(格式:YYYY-MM-DD),与 count 二选一 |
| end_date | str / date |
结束日期,默认为 '2015-12-31' |
| count | int |
截止 end_date 前的交易日数量,与 start_date 二选一 |
| df | bool |
返回格式:True 返回 pandas.DataFrame;False 返回 dict |
以下代码展示了如何在 JoinQuant 研究环境或策略中,调用 get_extras 提取螺纹钢(RB)主力合约的结算价和持仓量数据。
from jqdata import *
import datetime
# 1. 获取当前螺纹钢主力合约代码
dominant_symbol = get_dominant_future('RB')
print(f"当前螺纹钢主力合约: {dominant_symbol}")
# 2. 获取过去 5 个交易日的期货结算价
df_sett_price = get_extras(
info='futures_sett_price',
security_list=[dominant_symbol],
end_date=datetime.date.today(),
count=5,
df=True
)
print("\n--- 期货结算价 (DataFrame) ---")
print(df_sett_price)
# 3. 获取过去 5 个交易日的期货持仓量
df_positions = get_extras(
info='futures_positions',
security_list=[dominant_symbol],
end_date=datetime.date.today(),
count=5,
df=True
)
print("\n--- 期货持仓量 (DataFrame) ---")
print(df_positions)
持仓量的增减反映了多空双方资金交锋的剧烈程度。组合收盘价/结算价突破与持仓量放大,通常预示趋势形成。
def handle_data(context, data):
symbol = get_dominant_future('RB')
# 获取最近2天的持仓量与结算价
positions = get_extras('futures_positions', [symbol], end_date=context.current_dt, count=2)[symbol]
sett_price = get_extras('futures_sett_price', [symbol], end_date=context.current_dt, count=2)[symbol]
# 资金流入:持仓量增加
pos_increase = positions.iloc[-1] > positions.iloc[-2]
# 价格上涨:结算价上涨
price_up = sett_price.iloc[-1] > sett_price.iloc[-2]
if pos_increase and price_up:
log.info(f"{symbol} 量价齐升,放量上涨信号,考虑多头开仓")
AG9999.XSGE(主力合约)和 AG8888.XSGE(指数合约)主要用于行情分析,交易时需先调用 get_dominant_future(underlying_symbol) 获取具体的月份合约(如 AG2312.XSGE)。get_extras,当日的最终结算价需在收盘清算后更新。get_bars 或 get_price 时可同步获取 open_interest(持仓量)字段,但若需要跨品种历史结算价比对,get_extras 是最简便的专项 API。