3.2 逐笔成交数据的因子计算
逐笔成交数据包含的信息很丰富,可以构建很多中高频因子。以下为逐笔成交的样例数据:


利用逐笔成交数据中的买卖订单号,可以将其合并为单笔订单成交数据,并从单笔订单的角度区分大小单和主买卖方向等。本节从单笔订单和主买卖角度出发,计算主动买入和卖出的单笔订单均价,并统计成交时间与开始委托下单时长超过1分钟的订单数量。

3.2.1 单笔订单主动买入卖出均价
单笔订单主动买入、卖出均价为单笔主买、主卖订单的成交均价价格之和除以订单数量。


n
 表示截至 t 时刻主买、主卖订单数量, avgTradePrice
 表示单笔订单主买、主卖的成交均价。

def singleOrderAveragePrice(buyNo,sellNo,tradePrice,tradeQty,BSFlag="B"){
	if(BSFlag=="B"){
		 totolMoney=groupby(sum,iif(buyNo>sellNo,tradePrice*tradeQty,0),buyNo).values()[1]
		 totolqty=groupby(sum,iif(buyNo>sellNo,tradeQty,0),buyNo).values()[1]
	}
	else{
		 totolMoney=groupby(sum,iif(buyNo<sellNo,tradePrice*tradeQty,0),sellNo).values()[1]
		 totolqty=groupby(sum,iif(buyNo<sellNo,tradeQty,0),sellNo).values()[1]
		}
	 return totolMoney\totolqty
}
res=select avg(singleOrderAveragePrice(BidApplSeqNum,OfferApplSeqNum,TradePrice,TradeQty,"B")) as ActBuySingleOrderAveragePriceFactor,
avg(singleOrderAveragePrice(BidApplSeqNum,OfferApplSeqNum,TradePrice,TradeQty,"S")) as ActSellSingleOrderAveragePriceFactor from 
tradeTB where  TradePrice>0 group by SecurityID cgroup by minute(DateTime) as minute order by  minute;
这里首先通过自定义函数实现单笔订单的成交均价,然后再计算单笔订单的成交均价。自定义函数中使用 groupby 函数分别计算单笔订单的成交金额和成交数量。

最后通过 group by + cgroup by 计算每只股票当日最新一分钟单笔订单主动买入、卖出均价。计算结果如下:


3.2.2 股票延时成交订单因子
延时成交挂单数和成交量,一定程度上能反应大单或者机构成交情况。本节统计从下单到成交的时间间隔超过1分钟的订单数和成交量。

@state
def delayedTradeNum(bsFlag, flag, side){
      return iif(bsFlag==side && flag<=1, flag, 0).cumsum()

}

@state
def delayedTradeQty(bsFlag, flag, tradeQty, cumTradeQty, side){
        return iif(bsFlag==side && flag>1, tradeQty, iif(bsFlag==side && flag==1, cumTradeQty, 0)).cumsum()
}
///买方
///step 1,标记延时状态,计算笔订单的累计成交量
t1 = select SecurityID,DateTime,entrust.DateTime as time,"B" as bsFlag,tradeQty, cumsum(iif(DateTime-entrust.DateTime>60000,1,0)) as delayedTraderflag, cumsum(tradeQty) as cumTradeQty from lsj(trade, entrust, ['SecurityID', 'BidApplSeqNum'], ['SecurityID', 'ApplSeqNum']) where tradePrice>0 context by  SecurityID,BidApplSeqNum 
/////step 2,统计每只股票累计的延时成交订单数,和延时成交订单量
tt2 = select SecurityID,DateTime,delayedTradeNum(bsFlag, delayedTraderflag, "B") as DelayedTraderBuyOrderNum ,
 delayedTradeQty(bsFlag,delayedTraderflag, tradeQty, cumTradeQty, "B") as DelayedTraderBuyOrderQty from t1 context by SecurityID limit -1
 ///卖方
///step 1,标记延时状态,计算笔订单的累计成交量
t1 = select SecurityID,DateTime,"S" as bsFlag,tradeQty, cumsum(iif(DateTime-entrust.DateTime>60000,1,0)) as delayedTraderflag, cumsum(tradeQty) as cumTradeQty from lsj(trade, entrust, ['SecurityID', 'OfferApplSeqNum'], ['SecurityID', 'ApplSeqNum']) where tradePrice>0 context by SecurityID, OfferApplSeqNum
/////step 2,统计每只股票累计的延时成交订单数,和延时成交订单量
t3 = select SecurityID,DateTime,delayedTradeNum(bsFlag, delayedTraderflag, "S") as DelayedTraderSellOrderNum ,
 delayedTradeQty(bsFlag,delayedTraderflag , tradeQty, cumTradeQty, "S") as DelayedTraderSellOrderQty from t1 context  by SecurityID limit -1
 t2=lsj(t2,t3,`SecurityID`DateTime)
下单信息记录在逐笔委托表里,如果统计下单到成交之间的时间间隔,则需要把逐笔成交表和逐笔委托表进行关联。这里首先通过左半连接 (lsj) 返回逐笔成交表中所有与逐笔委托表匹配的记录,如果逐笔委托表中有多条匹配记录(如上交所的下单和撤单记录),lsj 将会取第一条(下单时的订单记录)匹配记录。因此,lsj 可以把订单委托下单的时间以及下单量准确关联到成交记录中。DolphinDB 提供很多表关联函数,具体可参考:表连接 — DolphinDB 2.0 documentation

计算股票延时成交订单因子的步骤为,首先根据成交表的买卖单号与委托表的订单委托号建立连接,并计算改订单的累计延时成交次数和订单的累计成交量;其次通过自定义函数,计算股票的延时成交订单数以及延时成交的订单量。

3.3 逐笔委托数据的因子计算
Level 2 行情逐笔委托数据包含所有的委托订单信息(除了上交所的市价单(即时成交)和部分撤单,深交所的撤单),本节根据买卖订单的委托信息,计算深交所股票的委买委卖金额和撤单金额指标数据。

逐笔委托数据数据样例如下:


3.3.1 委买委卖金额
委托买卖金额是对一段时间内委托订单的买卖方向的资金总量进行统计。由于深交所的市价订单的价格在逐笔委托表里的价格不是一个成交价格(一般标记为0)。因此统计计算委托金额时,我们需要在逐笔成交记录中找到最近的成交价格来作为其近似值。

defg calcSZOrderValue(side,price,orderQty,tradePrice,orderType,bsFlag){
	price_=iif(orderType =="50",price,NULL).nullFill(tradePrice)
	return sum(iif(side==bsFlag,price_*orderQty,NULL)).nullFill(0)
}
res=select calcSZOrderValue(side,price,orderQty,tradePrice,orderType,"B") as BuyOrderValue, 
calcSZOrderValue(side,price,orderQty,tradePrice,orderType,"S") as SellOrderValue
from aj(entrustTB,tradeTB,`SecurityID`ApplSeqNum) group by SecurityID,bar(DateTime,1m) as DateTime
这里通过 aj(asof join)把逐笔成交里的最新价格关联到逐笔成交中;对市价订单,以获取最新的成交价格作为当前市价委托单的委托价格,最后计算股票每分钟内的买卖委托金额。

3.3.2 买卖撤单金额
深交所的撤单标记记录在逐笔成交表中,撤单时的价格一般标记为0,而委托价格记录在逐笔委托表中,因此计算买卖方撤单金额时需要将逐笔成交表和逐笔委托表进行关联。


trade = select SecurityID,DateTime, max(BidApplSeqNum,OfferApplSeqNum) as ApplSeqNum,TradeQty from tradeTB where  ExecType=52
entrust = select  SecurityID,ApplSeqNum, Price,Side from entrustTB
res = select  sum(iif(side=="B",Price*TradeQty,NULL)).nullFill(0) as buywithdrawOrderValue,
sum(iif(side=="S",Price*TradeQty,NULL)).nullFill(0) as sellwithdrawOrderValue 
from lsj(trade,entrust,`SecurityID`ApplSeqNum) group by SecurityID,bar(DateTime,1m) as DateTime
这里通过 lj(left join)分别把买卖撤单的委托价格关联到撤单信息表中,然后计算每只股票每分钟的买卖撤单金额。

参考:https://gitee.com/dolphindb/Tutorials_CN/blob/master/Level-2_stock_data_processing.md#332-%E4%B9%B0%E5%8D%96%E6%92%A4%E5%8D%95%E9%87%91%E9%A2%9D

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐