forked from hacktrade/hacktrade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hacktrade.lua
442 lines (418 loc) · 11.1 KB
/
hacktrade.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
--[[
[ HackTrade version 1.4
[ Nano-framework for HFT-robots development.
[ -----------------------------------------------------------
[ © 2014 Denis Kolodin
[
[]]--
--[[
[ Releases:
[ 1.3.1 - Bids in quotes 2 reverses. Fix bugs.
[
[
[
[
[]]--
--[[
[ What to do?!
[ - save and restoring state (is it needed?)
[ - stop working during clearing time (global variable, use OnConnected)
[
[]]--
--[[ SERVICE FUNCTIONS ]]--
function string.starts(source, starts)
return string.sub(source, 1, string.len(starts)) == starts
end
function string.ends(source, ends)
return End=='' or string.sub(source,-string.len(ends)) == ends
end
function table.reverse(tab)
local size = #tab
local ntab = {}
for i, v in ipairs(tab) do
ntab[size-i+1] = v
end
return ntab
end
function table.transform(tab, felem)
local ntab = {}
for idx = 1, #tab do
ntab[idx] = felem(tab[idx])
end
return ntab
end
Trade = coroutine.yield
-- OOP support
__object_behaviour = {
__call = function(meta, o)
if meta.__index == nil then
setmetatable(o, {__index = meta})
else
setmetatable(o, meta)
end
if meta.init ~= nil then
meta.init(o)
end
return o
end
}
function round(num, idp)
local mult = 10 ^ (idp or 0)
return math.floor(num * mult + 0.5) / mult
end
--[[ MARKET DATA SOURCE ]]--
MarketData = {}
function MarketData._pvconverter(elem)
local nelem = {}
nelem.price = tonumber(elem.price)
nelem.volume = tonumber(elem.volume)
return nelem
end
function MarketData:init()
log:trace("MarketData created: " .. self.market .. " " .. self.ticker)
end
function MarketData:__index(key)
if MarketData[key] ~= nil then
return MarketData[key]
end
--[[ DEPRECATED: Lazy value support. Don't need it.
if string.starts(key, "lazy") then
lazy, key = string.match(key, "([^_]+)_([^_]+)")
return function() return self[key] end
end
]]--
if key == "bids" then
local data = getQuoteLevel2(self.market, self.ticker).bid
data = table.reverse(data) -- Reverse for normal order (not alphabet)!
data = table.transform(data, self._pvconverter)
return data or {}
elseif key == "offers" then
local data = getQuoteLevel2(self.market, self.ticker).offer
data = table.transform(data, self._pvconverter)
return data or {}
end
local param = getParamEx(self.market, self.ticker, key)
if tonumber(param.param_type) < 3 then
return tonumber(param.param_value)
else
return param.param_value
end
end
function MarketData:fit(price)
local step = feed.sec_price_step
local result = math.floor(price / step) * step
return round(result, self.sec_scale)
end
function MarketData:move(price, val)
local step = feed.sec_price_step
local result = (math.floor(price / step) * step) + (val * step)
return round(result, self.sec_scale)
end
setmetatable(MarketData, __object_behaviour)
--[[ HISTORY DATA SOURCE ]]--
History = {}
function History:__index(key)
if History[key] ~= nil then
return History[key]
end
if key < 0 then
key = #self + key + 1
end
return self[key]
end
setmetatable(History, __object_behaviour)
-- You can access by closes_0, values, values_1
Indicator = {}
function Indicator:init()
log:trace("Indicator created with tag: " .. self.tag)
end
function Indicator:__index(key)
local extractor = nil
if type(key) == "number" then
extractor = key
key = "closes_0"
end
local line = key:match("%d+")
local field = key:match("%a+")
if line == nil then
line = 0
end
local candles = getNumCandles(self.tag)
local data, n, b = getCandlesByIndex(self.tag, tonumber(line), 0, candles)
if n == 0 then
log:fatal("Can't load data for chart with tag: "..self.tag)
end
if field ~= nil then
field = field:sub(0, -2)
if field == "value" then
field = "close"
end
for idx = 1, #data do
data[idx] = data[idx][field]
end
end
local result = History(data)
if extractor ~= nil then
return result[extractor]
else
return result
end
end
setmetatable(Indicator, __object_behaviour)
--[[ EXECUTION SYSTEM ]]--
SmartOrder = {
-- 666 - Warning! This number uses for cancelling!
lower = 1000,
upper = 10000,
pool = {}
}
function SmartOrder:__index(key)
if SmartOrder[key] ~= nil then
return SmartOrder[key]
end
--[[
for k, v in pairs(self) do
if key == k then
return v
end
end
]]--
-- Dynamic fields have to be calculated!
if key == "remainder" then
return (self.planned - self.position)
end
if key == "filled" then
return (self.planned - self.position) == 0
end
return nil
end
function SmartOrder:init()
math.randomseed(os.time())
for i = self.lower, self.upper do
local key = math.random(self.lower, self.upper)
-- Store unique number of transaction which can be used as pool for processing
if SmartOrder.pool[key] == nil then
SmartOrder.pool[key] = self
self.trans_id = key
break
end
end
self.position = 0
self.planned = 0
self.order = nil
log:trace("SmartOrder created with trans_id: " .. self.trans_id)
end
function SmartOrder:destroy()
SmartOrder.pool[self.trans_id] = nil
end
function SmartOrder:update(price, planned)
if price ~= nil then
self.price = price
end
if planned ~= nil then
self.planned = planned
end
end
function SmartOrder:process()
log:debug("----------------------------------------------------------------------------------------------------")
log:debug("Processing SmartOrder >> " .. self.trans_id)
local order = self.order
if order ~= nil then
log:debug("Parameters >> ".."price="..tostring(self.price).." planned="..self.planned.." position="..self.position.." order_qty="..order.quantity)
local cancel = false
if order.price ~= self.price then
cancel = true
log:debug("NEW PRICE="..self.price)
end
local filled = order.filled * order.sign
--log:debug("Difference >> planned=" .. self.planned .. " position=" .. self.position .. " order_qty=" .. order.quantity)
if self.planned - self.position - order.quantity ~= 0 then
cancel = true
end
if order.active == false then
-- Считаем после установки флага!!!
filled = order.filled * order.sign
self.position = self.position + filled
self.order = nil
else
if cancel then
if self.order.number ~= nil then
if self.order.cancelled ~= nil then
if (os.time() - self.order.cancelled) > 5 then
self.order.cancelled = nil
end
else
local result = sendTransaction({
ACCOUNT=self.account,
CLIENT_CODE=self.client,
CLASSCODE=self.market,
SECCODE=self.ticker,
TRANS_ID="666",
ACTION="KILL_ORDER",
ORDER_KEY=tostring(self.order.number),
})
if result == "" then
self.order.cancelled = os.time()
end
log:debug("Kill order!")
end
end
end
end
else
log:debug("Parameters >> ".."price="..tostring(self.price).." planned="..self.planned.." position="..self.position.." order_qty=nil")
local diff = self.planned - self.position
if diff ~= 0 then
if self.order == nil then
local absdiff = math.abs(diff)
local result = sendTransaction({
ACCOUNT=self.account,
CLIENT_CODE=self.client,
CLASSCODE=self.market,
SECCODE=self.ticker,
TYPE="L",
TRANS_ID=tostring(self.trans_id),
ACTION="NEW_ORDER",
OPERATION=(diff > 0 and "B") or "S",
PRICE=tostring(self.price),
QUANTITY=tostring(absdiff)
})
if result == "" then
self.order = {
sign = diff / absdiff,
price = self.price,
quantity = diff,
active = true,
filled = 0,
}
log:debug("NEW_ORDER >> "..self.market.." "..self.ticker.." price="..tostring(self.price).." qty="..tostring(absdiff))
else
log:debug("New order ERROR: "..result)
end
end
end
end
end
setmetatable(SmartOrder, __object_behaviour)
--[[ LOGGING ]]--
log = {
logfile = nil,
loglevel = -1,
loglevels = {
[-1] = 'Debug',
[ 0] = 'Trace',
[ 1] = 'Info',
[ 2] = 'Warning',
[ 3] = 'Error',
}
}
function log:log(log_text, log_level)
if (log_level >= self.loglevel) then
local msg = string.format("[%s] %s: %s\n", os.date(), self.loglevels[log_level], log_text)
if (log_level > 0) then
message(msg, log_level)
end
self.logfile:write(msg)
self.logfile:flush()
end
end
function log:debug(t)
self:log(t, -1)
end
function log:trace(t)
self:log(t, 0)
end
function log:info(t)
self:log(t, 1)
end
function log:warning(t)
self:log(t, 2)
end
function log:error(t)
self:log(t, 3)
end
function log:fatal(t)
self:error(t)
error(t)
end
--[[ MAIN LOOP ]]--
working = true
function main()
--create_table()
log:trace("Robot started")
if Start ~= nil then
Start()
end
if Robot ~= nil then
local routine = coroutine.create(Robot)
while working do
local res, errmsg = coroutine.resume(routine)
if res == false then
log:fatal("Broken coroutine: " .. errmsg)
end
if coroutine.status(routine) == "dead" then
log:trace("Robot routine finished")
break
end
-- Orders processing calls after every coroutine iteration
for trans_id, smartorder in pairs(SmartOrder.pool) do
smartorder:process()
end
end
end
log:trace("Robot stopped")
if Stop ~= nil then
Stop()
end
io.close(log.logfile)
end
--[[ TRANSACTION CALLBACK ]]--
function OnTransReply(trans_reply)
local key = trans_reply.trans_id
local executor = SmartOrder.pool[key]
if executor ~= nil then
if trans_reply.status == 3 then
executor.order.number = trans_reply.ordernum
else
executor.order = nil
end
end
end
--[[ ORDERS CALLBACK ]]--
function OnOrder(order)
local key = order.trans_id
local executor = SmartOrder.pool[key]
-- There isn't order if was executed imidiately!
if executor ~= nil and executor.order ~= nil then
executor.order.filled = order.qty - order.balance
if (order.flags % 2) == 0 then
executor.order.active = false
end
end
end
withgui = false
--[[ INIT CALLBACK ]]--
function OnInit(path)
-- Only there it's possible to take path
log.logfile = io.open(path..'.log', 'w')
-- Table creation
if withgui == true then
local table_id = AllocTable()
if CreateWindow(table_id) == 1 then
log:trace("SmartOrders table created, id=" .. table_id)
SetWindowCaption(table_id, "SmartOrders [" .. path .. "]")
AddColumn(table_id, "trans_id", nil, QTABLE_INT_TYPE, 10)
AddColumn(table_id, "status", nil, QTABLE_STRING_TYPE, 10)
else
log:fatal("SmartOrders table not created!" .. table_id)
end
SmartOrder.table = table_id
end
end
--[[ END CALLBACK ]]--
function OnStop(stop_flag)
working = false
if withgui == true then
DestroyTable(SmartOrder.table)
end
end