-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.lua
276 lines (245 loc) · 7.87 KB
/
routes.lua
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
local json = require("libs/dkjson")
local routes = {}
function routes.get_security_info(secCode, classCode)
return getSecurityInfo(classCode, secCode)
end
function routes.get_class_info(classCode)
return getClassInfo(classCode)
end
function routes.get_class_securities(classCode)
local res = split_in_table(getClassSecurities(classCode), ",")
if #res >= 1 then
res[#res] = nil
end
return res
end
function routes.get_info_param(paramName)
return getInfoParam(paramName)
end
function routes.get_futures_holding(firmId, trdAccId, secCode, type)
return getFuturesHolding(firmId, trdAccId, secCode, tonumber(type))
end
function routes.get_futures_client_limits()
local res = {}
for i = 0, getNumberOf("futures_client_limits") - 1 do
table.insert(res, getItem("futures_client_limits", i))
end
return res
end
function routes.get_depo_limit(firmId, clientCode, secCode, trdAccId, limitKind)
return getDepoEx(firmId, clientCode, secCode, trdAccId, tonumber(limitKind))
end
function routes.get_money_limits()
local res = {}
for i = 0, getNumberOf("money_limits") - 1 do
table.insert(res, getItem("money_limits", i))
end
return res
end
function routes.get_trade_accounts()
local res = {}
for i = 0, getNumberOf("trade_accounts") - 1 do
local account = getItem("trade_accounts", i)
if account.class_codes ~= "" then
table.insert(res, account)
end
end
return res
end
function routes.get_candlesticks(secCode, classCode, interval, limit, skip)
--log_i("Candles loading started " .. secCode .. " class " .. classCode .. " interval " .. interval)
local dataSource, err = CreateDataSource(classCode, secCode, tonumber(interval))
local limit = tonumber(limit)
if limit == 0 then
limit = 10000
end
local skip = tonumber(skip)
if dataSource then
local res = {}
local sleepTime = 0
while dataSource:Size() == 0 and sleepTime < 5000 do
sleep(10)
sleepTime = sleepTime + 10
end
--log_i("Candles loading ended " .. sleepTime)
local size = dataSource:Size()
if size == 0 then
return nil
end
local start = math.max(1, size - limit - skip + 1)
local to = math.min(size, start - 1 + limit)
for i = start, to do
local candle = {}
candle.O = dataSource:O(i)
candle.H = dataSource:H(i)
candle.L = dataSource:L(i)
candle.C = dataSource:C(i)
candle.V = dataSource:V(i)
candle.T = dataSource:T(i)
table.insert(res, candle)
end
--log_e("Candles start " .. start .. " to " .. to .. " skip " .. skip .. " limit " .. limit .. " size " .. size)
dataSource:Close()
return res
else
log_e("Candlesticks were not loaded: " .. err)
return nil, err
end
end
function routes.get_quote_level_2(secCode, classCode)
return getQuoteLevel2(classCode, secCode)
end
function routes.subscribe_level_2_quotes(secCode, classCode)
return Subscribe_Level_II_Quotes(classCode, secCode)
end
function routes.unsubscribe_level_2_quotes(secCode, classCode)
return Unsubscribe_Level_II_Quotes(classCode, secCode)
end
function routes.is_subscribed_level_2_quotes(secCode, classCode)
return IsSubscribed_Level_II_Quotes(classCode, secCode)
end
--
-- ORDERS
--
function routes.get_orders(secCode, classCode)
local res = {}
for i = 0, getNumberOf("orders") - 1 do
local order = getItem("orders", i)
if order.sec_code == secCode and order.class_code == classCode then
table.insert(res, routes.fill_order_with_trades_data(order))
end
end
return res
end
function routes.get_order_by_id(secCode, classCode, transId)
local transId = tonumber(transId)
for i = getNumberOf("orders") - 1, 0, -1 do
local order = getItem("orders", i)
if order.sec_code == secCode and order.class_code == classCode and order.trans_id == transId then
return routes.fill_order_with_trades_data(order)
end
end
return nil
end
function routes.get_order_by_num(orderNumber)
local orderNum = tonumber(orderNumber)
for i = getNumberOf("orders") - 1, 0, -1 do
local order = getItem("orders", i)
if order.order_num == orderNum then
return routes.fill_order_with_trades_data(order)
end
end
return nil
end
function routes.get_stop_orders(secCode, classCode)
local res = {}
for i = 0, getNumberOf("stop_orders") - 1 do
local order = getItem("stop_orders", i)
if order.sec_code == secCode and order.class_code == classCode then
table.insert(res, order)
end
end
return res
end
function routes.get_stop_order_by_id(secCode, classCode, transId)
local transId = tonumber(transId)
for i = getNumberOf("stop_orders") - 1, 0, -1 do
local order = getItem("stop_orders", i)
if order.sec_code == secCode and order.class_code == classCode and order.trans_id == transId then
return routes.fill_order_with_trades_data(order)
end
end
return nil
end
function routes.get_stop_order_by_num(orderNumber)
local orderNum = tonumber(orderNumber)
for i = getNumberOf("stop_orders") - 1, 0, -1 do
local order = getItem("stop_orders", i)
if order.order_num == orderNum then
return routes.fill_order_with_trades_data(order)
end
end
return nil
end
function routes.get_trades_by_num(orderNumber)
local orderNum = tonumber(orderNumber)
local res = {}
for i = 0, getNumberOf("trades") - 1 do
local trade = getItem("trades", i)
if trade.order_num == orderNum then
table.insert(res, trade)
end
end
return res
end
function routes.fill_order_with_trades_data(order)
if order.qty == order.balance then
return order
end
local qty_of_trades = 0
local price_qty = 0
for i = 0, getNumberOf("trades") - 1 do
local trade = getItem("trades", i)
if trade.order_num == order.order_num then
price_qty = price_qty + trade.price * trade.qty
qty_of_trades = qty_of_trades + trade.qty
order.last_trade_datetime_ex = trade.datetime
end
if qty_of_trades == order.qty then
break
end
end
if qty_of_trades > 0 then
order.average_price_ex = price_qty / qty_of_trades
end
return order
end
function routes.send_transaction(transaction)
local err = sendTransaction(transaction)
if err ~= "" then
return nil, err
else
return true
end
end
--
-- BULK REQUESTS
--
function routes.get_security_info_bulk(secsClasses)
local res = {}
for i = 1, #secsClasses do
local secCode, classCode = split(secsClasses[i])
local security = getSecurityInfo(classCode, secCode)
if security then
table.insert(res, security)
else
table.insert(res, json.null)
end
end
return res
end
function routes.get_param_ex2_bulk(secsClassesParams)
local res = {}
for i = 1, #secsClassesParams do
local secCode, classCode, param = split(secsClassesParams[i])
table.insert(res, getParamEx2(classCode, secCode, param))
end
return res
end
function routes.param_request_bulk(secsClassesParams)
local res = {}
for i = 1, #secsClassesParams do
local secCode, classCode, param = split(secsClassesParams[i])
table.insert(res, ParamRequest(classCode, secCode, param))
end
return res
end
function routes.cancel_param_request_bulk(secsClassesParams)
local res = {}
for i = 1, #secsClassesParams do
local secCode, classCode, param = split(secsClassesParams[i])
table.insert(res, CancelParamRequest(classCode, secCode, param))
end
return res
end
return routes