A股量化数据工程:从 REST 接口到策略信号(第 27 篇):龙虎榜机构席位追踪
前言
龙虎榜里最值得看的,是机构专用席位在买卖什么——那是专业资金真金白银的选择。接口hilh/jgxw(机构席位追踪)、xwmx(席位成交明细)给出买卖额与次数,本篇自研机构净买入信号,定位被机构看好的标的。
一、接口原始字段梳理
1)机构席位追踪
接口地址:https://api.biyingapi.com/hilh/jgxw/{近几日}/{LICENCE}
| 字段 | 说明 |
|---|---|
| dm | 股票代码 |
| mc | 股票名称 |
| be | 买入额 |
| bcount | 买入次数 |
| se | 卖出额 |
| scount | 卖出次数 |
| ende | 净额(接口原始字段) |
2)机构席位成交明细
接口地址:https://api.biyingapi.com/hilh/xwmx/{LICENCE}
| 字段 | 说明 |
|---|---|
| dm | 股票代码 |
| mc | 股票名称 |
| t | 时间 |
| buy | 买入额 |
| sell | 卖出额 |
| type | 类型(接口原始字段) |
二、自研衍生统计指标
- 机构净买入:
be − se,正为机构净买入,越高越被看好。 - 买卖比:
be / se,>1 买方主导。 - 机构偏好股:按
be − se排序 Top,与第 23 篇三类共振交叉验证。
净额与比值为自研,原始
be/se为接口输入(接口也返回ende净额,可核对)。
三、数据表设计
CREATETABLEIFNOTEXISTSlhb_inst_seat(dmTEXT,windowTEXT,beREAL,bcountINTEGER,seREAL,scountINTEGER,netREAL,PRIMARYKEY(dm,window));CREATETABLEIFNOTEXISTSlhb_inst_detail(dmTEXT,tTEXT,buyREAL,sellREAL,typeTEXT,PRIMARYKEY(dm,t));四、完整代码
importrequestsimportsqlite3importtime LICENCE="YOUR_LICENCE"BASE="https://api.biyingapi.com"DB_PATH="quant.db"defbiying_api_get_retry(url,timeout=15,max_retry=3):foriinrange(max_retry):try:r=requests.get(url,timeout=timeout)r.raise_for_status()returnr.json()exceptExceptionase:ifi==max_retry-1:raisetime.sleep(1+i)deffetch(path,arg=""):d=biying_api_get_retry(f"{BASE}/hilh/{path}/{arg}{LICENCE}")returnd.get("data",[])ifisinstance(d,dict)else[]defnet_buy(rows):out=[]forrinrows:net=(r.get("be")or0)-(r.get("se")or0)out.append((r.get("dm"),r.get("mc"),net))returnoutif__name__=="__main__":seat=fetch("jgxw","5/")# 近 5 日mx=fetch("xwmx")con=sqlite3.connect(DB_PATH)forrinseat:net=(r.get("be")or0)-(r.get("se")or0)con.execute("INSERT OR REPLACE INTO lhb_inst_seat(dm,window,be,bcount,se,scount,net)""VALUES(?,?,?,?,?,?,?)",[r.get("dm"),"5",r.get("be"),r.get("bcount"),r.get("se"),r.get("scount"),net])forrinmx:con.execute("INSERT OR REPLACE INTO lhb_inst_detail(dm,t,buy,sell,type) VALUES(?,?,?,?,?)",[r.get("dm"),r.get("t"),r.get("buy"),r.get("sell"),r.get("type")])con.commit();con.close()ranked=sorted(net_buy(seat),key=lambdax:x[2],reverse=True)[:5]print("机构净买入 Top5:")fordm,mc,netinranked:print(f"{dm}{mc}净买入={net}")五、关键业务说明
ende可核对:接口已返回净额,本篇自研be−se与其一致,双重确认口径。- 窗口要统一:
jgxw的「近几日」参数决定统计窗口,比较时固定同一窗口。 - 与三类共振互补:机构席位净买入(短线) + 第 23 篇基金/社保/QFII 重仓(中长线),构成机构视角双周期。
六、拓展练习
- 把
lhb_inst_seat净买入与第 23 篇共振股做交集,找「短线席位 + 长线机构」双确认。 - 用
xwmx明细算单一席位买卖节奏(结合type)识别操作风格(仅统计)。 - 跟踪机构净买入 Top 股后续 3 日相对收益(仅统计演示)。
下篇预告
A股量化数据工程:从 REST 接口到策略信号(第 28 篇):大宗交易与解禁数据
下一篇用
hitc/dzjy与jjxs拉取大宗交易折价与解禁压力,自研「大宗折价率」与「解禁冲击」指标,补齐事件风险维度。
免责申明:本篇全部代码、接口调用仅作为量化编程学习演示,仅为数据演示,不构成投资建议。市场有风险,投资需谨慎。