场景描述:在取到的高频的行情数据表中,需要添加昨日的持仓量
测试数据源:1分钟线的带夜盘的期货合约行情数据
setsysparam(pn_cycle(),cy_1m());
tmpt:= select FormaTDateTime("yyyy-mm-dd hh:nn:ss",["date"]) as "DateTime",
//若有夜盘,则应该得先得到对应的交易日,因此添加此列
getdate(["date"]) as "tradeDay",
["StockID"],["StockName"],['price'],['vol'],
["sectional_cjbs"]
from markettable
datekey 20220101T to 20220110.16T of 'RB2202' end;
return tmpt;
其中,getdate函数的实现可参考:FAQ:
Q:有夜盘数据时,如何确定当前时点的数据属于哪个交易日?
数据为:
封装函数获取指定日指定证券的昨成交笔数(昨持仓量),具体如下:
Function getSYScjbs(StockID,Endt);
begin
with *,array(pn_cycle():cy_day()) do
return static spec(specdate(ref(SectionOpenInterest(),1),Endt),StockID) name '_CJBS'$StockID$Endt;
end
注:其中利用static进行静态缓存,使得程序中每个证券每日只计算一次。
在高频行情中调用该模型进行提取如下:
setsysparam(pn_cycle(),cy_1m());
tmpt:= select FormaTDateTime("yyyy-mm-dd hh:nn:ss",["date"]) as "DateTime",
getdate(["date"]) as "tradeDay",
["StockID"],["StockName"],['price'],['vol'],
["sectional_cjbs"]
from markettable
datekey 20220101T to 20220110.16T of 'RB2202' end;
return select *,getSYScjbs(["StockID"],['tradeDay']) as 'pre_CJBS' from tmpt end;
返回结果如下(部分结果展示):