-
Notifications
You must be signed in to change notification settings - Fork 7
/
Trading.lua
420 lines (392 loc) · 14.5 KB
/
Trading.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
module("Trading", package.seeall)
local SysFunc=require("SysFunc")
function SendLimitOrder(ClassCode,SecCode,Operation,Price,Volume,Account,ClientCode,Comment)--[[
ñòàâèò â î÷åðåäü ëèìèòèðîâàííóþ çàÿâêó ïî çàäàííîé öåíå
]]
if (ClassCode==nil or SecCode==nil or Operation==nil or Price==nil or Volume==nil or Account==nil) then
return false,0,"qlib.SendLimitOrder(): Can`t send order. Nil parameters."
end
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="NEW_ORDER",
["CLASSCODE"]=ClassCode,
["SECCODE"]=SecCode,
["OPERATION"]=Operation,
["QUANTITY"]=tostring(Volume),
["PRICE"]=tostring(SysFunc.toPrice(SecCode,Price)),
["ACCOUNT"]=Account,
["EXECUTION_CONDITION"] = "PUT_IN_QUEUE"
}
if ClientCode==nil then
transaction.client_code=Account
else
transaction.client_code=ClientCode
end
if Comment~=nil then
transaction.client_code=ClientCode.."//"..Comment
end
local res=sendTransaction(transaction)
if res~="" then
message(res,1)
return transaction["TRANS_ID"], "qlib.SendLimitOrder():"..res,res
else
return transaction["TRANS_ID"], "qlib.SendLimitOrder(): Limit order sent sucesfully. Class="..ClassCode.." Sec="..SecCode.." Dir="..Operation.." Price="..Price.." Vol="..Volume.." Acc="..Account,res
end
end
function SendMarketOrder(ClassCode,SecCode,Operation,Volume,Account,ClientCode) --ðàáîòàåò ñòðàííî, ëó÷øå èñïîëüçîâàòü ïðåäûäóùóþ ôóíêöèþ
--[[SendMarketOrder(ClassCode,SecCode,"S",Volume,Account,ClientCode)
ïîñûëàåò ðûíî÷íóþ çàÿâêó
]]
if (ClassCode==nil or SecCode==nil or Operation==nil or Volume==nil or Account==nil) then
return nil,"QL.sendMarket(): Can`t send order. Nil parameters."
end
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="NEW_ORDER",
["CLASSCODE"]=ClassCode,
["SECCODE"]=SecCode,
["OPERATION"]=Operation,
["TYPE"]="M",
["QUANTITY"]=tostring(Volume),
["ACCOUNT"]=Account
}
if ClientCode==nil then
transaction.client_code=Account
else
transaction.client_code=ClientCode
end
if string.find("SPBFUT",ClassCode)~=nil then
if direction=="B" then
transaction.price=getParamEx(class,security,"PRICEMAX").param_image
else
transaction.price=getParamEx(class,security,"PRICEMIN").param_image
end
else
transaction.price="0"
end
if Comment~=nil then
transaction.comment=string_sub(tostring(Comment),0,20)
else
transaction.comment='fuck'
end
local res=sendTransaction(transaction)
if res~="" then
return nil, "qlib.sendMarket():"..res
else
return transaction["TRANS_ID"], "qlib.sendMarket(): Market order sended sucesfully. Class="..ClassCode.." Sec="..SecCode.." Dir="..Operation.." Vol="..Volume.." Acc="..Account
end
end
function SendTPSLOrder(ClassCode,SecCode,Operation,Price,TP,SL,MaxOffset,DefSpread,Volume,Account,ClientCode)
--[[
ïîñûëàåò ÒåéêÏðîôèò-ÑòîïËèìèò çàÿâêó
âñå ïàðàìåòðû äîëæíû ïðèñóòñòâîâàòü
âñå çíà÷åíèÿ îêðóãëÿþòñÿ âíèç, åñëè íå ñîîòâåòñòâóþò øàãó öåíû
]]
if (ClassCode==nil or SecCode==nil or Operation==nil or Price==nil or Volume==nil or Account==nil or TP==nil or SL==nil or MaxOffset==nil or DefSpread==nil) then
return false,0,"qlib.SendTPSLOrder(): Can`t send order. Nil parameters."
end
local StopLoss,TakeProfit=0,0
if (Operation=="B") then
StopLoss = SL
TakeProfit = TP
LimitPrice = SL + DefSpread
end
if (Operation=="S") then
StopLoss = SL
TakeProfit = TP
LimitPrice = SL - DefSpread
end
TakeProfit,StopLoss,LimitPrice = RoundToStep(ClassCode,SecCode,TakeProfit),RoundToStep(ClassCode,SecCode,StopLoss),RoundToStep(ClassCode,SecCode,LimitPrice)
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="NEW_STOP_ORDER",
["STOP_ORDER_KIND"]="TAKE_PROFIT_AND_STOP_LIMIT_ORDER",
["STOPPRICE"]=tostring(SysFunc.toPrice(SecCode,TakeProfit)),
["OFFSET"]=tostring(SysFunc.toPrice(SecCode,MaxOffset)),
["OFFSET_UNITS"]="PRICE_UNITS",
["SPREAD"]=tostring(SysFunc.toPrice(SecCode,DefSpread)),
["SPREAD_UNITS"]="PRICE_UNITS",
["MARKET_STOP_LIMIT"]="YES",
["MARKET_TAKE_PROFIT"]="YES",
["STOPPRICE2"]=tostring(SysFunc.toPrice(SecCode,StopLoss)),
["CLASSCODE"]=ClassCode,
["SECCODE"]=SecCode,
["OPERATION"]=Operation,
["QUANTITY"]=tostring(Volume),
["PRICE"]=tostring(SysFunc.toPrice(SecCode,LimitPrice)),
["ACCOUNT"]=Account,
["EXECUTION_CONDITION"] = "FILL_OR_KILL",
["EXPIRY_DATE"]="GTC",
}
if ClassCode=='QJSIM' then
transaction["EXPIRY_DATE"]=tostring(SysFunc.GetDate())
end
if ClientCode==nil then
transaction.client_code=Account
else
transaction.client_code=ClientCode
end
if Comment~=nil then
transaction.comment=string_sub(tostring(Comment),0,20)
else
transaction.comment='fuck'
end
local res=sendTransaction(transaction)
if res~="" then
message(res,1)
return transaction["TRANS_ID"], "qlib.SendTPSLOrder():"..res
else
return transaction["TRANS_ID"], "qlib.SendTPSLOrder(): TPSL order sended sucesfully. Class="..ClassCode.." Sec="..SecCode.." Dir="..Operation.." Price="..Price.." TakeProfit="..TakeProfit.." StopLoss="..StopLoss.." MaxOffset="..MaxOffset.." Trans_id="..transaction["TRANS_ID"]
end
end
function MoveOrder(mode,firstnumber,firstprice,firstquantity)
local order=SysFunc.GetRowFromTable("orders",'order_num',firstnumber)
if order==nil then
return nil,"MoveOrder(): Can`t find order number="..firstnumber.." in orders table!"
end
if getSecurityInfo("",order.sec_code).class_code=="SPBFUT" then
local transaction={}
if (SysFunc.orderflags2table(order.flags).cancelled==1 or (SysFunc.orderflags2table(order.flags).done==1 and order.balance==0)) then
return nil,"MoveOrder(): Can`t move cancelled or done order!"
end
transaction["FIRST_ORDER_NUMBER"]=firstnumber
transaction["FIRST_ORDER_NEW_PRICE"]=SysFunc.toPrice(order.sec_code,firstprice)
transaction["FIRST_ORDER_NEW_QUANTITY"]="0"
transaction["MODE"]="0"
transaction["TRANS_ID"]=tostring(math.random(2000000000))
transaction["CLASSCODE"]=getSecurityInfo("",SecCode).class_code
transaction["SECCODE"]=order.sec_code
transaction["ACTION"]="MOVE_ORDERS"
local res=sendTransaction(transaction)
if res~="" then
Logging.DebugLog("","MoveOrder():order",order)
Logging.DebugLog("","MoveOrder():transaction",transaction)
return nil, "MoveOrder():"..res
else
return transaction["TRANS_ID"], "MoveOrder(): Move order sent sucesfully. Mode="..mode.." Number="..firstnumber.." Price="..firstprice
end
else
if (SysFunc.orderflags2table(order.flags).cancelled==1 or (SysFunc.orderflags2table(order.flags).done==1 and order.balance==0)) then
return nil,"MoveOrder(): Can`t move cancelled or done order!"
end
KillOrder(firstnumber,order.sec_code,getSecurityInfo("",order.sec_code).class_code)
a,b,c=SendLimitOrder(getSecurityInfo("",order.sec_code).class_code,order.sec_code,SysFunc.orderflags2table(order.flags).operation,firstprice,order.balance,order.account,order.client_code)
if c~="" then
Logging.DebugLog("","MoveOrder():order",order)
Logging.DebugLog("","MoveOrder():transaction",transaction)
return nil, "MoveOrder():"..c
else
return a, "MoveOrder(): Move order sent sucesfully. Mode="..mode.." Number="..firstnumber.." Price="..firstprice
end
end
end
function KillOrder(orderkey,security,class)
-- ôóíêöèÿ îòìåíû ëèìèòèðîâàííîé çàÿâêè ïî íîìåðó
-- ïðèíèìàåò ìèíèìóì 1 ïàðàìåð
-- ÂÀÆÍÎ! Äàííàÿ ôóíêöèÿ íå ãàðàíòèðóåò ñíÿòèå çàÿâêè
-- Âîçâðàùàåò ñîîáùåíèå ñåðâåðà â ñëó÷àå îøèáêè âûÿâëåííîé ñåðâåðîì Êâèê ëèáî ñòðîêó ñ èíôîðìàöèåé î òðàíçàêöèè
if orderkey==nil or tonumber(orderkey)==0 then
return nil,"KillOrder(): Can`t kill order. OrderKey nil or zero"
end
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="KILL_ORDER",
["ORDER_KEY"]=tostring(orderkey)
}
if security then
transaction.seccode=security
transaction.classcode=class or getSecurityInfo("",security).class_code
else
local order=SysFunc.GetRowFromTable("orders",'order_num',orderkey)
if order==nil then return nil,"KillOrder(): Can`t kill order. No such order in Orders table." end
transaction.classcode=order.class_code
transaction.seccode=order.sec_code
end
--toLog("ko.txt",transaction)
local res=sendTransaction(transaction)
if res~="" then
return nil,"KillOrder(): "..res
else
return transaction["TRANS_ID"],"KillOrder(): Limit order kill sended. Class="..transaction.classcode.." Sec="..transaction.seccode.." Key="..orderkey.." Trans_id="..transaction["TRANS_ID"]
end
end
function KillStopOrder(orderkey,security,class)
-- ôóíêöèÿ îòìåíû ñòîï-çàÿâêè ïî íîìåðó
-- ïðèíèìàåò ìèíèìóì 1 ïàðàìåð
-- ÂÀÆÍÎ! Äàííàÿ ôóíêöèÿ íå ãàðàíòèðóåò ñíÿòèå çàÿâêè
-- Âîçâðàùàåò ñîîáùåíèå ñåðâåðà â ñëó÷àå îøèáêè âûÿâëåííîé ñåðâåðîì Êâèê ëèáî ñòðîêó ñ èíôîðìàöèåé î òðàíçàêöèè
if orderkey==nil or tonumber(orderkey)==0 then
return nil,"KillStopOrder(): Can`t kill order. OrderKey nil or zero"
end
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="KILL_STOP_ORDER",
["STOP_ORDER_KEY"]=tostring(orderkey)
}
if security==nil or class==nil then
local order=SysFunc.GetRowFromTable("stop_orders",'order_num',orderkey)
if order==nil then return nil,"KillStopOrder(): Can`t kill order. No such order in StopOrders table." end
transaction.classcode=order.class_code
transaction.seccode=order.sec_code
else
transaction.seccode=security
transaction.classcode=class
end
--toLog("ko.txt",transaction)
local res=sendTransaction(transaction)
if res~="" then
return nil,"KillStopOrder(): "..res
else
return transaction["TRANS_ID"],"KillStopOrder(): Stop-order kill sended. Class="..transaction.classcode.." Sec="..transaction.seccode.." Key="..orderkey.." Trans_id="..transaction["TRANS_ID"]
end
end
function KillAllStops(ClassCode,SecCode)
--[[
äàííàÿ ôóíêöèÿ îòïðàâèò òðàíçàêöèè íà îòìåíó ÀÊÒÈÂÍÛÕ ÑÒÎÏçàÿâîê ñîîòâåòñòâóþùèì
ClassCode è SecCode
]]
local ord = "stop_orders"
for i=0,getNumberOf(ord) do
local t=getItem(ord, i)
if t ~= nil and type(t) == "table" then
if( t.seccode == SecCode and SysFunc.SysFunc.orderflags2table(t.flags).active == 1) then
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="KILL_STOP_ORDER",
["CLASSCODE"]=ClassCode,
["SECCODE"]=SecCode,
["STOP_ORDER_KEY"]=tostring(t.ordernum),
}
local res=sendTransaction(transaction)
if res~="" then
return nil, "qlib.KillAllStops():"..res
else
return trans_id, res
end
end
end
end
end
function KillAllOrders(ClassCode,SecCode)
--[[
äàííàÿ ôóíêöèÿ îòïðàâèò òðàíçàêöèè íà îòìåíó ÀÊÒÈÂÍÛÕ çàÿâîê ñîîòâåòñòâóþùèì
ClassCode è SecCode
]]
local ord = "orders"
for i=0,getNumberOf(ord) do
local t=getItem(ord, i)
if t ~= nil and type(t) == "table" then
if( t.seccode == SecCode and SysFunc.SysFunc.orderflags2table(t.flags).active == 1) then
local transaction={
["TRANS_ID"]=tostring(math.random(2000000000)),
["ACTION"]="KILL_ORDER",
["CLASSCODE"]=ClassCode,
["SECCODE"]=SecCode,
["ORDER_KEY"]=tostring(t.ordernum),
}
local res=sendTransaction(transaction)
end
end
end
end
function GetLastOrder(SecCode) --â ïðèíöèïå íå íóæíà
local ord = "trades"
for i=getNumberOf(ord),0,-1 do
local t=getItem(ord, i)
if t ~= nil and type(t) == "table" then
if( t.seccode == SecCode) then
return t
end
end
end
return 0
end
function RoundToStep(ClassCode,SecCode,val)
--[[
îêðóãëÿåò çíà÷åíèå val äî øàãà öåíû SecCode èíñòðóìåíòà
îêðóãëÿåò âíèç
]]
if (ClassCode == nil or SecCode == nil or val == nil) then
message("qlib.RoundToStep(): error occured, one of parameters is nil",3)
return 0
end
local t = getParamEx(ClassCode,SecCode,"SEC_PRICE_STEP")
local st=t.param_value
if st==0 then
message("qlib.RoundToStep(): step=0",3)
end
if st~=0 then
return math.floor(val / st)*st
end
end
function GetBoughtCount(SecCode)
--[[
âîçâðàùàåò òåêóùóþ ÷èñòóþ ïîçèöèþ ïî SecCode áóìàãå
]]
local ClassCode=getSecurityInfo("",SecCode).class_code
if ClassCode=="SPBFUT" then
local fch = "futures_client_holding"
for i=0,getNumberOf(fch) do
local t=getItem(fch, i)
if t ~= nil and type(t) == "table" then
if( t.seccode == SecCode) then
if t.totalnet==nil then
return 0
else
return t.totalnet
end
end
end
end
else
for i=0,getNumberOf("depo_limits") do
local row=getItem("depo_limits",i)
--toLog(log,row)
if row~=nil and row['sec_code']==SecCode then
if row.currentbal==nil then
return 0
else
return tonumber(row.currentbal)
end
end
end
end
return 0
end
function GetMargin(SecCode)
--[[
âîçâðàùàåò ìàðæó ïî SecCode áóìàãå
çíà÷åíèå áåðåòñÿ èç êëèåíòñêîé òàáëèöû, ïîòîìó ÷òî ìíå ëåíü ñ÷èòàòü.
â íàñòðîéêàõ êâèêà ìîæíî ïîñòàâèòü îáíîâëåíèå ýòîé òàáëèöû êàæäóþ ñåêóíäó
]]
local fch = "futures_client_holding"
for i=0,getNumberOf(fch) do
local t=getItem(fch, i)
if t ~= nil and type(t) == "table" then
if( t.seccode == SecCode) then
if t.varmargin==nil then
return 0
else
return t.varmargin
end
end
end
end
return 0
end
function GetStopCount(SecCode)
--[[
âîçâðàùàåò êîëè÷åñòâî ñòîï-çàÿâîê ïî SecCode áóìàãå
]]
local count=0
local fch = "stop_orders"
for i=0,getNumberOf(fch) do
local t=getItem(fch, i)
if t ~= nil and type(t) == "table" then
if( SysFunc.SysFunc.orderflags2table(t.flags).active == 1) then
count=count+1
end
end
end
return count
end