3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在量化交易与短线策略中,龙虎榜数据是追踪游资炒作、机构加仓及筹码集中度的重要依据。QMT (迅投 Quant) 提供了原生 Python 接口 get_longhubang,方便投资者高效提取股票的上榜原因及买卖席位明细。
ContextInfo.get_longhubang 用于查询指定股票列表在特定时间范围内的龙虎榜数据及买卖营业部席位详情。
ContextInfo.get_longhubang(stock_list, startTime, endTime)
['000002.SZ', '600000.SH'])。'YYYYMMDD')。'YYYYMMDD')。接口返回一个 DataFrame,核心字段包括:
DataFrame 结构)DataFrame 结构)其中 buyTraderBooth / sellTraderBooth 结构中包含:
traderName: 交易营业部/机构名称buyAmount: 买入金额sellAmount: 卖出金额buyPercent: 买入占总成交比例sellPercent: 卖出占总成交比例rank: 席位排名 (1-5)direction: 买卖方向以下示例展示了如何在 QMT 策略中获取并打印指定股票的龙虎榜席位明细:
#encoding:gbk
import pandas as pd
def init(ContextInfo):
# 设置初始股票池
ContextInfo.set_universe(['000002.SZ'])
def handlebar(ContextInfo):
# 仅在最后一根 K 线运行一次
if not ContextInfo.is_last_bar():
return
stock = '000002.SZ'
start_date = '20230101'
end_date = '20231231'
# 获取龙虎榜数据
lhb_df = ContextInfo.get_longhubang([stock], start_date, end_date)
if lhb_df.empty:
print(f"{stock} 在指定时间段内无龙虎榜数据")
return
for idx, row in lhb_df.iterrows():
print(f"=== 日期/记录: {idx} ===")
print(f"上榜原因: {row.get('reason')}")
print(f"收盘价: {row.get('close')}, 涨跌幅: {row.get('spreadRate')}%")
# 解析买方前五席位
buy_booths = row.get('buyTraderBooth')
if isinstance(buy_booths, pd.DataFrame) and not buy_booths.empty:
print("--- 买入前五席位 ---")
for _, booth in buy_booths.iterrows():
print(f"排名{booth['rank']}: {booth['traderName']} | 买入: {booth['buyAmount']/10000:.2f}万")
# 解析卖方前五席位
sell_booths = row.get('sellTraderBooth')
if isinstance(sell_booths, pd.DataFrame) and not sell_booths.empty:
print("--- 卖出前五席位 ---")
for _, booth in sell_booths.iterrows():
print(f"排名{booth['rank']}: {booth['traderName']} | 卖出: {booth['sellAmount']/10000:.2f}万")
traderName 中包含“机构专用”的席位,分析机构净买入金额与后续股价走势的相关性。