-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathstartup.dos
More file actions
225 lines (202 loc) · 10.8 KB
/
startup.dos
File metadata and controls
225 lines (202 loc) · 10.8 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
login(`admin, `123456)
version()
// 加载插件
try{ loadPlugin("plugins/amdquote/PluginAmdQuote.txt") } catch(ex) { print(ex) }
go
// 连接行情服务器
option = dict(STRING, ANY)
option[`ReceivedTime] = true
option[`DailyIndex] = true
// option[`OutputElapsed] = true
handle = amdQuote::connect(
"myUsername",
"myPassword",
["110.110.10.10"],
[7890],
option
)
// use ops
// unsubscribeAll()
// dropStreamTable("snapshot")
// dropStreamTable("order")
// dropStreamTable("execution")
// dropStreamTable("orderExecution1")
// dropStreamTable("orderExecution2")
// 获取行情数据的表结构并建流表
snapshotTbName = "snapshot"
orderTbName = "order"
executionTbName = "execution"
snapshotSchema = amdQuote::getSchema(`snapshot)
colName = append!(["tradeDate"], snapshotSchema.name)
colType = append!(["DATE"], snapshotSchema.type)
enableTableShareAndPersistence(table=streamTable(2000000:0, colName, colType), tableName=snapshotTbName, cacheSize=200000)
orderSchema = amdQuote::getSchema(`order)
colName = append!(["tradeDate"], orderSchema.name)
colType = append!(["DATE"], orderSchema.type)
enableTableShareAndPersistence(table=streamTable(2000000:0, colName, colType), tableName=orderTbName, cacheSize=200000)
executionSchema = amdQuote::getSchema(`execution)
colName = append!(["tradeDate"], executionSchema.name)
colType = append!(["DATE"], executionSchema.type)
enableTableShareAndPersistence(table=streamTable(2000000:0, colName, colType), tableName=executionTbName, cacheSize=200000)
orderExecutionSchema = amdQuote::getSchema(`orderExecution)
enableTableShareAndPersistence(table=streamTable(2000000:0, orderExecutionSchema.name, orderExecutionSchema.type), tableName=`orderExecution1, cacheSize=200000)
enableTableShareAndPersistence(table=streamTable(2000000:0, orderExecutionSchema.name, orderExecutionSchema.type), tableName=`orderExecution2, cacheSize=200000)
go
// 根据获取的行情数据的表结构建分布式库表
dbName = "dfs://amd"
if (!existsDatabase(dbName)) {
dbDate = database("", VALUE, 2023.01.01..2023.12.31)
dbCode = database("", HASH, [SYMBOL, 10])
db = database(dbName, COMPO, [dbDate, dbCode], engine="TSDB")
} else {
db = database(dbName)
}
if (!existsTable(dbName, snapshotTbName)) {
colName = append!(["tradeDate"], snapshotSchema.name)
colType = append!(["DATE"], snapshotSchema.type)
tbSchema = table(1:0, colName, colType)
pt = db.createPartitionedTable(table=tbSchema, tableName=snapshotTbName, partitionColumns=`tradeDate`securityCode, sortColumns=`securityCode`origTime, keepDuplicates=ALL)
}
if (!existsTable(dbName, orderTbName)) {
colName = append!(["tradeDate"], orderSchema.name)
colType = append!(["DATE"], orderSchema.type)
tbSchema = table(1:0, colName, colType)
pt = db.createPartitionedTable(table=tbSchema, tableName=orderTbName, partitionColumns=`tradeDate`securityCode, sortColumns=`securityCode`orderTime, keepDuplicates=ALL)
}
if (!existsTable(dbName, executionTbName)) {
colName = append!(["tradeDate"], executionSchema.name)
colType = append!(["DATE"], executionSchema.type)
tbSchema = table(1:0, colName, colType)
pt = db.createPartitionedTable(table=tbSchema, tableName=executionTbName, partitionColumns=`tradeDate`securityCode, sortColumns=`securityCode`execTime, keepDuplicates=ALL)
}
go
// 订阅处理方法
def handleSnapshotSubs(mutable msg, reorderedColNames) {
// 增加一个日期字段 tradeDate,其值为对应的时间戳字段的日期部分
update msg set tradeDate = date(origTime)
// 股票代码字段增加后缀,上海市场的后缀为".SH",深圳市场的后缀为".SZ"
update msg set securityCode = securityCode + ".SZ" where marketType=102
update msg set securityCode = securityCode + ".SH" where marketType=101
// 所有价格字段值除以 100
update msg set lastPrice = lastPrice / 100
update msg set openPrice = openPrice / 100
update msg set highPrice = highPrice / 100
update msg set lowPrice = lowPrice / 100
update msg set preClosePrice = preClosePrice / 100
update msg set offerPrice1 = offerPrice1 / 100
update msg set offerPrice2 = offerPrice2 / 100
update msg set offerPrice3 = offerPrice3 / 100
update msg set offerPrice4 = offerPrice4 / 100
update msg set offerPrice5 = offerPrice5 / 100
update msg set offerPrice6 = offerPrice6 / 100
update msg set offerPrice7 = offerPrice7 / 100
update msg set offerPrice8 = offerPrice8 / 100
update msg set offerPrice9 = offerPrice9 / 100
update msg set offerPrice10 = offerPrice10 / 100
update msg set bidPrice1 = bidPrice1 / 100
update msg set bidPrice2 = bidPrice2 / 100
update msg set bidPrice3 = bidPrice3 / 100
update msg set bidPrice4 = bidPrice4 / 100
update msg set bidPrice5 = bidPrice5 / 100
update msg set bidPrice6 = bidPrice6 / 100
update msg set bidPrice7 = bidPrice7 / 100
update msg set bidPrice8 = bidPrice8 / 100
update msg set bidPrice9 = bidPrice9 / 100
update msg set bidPrice10 = bidPrice10 / 100
update msg set weightedAvgOfferPrice = weightedAvgOfferPrice / 100
update msg set weightedAvgBidPrice = weightedAvgBidPrice / 100
update msg set highLimited = highLimited / 100
update msg set lowLimited = lowLimited / 100
// 调整列顺序为与流表、分布式表一致
reorderColumns!(msg, reorderedColNames)
return msg
}
def handleOrderSubs(mutable msg, reorderedColNames) {
// 增加一个日期字段 tradeDate,其值为对应的时间戳字段的日期部分
update msg set tradeDate = date(orderTime)
// 股票代码字段增加后缀,上海市场的后缀为".SH",深圳市场的后缀为".SZ"
update msg set securityCode = securityCode + ".SZ" where marketType = 102
update msg set securityCode = securityCode + ".SH" where marketType = 101
// 所有价格字段值除以 100
update msg set orderPrice = orderPrice / 100
// 调整列顺序为与流表、分布式表一致
reorderColumns!(msg, reorderedColNames)
return msg
}
def handleExecutionSubs(mutable msg, reorderedColNames) {
// 增加一个日期字段 tradeDate,其值为对应的时间戳字段的日期部分
update msg set tradeDate = date(execTime)
update msg set securityCode = securityCode + ".SZ" where marketType = 102
update msg set securityCode = securityCode + ".SH" where marketType = 101
// 所有价格字段值除以 100
update msg set execPrice = execPrice / 100
// 调整列顺序为与流表、分布式表一致
reorderColumns!(msg, reorderedColNames)
return msg
}
go
// 订阅入库
pt = loadTable(dbName, snapshotTbName)
subscribeTable(tableName=snapshotTbName, actionName="saveSnapshotToDFS", offset=-2, handler=pt, msgAsTable=true, batchSize=200000, throttle=60)
pt = loadTable(dbName, orderTbName)
subscribeTable(tableName=orderTbName, actionName="saveOrderToDFS", offset=-2, handler=pt, msgAsTable=true, batchSize=200000, throttle=60)
pt = loadTable(dbName, executionTbName)
subscribeTable(tableName=executionTbName, actionName="saveExecutionToDFS", offset=-2, handler=pt, msgAsTable=true, batchSize=200000, throttle=60)
go
/** 查看订阅信息
getStreamingStat().pubTables;
getStreamingStat().pubConns;
getStreamingStat().subWorkers;
getStreamingStat().subConns;
*/
// AMD 订阅
reorderedColNames = loadTable(dbName, snapshotTbName).schema().colDefs.name
amdQuote::subscribe(handle, `snapshot, snapshot, 101, , handleSnapshotSubs{reorderedColNames=reorderedColNames})
amdQuote::subscribe(handle, `snapshot, snapshot, 102, , handleSnapshotSubs{reorderedColNames=reorderedColNames})
reorderedColNames = loadTable(dbName, orderTbName).schema().colDefs.name
amdQuote::subscribe(handle, `order, order, 101, , handleOrderSubs{reorderedColNames=reorderedColNames})
amdQuote::subscribe(handle, `order, order, 102, , handleOrderSubs{reorderedColNames=reorderedColNames})
reorderedColNames = loadTable(dbName, executionTbName).schema().colDefs.name
amdQuote::subscribe(handle, `execution, execution, 101, , handleExecutionSubs{reorderedColNames=reorderedColNames})
amdQuote::subscribe(handle, `execution, execution, 102, , handleExecutionSubs{reorderedColNames=reorderedColNames})
d = dict(INT, ANY)
d[1] = orderExecution1
d[2] = orderExecution2
amdQuote::subscribe(handle, `orderExecution, d, 101)
go
// 可转债和 ETF 基金订阅
def subscribeConvertibleBond(handle, reorderedColNames) {
// 通过代码规律过滤得到可转债代码表
codeList = amdQuote::getCodeList()
convertibleBondCodeSh = exec securityCode from codeList where marketType = 101 and (securityCode like "110%" or securityCode like "111%" or securityCode like "113%" or securityCode like "118%" or securityCode like "1320%")
convertibleBondCodeSz = exec securityCode from codeList where marketType = 102 and (securityCode like "123%" or securityCode like "127%" or securityCode like "128%")
amdQuote::subscribe(handle, `bondSnapshot, objByName("snapshot"), 101, convertibleBondCodeSh, handleSnapshotSubs{reorderedColNames=reorderedColNames})
amdQuote::subscribe(handle, `bondSnapshot, objByName("snapshot"), 102, convertibleBondCodeSz, handleSnapshotSubs{reorderedColNames=reorderedColNames})
}
def subscribeEtfFund(handle, reorderedColNames) {
// 获取ETF代码表
codeList = exec securityCode from amdQuote::getETFCodeList()
amdQuote::subscribe(handle, `fundSnapshot, objByName("snapshot"), 101, codeList, handleSnapshotSubs{reorderedColNames=reorderedColNames})
amdQuote::subscribe(handle, `fundSnapshot, objByName("snapshot"), 102, codeList, handleSnapshotSubs{reorderedColNames=reorderedColNames})
}
reorderedColNames = loadTable(dbName, snapshotTbName).schema().colDefs.name
try {
subscribeConvertibleBond(handle, reorderedColNames)
subscribeEtfFund(handle, reorderedColNames)
} catch(ex) {
print(ex)
writeLog(ex)
}
go
// 查看 AMD 订阅信息
amdQuote::getStatus(handle)
// 取消所有 AMD 订阅
// amdQuote::unsubscribe(handle, `all)
// 关闭 AMD 连接
// amdQuote::close(handle)
// 时延检查,需要配置option[`OutputElapsed] = true
// select avg(nanosecond(perPenetrationTime)), max(nanosecond(perPenetrationTime)), min(nanosecond(perPenetrationTime)), median(nanosecond(perPenetrationTime)) from snapshot
// select avg(nanosecond(perPenetrationTime)), max(nanosecond(perPenetrationTime)), min(nanosecond(perPenetrationTime)), median(nanosecond(perPenetrationTime)) from execution
// select avg(nanosecond(perPenetrationTime)), max(nanosecond(perPenetrationTime)), min(nanosecond(perPenetrationTime)), median(nanosecond(perPenetrationTime)) from order
// select avg(nanosecond(perPenetrationTime)), max(nanosecond(perPenetrationTime)), min(nanosecond(perPenetrationTime)), median(nanosecond(perPenetrationTime)) from orderExecution1
// select avg(nanosecond(perPenetrationTime)), max(nanosecond(perPenetrationTime)), min(nanosecond(perPenetrationTime)), median(nanosecond(perPenetrationTime)) from orderExecution2