-
Notifications
You must be signed in to change notification settings - Fork 77
/
shapshot数据处理.dos
83 lines (69 loc) · 3.25 KB
/
shapshot数据处理.dos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
*移动平均时间加权订单斜率
*/
dbName="dfs://level_2"
snapshotTBname="snapshot"
idate=2022.04.14
//snapshotTB=select* from loadTable(dbName,snapshotTBname) where date(DateTime)=idate
@state
def timeWeightedOrderSlope(bid,bidQty,ask,askQty,lag=20){
return (log(iif(ask==0,bid,ask))-log(iif(bid==0,ask,bid)))\(log(askQty)-log(bidQty)).ffill().mavg(lag, 1).nullFill(0)
}
/*
* 逐档订单失衡率因子
* 订单薄上的委托量反应了交易者们对股价未来的预期,当交易者预期股票价格上升,他们将通过下买单持有更多的股票多头头寸,
* 这将导致买盘的委托量增加,反之亦然。因此,买卖盘的委托量的不平衡可以反应市场的总体情绪和方向
*/
@state
def wavgSOIR(bidQty,askQty,lag=20){
imbalance= rowWavg((bidQty - askQty)\(bidQty + askQty), 10 9 8 7 6 5 4 3 2 1).ffill().nullFill(0)
mean = mavg(prev(imbalance), (lag-1), 2)
std = mstdp(prev(imbalance) * 1000000, (lag-1), 2) \ 1000000
return iif(std >= 0.0000001,(imbalance - mean) \ std, NULL).ffill().nullFill(0)
}
/*
* 成交价加权净委买比例
*/
@state
def traPriceWeightedNetBuyQuoteVolumeRatio(bid,bidQty,ask,askQty,TotalValTrd,TotalVolTrd,lag=20){
prevbid = prev(bid)
prevbidQty = prev(bidQty)
prevask = prev(ask)
prevaskQty = prev(askQty)
bidchg = iif(round(bid-prevbid,2)>0, bidQty, iif(round(bid-prevbid,2)<0, -prevbidQty, bidQty-prevbidQty))
offerchg = iif(iif(ask==0,iif(prevask>0,1,0), ask-prevask)>0, prevaskQty, iif(iif(prevask==0,
iif(ask>0,-1,0), iif(ask>0,ask-prevask,1))<0, askQty, askQty-prevaskQty))
avgprice = deltas(TotalValTrd)\deltas(TotalVolTrd)
factorValue = (bidchg-offerchg)\(abs(bidchg)+abs(offerchg))*avgprice
return nullFill(msum(factorValue,lag,1)\msum(avgprice,lag,1), 0)
}
/*
* 10档委买、卖增额
*/
@state
def level10_Diff(price, qty, buy, lag=20){
prevPrice = price.prev()
left, right = rowAlign(price, prevPrice, how=iif(buy, "bid", "ask"))
qtyDiff = (qty.rowAt(left).nullFill(0) - qty.prev().rowAt(right).nullFill(0))
amtDiff = rowSum(nullFill(price.rowAt(left), prevPrice.rowAt(right)) * qtyDiff)
return msum(amtDiff, lag, 1).nullFill(0)
}
@state
def level10_InferPriceTrend(bid, ask, bidQty, askQty, lag1=60, lag2=20){
inferPrice = (rowSum(bid*bidQty)+rowSum(ask*askQty))\(rowSum(bidQty)+rowSum(askQty))
price = iif(bid[0] <=0 or ask[0]<=0, NULL, inferPrice)
return price.ffill().linearTimeTrend(lag1).at(1).nullFill(0).mavg(lag2, 1).nullFill(0)
}
////Time elapsed: 9101.371 ms,32
////Time elapsed: 13913.538 ms ,16
////Time elapsed: 21270.213 ms ,8
////Time elapsed: 35511.821 ms ,4
timer {
res=select SecurityID,DateTime,timeWeightedOrderSlope(bidPrice[0],bidOrderQty[0],OfferPrice[0],OfferOrderQty[0]) as TimeWeightedOrderSlope,
level10_InferPriceTrend(bidPrice,OfferPrice,bidOrderQty,OfferOrderQty,60,20) as Level10_InferPriceTrend,
level10_Diff(bidPrice, bidOrderQty, true,20) as Level10_Diff,
traPriceWeightedNetBuyQuoteVolumeRatio(bidPrice[0],bidOrderQty[0],OfferPrice[0],OfferOrderQty[0],TotalValueTrade,
totalVolumeTrade) as TraPriceWeightedNetBuyQuoteVolumeRatio,
wavgSOIR( bidOrderQty,OfferOrderQty,20) as HeightImbalance
from loadTable(dbName,snapshotTBname) where date(DateTime)=idate context by SecurityID csort DateTime map
}