-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathqsfunctions.lua
More file actions
1111 lines (986 loc) · 39.6 KB
/
Copy pathqsfunctions.lua
File metadata and controls
1111 lines (986 loc) · 39.6 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--~ Copyright (c) 2014-2020 QUIKSharp Authors https://github.com/finsight/QUIKSharp/blob/master/AUTHORS.md. All rights reserved.
--~ Licensed under the Apache License, Version 2.0. See LICENSE.txt in the project root for license information.
local json = require ("dkjson")
local qsfunctions = {}
function qsfunctions.dispatch_and_process(msg)
if qsfunctions[msg.cmd] then
-- dispatch a command simply by a table lookup
-- in qsfunctions method names must match commands
local status, result = pcall(qsfunctions[msg.cmd], msg)
if status then
return result
else
msg.cmd = "lua_error"
msg.lua_error = "Lua error: " .. result
return msg
end
else
log(to_json(msg), 3)
msg.lua_error = "Command not implemented in Lua qsfunctions module: " .. msg.cmd
msg.cmd = "lua_error"
return msg
end
end
---------------------
-- Debug functions --
---------------------
--- Returns Pong to Ping
-- @param msg message table
-- @return same msg table
function qsfunctions.ping(msg)
-- need to know data structure the caller gives
msg.t = 0 -- avoid time generation. Could also leave original
if msg.data == "Ping" then
msg.data = "Pong"
return msg
else
msg.data = msg.data .. " is not Ping"
return msg
end
end
--- Echoes its message
function qsfunctions.echo(msg)
return msg
end
--- Test error handling
function qsfunctions.divide_string_by_zero(msg)
msg.data = "asd" / 0
return msg
end
--- Is running inside quik
function qsfunctions.is_quik(msg)
if getScriptPath then msg.data = 1 else msg.data = 0 end
return msg
end
-----------------------
-- Service functions --
-----------------------
--- Функция предназначена для определения состояния подключения клиентского места к
-- серверу. Возвращает «1», если клиентское место подключено и «0», если не подключено.
function qsfunctions.isConnected(msg)
-- set time when function was called
msg.t = timemsec()
msg.data = isConnected()
return msg
end
--- Функция возвращает путь, по которому находится файл info.exe, исполняющий данный
-- скрипт, без завершающего обратного слэша («\»). Например, C:\QuikFront.
function qsfunctions.getWorkingFolder(msg)
-- set time when function was called
msg.t = timemsec()
msg.data = getWorkingFolder()
return msg
end
--- Функция возвращает путь, по которому находится запускаемый скрипт, без завершающего
-- обратного слэша («\»). Например, C:\QuikFront\Scripts.
function qsfunctions.getScriptPath(msg)
-- set time when function was called
msg.t = timemsec()
msg.data = getScriptPath()
return msg
end
--- Функция возвращает значения параметров информационного окна (пункт меню
-- Связь / Информационное окно…).
function qsfunctions.getInfoParam(msg)
-- set time when function was called
msg.t = timemsec()
msg.data = getInfoParam(msg.data)
return msg
end
--- Функция отображает сообщения в терминале QUIK.
function qsfunctions.message(msg)
log(msg.data, 1)
msg.data = ""
return msg
end
function qsfunctions.warning_message(msg)
log(msg.data, 2)
msg.data = ""
return msg
end
function qsfunctions.error_message(msg)
log(msg.data, 3)
msg.data = ""
return msg
end
--- Функция приостанавливает выполнение скрипта.
function qsfunctions.sleep(msg)
delay(msg.data)
msg.data = ""
return msg
end
--- Функция для вывода отладочной информации.
function qsfunctions.PrintDbgStr(msg)
log(msg.data, 0)
msg.data = ""
return msg
end
-- Выводит на график метку
function qsfunctions.addLabel(msg)
local spl = split(msg.data, "|")
local price, curdate, curtime, qty, path, id, algmnt, bgnd = spl[1], spl[2], spl[3], spl[4], spl[5], spl[6], spl[7], spl[8]
label = {
TEXT = "",
IMAGE_PATH = path,
ALIGNMENT = algmnt,
YVALUE = tostring(price),
DATE = tostring(curdate),
TIME = tostring(curtime),
R = 255,
G = 255,
B = 255,
TRANSPARENCY = 0,
TRANSPARENT_BACKGROUND = bgnd,
FONT_FACE_NAME = "Arial",
FONT_HEIGHT = "15",
HINT = " " .. tostring(price) .. " " .. tostring(qty)
}
local res = AddLabel(id, label)
msg.data = res
return msg
end
-- Выводит на график метку
-- Функция возвращает числовой идентификатор метки. В случае неуспешного завершения функция возвращает «nil».
function qsfunctions.addLabel2(msg)
local spl = split2(msg.data, "|");
local chartTag, yValue, strDate, strTime, text, imagePath, alignment, hint, r, g, b, transparency, tranBackgrnd, fontName, fontHeight =
spl[1], spl[2], spl[3], spl[4], spl[5], spl[6], spl[7], spl[8], spl[9], spl[10], spl[11], spl[12], spl[13], spl[14], spl[15];
-- значения по умолчанию
if text == "" then text = nil else r = 255 end
if imagePath == "" then imagePath = nil end
if alignment == "" then alignment = nil end
if hint == "" then hint = nil end
if r == "-1" then r = nil end
if g == "-1" then g = nil end
if b == "-1" then b = nil end
if transparency == "-1" then transparency = nil end
if tranBackgrnd == "-1" then tranBackgrnd = nil end
if fontName == "" then fontName = nil end
if fontHeight == "-1" then fontHeight = nil end
local labelParams = {
YVALUE = yValue:gsub(",", "."),
DATE = strDate,
TIME = strTime,
TEXT = text,
IMAGE_PATH = imagePath,
ALIGNMENT = alignment,
HINT = hint,
R = r,
G = g,
B = b,
TRANSPARENCY = transparency,
TRANSPARENT_BACKGROUND = tranBackgrnd,
FONT_FACE_NAME = fontName,
FONT_HEIGHT = fontHeight,
}
local res = AddLabel(chartTag, labelParams);
msg.data = res;
return msg;
end
-- Функция задает параметры для метки с указанным идентификатором.
-- В случае успешного завершения функция возвращает «true», иначе – «false».
function qsfunctions.setLabelParams(msg)
local spl = split2(msg.data, "|");
local chartTag, labelId, yValue, strDate, strTime, text, imagePath, alignment, hint, r, g, b, transparency, tranBackgrnd, fontName, fontHeight =
spl[1], spl[2], spl[3], spl[4], spl[5], spl[6], spl[7], spl[8], spl[9], spl[10], spl[11], spl[12], spl[13], spl[14], spl[15], spl[16];
-- значения по умолчанию
if text == "" then text = nil else r = 255 end
if imagePath == "" then imagePath = nil end
if alignment == "" then alignment = nil end
if hint == "" then hint = nil end
if r == "-1" then r = nil end
if g == "-1" then g = nil end
if b == "-1" then b = nil end
if transparency == "-1" then transparency = nil end
if tranBackgrnd == "-1" then tranBackgrnd = nil end
if fontName == "" then fontName = nil end
if fontHeight == "-1" then fontHeight = nil end
local labelParams = {
YVALUE = yValue,
DATE = strDate,
TIME = strTime,
TEXT = text,
IMAGE_PATH = imagePath,
ALIGNMENT = alignment,
HINT = hint,
R = r,
G = g,
B = b,
TRANSPARENCY = transparency,
TRANSPARENT_BACKGROUND = tranBackgrnd,
FONT_FACE_NAME = fontName,
FONT_HEIGHT = fontHeight,
}
local res = SetLabelParams(chartTag, tonumber(labelId), labelParams);
msg.data = tostring(res);
return msg;
end
-- позволяет получить параметры метки
-- Функция возвращает таблицу с параметрами метки. В случае неуспешного завершения функция возвращает «nil».
function qsfunctions.getLabelParams(msg)
local spl = split2(msg.data, "|");
local chartTag, labelId = spl[1], spl[2];
local res = GetLabelParams(chartTag, tonumber(labelId));
msg.data = res;
return msg;
end
-- Удаляем выбранную метку
function qsfunctions.delLabel(msg)
local spl = split(msg.data, "|")
local tag, id = spl[1], spl[2]
DelLabel(tag, tonumber(id))
msg.data = ""
return msg
end
-- Удаляем все метки с графика
function qsfunctions.delAllLabels(msg)
local spl = split(msg.data, "|")
local id = spl[1]
DelAllLabels(id)
msg.data = ""
return msg
end
---------------------
-- Class functions --
---------------------
--- Функция предназначена для получения списка кодов классов, переданных с сервера в ходе сеанса связи.
function qsfunctions.getClassesList(msg)
msg.data = getClassesList()
-- if msg.data then log(msg.data) else log("getClassesList returned nil") end
return msg
end
--- Функция предназначена для получения информации о классе.
function qsfunctions.getClassInfo(msg)
msg.data = getClassInfo(msg.data)
-- if msg.data then log(msg.data.name) else log("getClassInfo returned nil") end
return msg
end
--- Функция предназначена для получения списка кодов бумаг для списка классов, заданного списком кодов.
function qsfunctions.getClassSecurities(msg)
msg.data = getClassSecurities(msg.data)
-- if msg.data then log(msg.data) else log("getClassSecurities returned nil") end
return msg
end
--- Функция получает информацию по указанному классу и бумаге.
function qsfunctions.getSecurityInfo(msg)
local spl = split(msg.data, "|")
local class_code, sec_code = spl[1], spl[2]
msg.data = getSecurityInfo(class_code, sec_code)
return msg
end
--- Функция берет на вход список из элементов в формате class_code|sec_code и возвращает список ответов функции getSecurityInfo.
-- Если какая-то из бумаг не будет найдена, вместо ее значения придет null
function qsfunctions.getSecurityInfoBulk(msg)
local result = {}
for i=1,#msg.data do
local spl = split(msg.data[i], "|")
local class_code, sec_code = spl[1], spl[2]
local status, security = pcall(getSecurityInfo, class_code, sec_code)
if status and security then
table.insert(result, security)
else
if not status then
log("Error happened while calling getSecurityInfoBulk with ".. class_code .. "|".. sec_code .. ": ".. security)
end
table.insert(result, json.null)
end
end
msg.data = result
return msg
end
--- Функция предназначена для определения класса по коду инструмента из заданного списка классов.
function qsfunctions.getSecurityClass(msg)
local spl = split(msg.data, "|")
local classes_list, sec_code = spl[1], spl[2]
for class_code in string.gmatch(classes_list,"([^,]+)") do
if getSecurityInfo(class_code,sec_code) then
msg.data = class_code
return msg
end
end
msg.data = ""
return msg
end
--- Функция возвращает код клиента
function qsfunctions.getClientCode(msg)
for i=0,getNumberOf("MONEY_LIMITS")-1 do
local clientcode = getItem("MONEY_LIMITS",i).client_code
if clientcode ~= nil then
msg.data = clientcode
return msg
end
end
return msg
end
--- Функция возвращает все коды клиента
function qsfunctions.getClientCodes(msg)
local client_codes = {}
for i=0,getNumberOf("MONEY_LIMITS")-1 do
local clientcode = getItem("MONEY_LIMITS",i).client_code
if clientcode ~= nil then
fnd = false
for index, value in ipairs(client_codes) do
if value == clientcode then
fnd = true
end
end
if fnd == false then
table.insert(client_codes, clientcode)
end
end
end
msg.data = client_codes
return msg
end
--- Функция возвращает торговый счет для запрашиваемого кода класса
function qsfunctions.getTradeAccount(msg)
for i=0,getNumberOf("trade_accounts")-1 do
local trade_account = getItem("trade_accounts",i)
if string.find(trade_account.class_codes,'|' .. msg.data .. '|',1,1) then
msg.data = trade_account.trdaccid
return msg
end
end
return msg
end
--- Функция возвращает торговые счета в системе, у которых указаны поддерживаемые классы инструментов.
function qsfunctions.getTradeAccounts(msg)
local trade_accounts = {}
for i=0,getNumberOf("trade_accounts")-1 do
local trade_account = getItem("trade_accounts",i)
if trade_account.class_codes ~= "" then
table.insert(trade_accounts, trade_account)
end
end
msg.data = trade_accounts
return msg
end
---------------------------------------------------------------------
-- Order Book functions (Функции для работы со стаканом котировок) --
---------------------------------------------------------------------
--- Функция заказывает на сервер получение стакана по указанному классу и бумаге.
function qsfunctions.Subscribe_Level_II_Quotes(msg)
local spl = split(msg.data, "|")
local class_code, sec_code = spl[1], spl[2]
msg.data = Subscribe_Level_II_Quotes(class_code, sec_code)
return msg
end
--- Функция отменяет заказ на получение с сервера стакана по указанному классу и бумаге.
function qsfunctions.Unsubscribe_Level_II_Quotes(msg)
local spl = split(msg.data, "|")
local class_code, sec_code = spl[1], spl[2]
msg.data = Unsubscribe_Level_II_Quotes(class_code, sec_code)
return msg
end
--- Функция позволяет узнать, заказан ли с сервера стакан по указанному классу и бумаге.
function qsfunctions.IsSubscribed_Level_II_Quotes(msg)
local spl = split(msg.data, "|")
local class_code, sec_code = spl[1], spl[2]
msg.data = IsSubscribed_Level_II_Quotes(class_code, sec_code)
return msg
end
--- Функция предназначена для получения стакана по указанному классу и инструменту.
function qsfunctions.GetQuoteLevel2(msg)
local spl = split(msg.data, "|")
local class_code, sec_code = spl[1], spl[2]
local server_time = getInfoParam("SERVERTIME")
local status, ql2 = pcall(getQuoteLevel2, class_code, sec_code)
if status then
msg.data = ql2
msg.data.class_code = class_code
msg.data.sec_code = sec_code
msg.data.server_time = server_time
else
OnError(ql2)
end
return msg
end
-----------------------
-- Trading functions --
-----------------------
--- Функция предназначена для расчета максимально возможного количества лотов в заявке.
-- При заданном параметре is_market=true, необходимо передать параметр price=0, иначе будет рассчитано максимально возможное количество лотов в заявке по цене price.
function qsfunctions.calc_buy_sell(msg)
local bs = CalcBuySell
local spl = split(msg.data, "|")
local class_code, sec_code, clientCode, account, price, is_buy, is_market = spl[1], spl[2], spl[3], spl[4], spl[5], spl[6], spl[7]
if is_buy == "True" then
is_buy = true
else
is_buy = false
end
if is_market == "True" then
is_market = true
else
is_market = false
end
local qty, comiss = bs(class_code, sec_code, clientCode, account, tonumber(price), is_buy, is_market)
if qty ~= "" then
msg.data = {}
msg.data.qty = qty
msg.data.comission = comiss
else
message("Ошибка функции CalcBuySell", 1)
end
return msg
end
--- отправляет транзакцию на сервер и возвращает пустое сообщение, которое
-- будет проигноировано. Вместо него, отправитель будет ждать события
-- OnTransReply, из которого по TRANS_ID он получит результат отправленной транзакции
function qsfunctions.sendTransaction(msg)
local res = sendTransaction(msg.data)
if res~="" then
-- error handling
msg.cmd = "lua_transaction_error"
msg.lua_error = res
return msg
else
-- transaction sent
msg.data = true
return msg
end
end
--- Функция заказывает получение параметров Таблицы текущих торгов. В случае успешного завершения функция возвращает «true», иначе – «false»
function qsfunctions.paramRequest(msg)
local spl = split(msg.data, "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
msg.data = ParamRequest(class_code, sec_code, param_name)
return msg
end
--- Функция принимает список строк (JSON Array) в формате class_code|sec_code|param_name, вызывает функцию paramRequest для каждой строки.
-- Возвращает список ответов в том же порядке
function qsfunctions.paramRequestBulk(msg)
local result = {}
for i=1,#msg.data do
local spl = split(msg.data[i], "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
table.insert(result, ParamRequest(class_code, sec_code, param_name))
end
msg.data = result
return msg
end
--- Функция отменяет заказ на получение параметров Таблицы текущих торгов. В случае успешного завершения функция возвращает «true», иначе – «false»
function qsfunctions.cancelParamRequest(msg)
local spl = split(msg.data, "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
msg.data = CancelParamRequest(class_code, sec_code, param_name)
return msg
end
--- Функция принимает список строк (JSON Array) в формате class_code|sec_code|param_name, вызывает функцию CancelParamRequest для каждой строки.
-- Возвращает список ответов в том же порядке
function qsfunctions.cancelParamRequestBulk(msg)
local result = {}
for i=1,#msg.data do
local spl = split(msg.data[i], "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
table.insert(result, CancelParamRequest(class_code, sec_code, param_name))
end
msg.data = result
return msg
end
--- Функция предназначена для получения значений всех параметров биржевой информации из Таблицы текущих значений параметров.
-- С помощью этой функции можно получить любое из значений Таблицы текущих значений параметров для заданных кодов класса и бумаги.
function qsfunctions.getParamEx(msg)
local spl = split(msg.data, "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
msg.data = getParamEx(class_code, sec_code, param_name)
return msg
end
--- Функция предназначена для получения значении? всех параметров биржевои? информации из Таблицы текущих торгов
-- с возможностью в дальнеи?шем отказаться от получения определенных параметров, заказанных с помощью функции ParamRequest.
-- Для отказа от получения какого-либо параметра воспользуи?тесь функциеи? CancelParamRequest.
-- Функция возвращает таблицу Lua с параметрами, аналогичными параметрам, возвращаемым функциеи? getParamEx
function qsfunctions.getParamEx2(msg)
local spl = split(msg.data, "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
msg.data = getParamEx2(class_code, sec_code, param_name)
return msg
end
--- Функция принимает список строк (JSON Array) в формате class_code|sec_code|param_name и возвращает результаты вызова
-- функции getParamEx2 для каждой строки запроса в виде списка в таком же порядке, как в запросе
function qsfunctions.getParamEx2Bulk(msg)
local result = {}
for i=1,#msg.data do
local spl = split(msg.data[i], "|")
local class_code, sec_code, param_name = spl[1], spl[2], spl[3]
table.insert(result, getParamEx2(class_code, sec_code, param_name))
end
msg.data = result
return msg
end
-- Функция предназначена для получения информации по бумажным лимитам.
function qsfunctions.getDepo(msg)
local spl = split(msg.data, "|")
local clientCode, firmId, secCode, account = spl[1], spl[2], spl[3], spl[4]
msg.data = getDepo(clientCode, firmId, secCode, account)
return msg
end
-- Функция предназначена для получения информации по бумажным лимитам.
function qsfunctions.getDepoEx(msg)
local spl = split(msg.data, "|")
local firmId, clientCode, secCode, account, limit_kind = spl[1], spl[2], spl[3], spl[4], spl[5]
msg.data = getDepoEx(firmId, clientCode, secCode, account, tonumber(limit_kind))
return msg
end
-- Функция для получения информации по денежным лимитам.
function qsfunctions.getMoney(msg)
local spl = split(msg.data, "|")
local client_code, firm_id, tag, curr_code = spl[1], spl[2], spl[3], spl[4]
msg.data = getMoney(client_code, firm_id, tag, curr_code)
return msg
end
-- Функция для получения информации по денежным лимитам указанного типа.
function qsfunctions.getMoneyEx(msg)
local spl = split(msg.data, "|")
local firm_id, client_code, tag, curr_code, limit_kind = spl[1], spl[2], spl[3], spl[4], spl[5]
msg.data = getMoneyEx(firm_id, client_code, tag, curr_code, tonumber(limit_kind))
return msg
end
-- Функция возвращает информацию по всем денежным лимитам.
function qsfunctions.getMoneyLimits(msg)
local limits = {}
for i=0,getNumberOf("money_limits")-1 do
local limit = getItem("money_limits",i)
table.insert(limits, limit)
end
msg.data = limits
return msg
end
-- Функция предназначена для получения информации по фьючерсным лимитам.
function qsfunctions.getFuturesLimit(msg)
local spl = split(msg.data, "|")
local firmId, accId, limitType, currCode = spl[1], spl[2], spl[3], spl[4]
local result, err = getFuturesLimit(firmId, accId, limitType*1, currCode)
if result then
msg.data = result
else
log("Futures limit returns nil", 3)
msg.data = nil
end
return msg
end
-- Функция возвращает информацию по фьючерсным лимитам для всех торговых счетов.
function qsfunctions.getFuturesClientLimits(msg)
local limits = {}
for i=0,getNumberOf("futures_client_limits")-1 do
local limit = getItem("futures_client_limits",i)
table.insert(limits, limit)
end
msg.data = limits
return msg
end
function qsfunctions.getFuturesHolding(msg)
local spl = split(msg.data, "|")
local firmId, accId, secCode, posType = spl[1], spl[2], spl[3], spl[4]
local result, err = getFuturesHolding(firmId, accId, secCode, posType*1)
if result then
msg.data = result
else
--log("Futures holding returns nil", 3)
msg.data = nil
end
return msg
end
-- Функция для получения информации по всем фьючерсным позициям
function qsfunctions.getFuturesClientHoldings(msg)
local holdings = {}
for i=0,getNumberOf("futures_client_holding")-1 do
local holding = getItem("futures_client_holding",i)
table.insert(holdings, holding)
end
msg.data = holdings
return msg
end
-- Функция возвращает таблицу заявок (всю или по заданному инструменту)
function qsfunctions.get_orders(msg)
if msg.data ~= "" then
local spl = split(msg.data, "|")
class_code, sec_code = spl[1], spl[2]
end
local orders = {}
for i = 0, getNumberOf("orders") - 1 do
local order = getItem("orders", i)
if msg.data == "" or (order.class_code == class_code and order.sec_code == sec_code) then
table.insert(orders, order)
end
end
msg.data = orders
return msg
end
-- Функция возвращает заявку по заданному инструменту и ID-транзакции
function qsfunctions.getOrder_by_ID(msg)
if msg.data ~= "" then
local spl = split(msg.data, "|")
class_code, sec_code, trans_id = spl[1], spl[2], spl[3]
end
local order_num = 0
local res
for i = 0, getNumberOf("orders") - 1 do
local order = getItem("orders", i)
if order.class_code == class_code and order.sec_code == sec_code and order.trans_id == tonumber(trans_id) and order.order_num > order_num then
order_num = order.order_num
res = order
end
end
msg.data = res
return msg
end
---- Функция возвращает заявку по номеру
function qsfunctions.getOrder_by_Number(msg)
for i=0,getNumberOf("orders")-1 do
local order = getItem("orders",i)
if order.order_num == tonumber(msg.data) then
msg.data = order
return msg
end
end
return msg
end
--- Возвращает заявку по её номеру и классу инструмента ---
--- На основе http://help.qlua.org/ch4_5_1_1.htm ---
function qsfunctions.get_order_by_number(msg)
local spl = split(msg.data, "|")
local class_code = spl[1]
local order_id = tonumber(spl[2])
msg.data = getOrderByNumber(class_code, order_id)
return msg
end
--- Возвращает список записей из таблицы 'Лимиты по бумагам'
--- На основе http://help.qlua.org/ch4_6_11.htm и http://help.qlua.org/ch4_5_3.htm
function qsfunctions.get_depo_limits(msg)
local sec_code = msg.data
local count = getNumberOf("depo_limits")
local depo_limits = {}
for i = 0, count - 1 do
local depo_limit = getItem("depo_limits", i)
if msg.data == "" or depo_limit.sec_code == sec_code then
table.insert(depo_limits, depo_limit)
end
end
msg.data = depo_limits
return msg
end
-- Функция возвращает таблицу сделок (всю или по заданному инструменту)
function qsfunctions.get_trades(msg)
if msg.data ~= "" then
local spl = split(msg.data, "|")
class_code, sec_code = spl[1], spl[2]
end
local trades = {}
for i = 0, getNumberOf("trades") - 1 do
local trade = getItem("trades", i)
if msg.data == "" or (trade.class_code == class_code and trade.sec_code == sec_code) then
table.insert(trades, trade)
end
end
msg.data = trades
return msg
end
-- Функция возвращает таблицу сделок по номеру заявки
function qsfunctions.get_Trades_by_OrderNumber(msg)
local order_num = tonumber(msg.data)
local trades = {}
for i = 0, getNumberOf("trades") - 1 do
local trade = getItem("trades", i)
if trade.order_num == order_num then
table.insert(trades, trade)
end
end
msg.data = trades
return msg
end
-- Функция предназначена для получения значений параметров таблицы «Клиентский портфель», соответствующих идентификатору участника торгов «firmid» и коду клиента «client_code».
function qsfunctions.getPortfolioInfo(msg)
local spl = split(msg.data, "|")
local firmId, clientCode = spl[1], spl[2]
msg.data = getPortfolioInfo(firmId, clientCode)
return msg
end
-- Функция предназначена для получения значений параметров таблицы «Клиентский портфель», соответствующих идентификатору участника торгов «firmid», коду клиента «client_code» и виду лимита «limit_kind».
function qsfunctions.getPortfolioInfoEx(msg)
local spl = split(msg.data, "|")
local firmId, clientCode, limit_kind = spl[1], spl[2], spl[3]
msg.data = getPortfolioInfoEx(firmId, clientCode, tonumber(limit_kind))
return msg
end
-- Функция предназначена для получения таблицы обезличенных сделок по выбранному инструменту или всю целиком.
function qsfunctions.get_all_trades(msg)
if msg.data ~= "" then
local spl = split(msg.data, "|")
class_code, sec_code = spl[1], spl[2]
end
local trades = {}
for i = 0, getNumberOf("all_trades") - 1 do
local trade = getItem("all_trades", i)
if msg.data == "" or (trade.class_code == class_code and trade.sec_code == sec_code) then
table.insert(trades, trade)
end
end
msg.data = trades
return msg
end
--------------------------
-- OptionBoard functions --
--------------------------
function qsfunctions.getOptionBoard(msg)
local spl = split(msg.data, "|")
local classCode, secCode, series = spl[1], spl[2], spl[3]
local result, err = getOptions(classCode, secCode, series )
if result then
msg.data = result
else
log("Option board returns nil", 3)
msg.data = nil
end
return msg
end
function getOptions(classCode,secCode,series)
--classCode = "SPBOPT"
--BaseSecList="RIZ6"
--series: 0 - ближайшая неделя, 1 - ближний месяц, 2 - ближний квартал, 4 - все
local SecList = getClassSecurities(classCode) --все сразу
local t={}
local p={}
local week = false
local month = false
local quartal = false
local all = false
local len = 0;
local days_to_mat
local last_char
for sec in string.gmatch(SecList, "([^,]+)") do --перебираем опционы по очереди.
week = false
month = false
quartal = false
all = false
local Optionbase=getParamEx(classCode,sec,"optionbase").param_image
if (string.find(secCode,Optionbase)~=nil ) then
days_to_mat = getParamEx(classCode,sec,"DAYS_TO_MAT_DATE").param_value+0
len = string.len(sec)
last_char = string.sub(sec, len)
--log("Last char:"..last_char)
--log("Type:"..type(tonumber(last_char)))
if(tonumber(last_char) ~= nil) then
-- log("Convert: "..tonumber(last_char))
month = true
end
if( tonumber(days_to_mat) <= 8 ) then
-- log("this week".."Sec:"..sec.." Days:"..days_to_mat)
week = true
else
-- log("Sec:"..sec.." Days:"..days_to_mat)
end
if(( tonumber(series) == 0 and week) or (tonumber(series) == 1 and month) ) or tonumber(series) == 4 then
p={
["code"]=getParamEx(classCode,sec,"code").param_image,
["Name"]=getSecurityInfo(classCode,sec).name,
["DAYS_TO_MAT_DATE"]=days_to_mat,
["BID"]=getParamEx(classCode,sec,"BID").param_value+0,
["OFFER"]=getParamEx(classCode,sec,"OFFER").param_value+0,
["OPTIONBASE"]=Optionbase,
["OPTIONTYPE"]=getParamEx(classCode,sec,"optiontype").param_image,
["Longname"]=getParamEx(classCode,sec,"longname").param_image,
["shortname"]=getParamEx(classCode,sec,"shortname").param_image,
["Volatility"]=getParamEx(classCode,sec,"volatility").param_value+0,
["Strike"]=getParamEx(classCode,sec,"strike").param_value+0,
["Lastprice"]=getParamEx(classCode,sec,"last").param_value+0,
["THEORPRICE"]=getParamEx(classCode,sec,"THEORPRICE").param_value+0,
["MAT_DATE"]=getParamEx(classCode,sec,"MAT_DATE").param_image,
["STEPPRICET"]=getParamEx(classCode,sec,"STEPPRICET").param_value+0,
["SEC_PRICE_STEP"]=getParamEx(classCode,sec,"SEC_PRICE_STEP").param_value+0
}
table.insert( t, p )
end
end
end
return t
end
--------------------------
-- Stop order functions --
--------------------------
--- Возвращает список стоп-заявок
function qsfunctions.get_stop_orders(msg)
if msg.data ~= "" then
local spl = split(msg.data, "|")
class_code, sec_code = spl[1], spl[2]
end
local count = getNumberOf("stop_orders")
local stop_orders = {}
for i = 0, count - 1 do
local stop_order = getItem("stop_orders", i)
if msg.data == "" or (stop_order.class_code == class_code and stop_order.sec_code == sec_code) then
table.insert(stop_orders, stop_order)
end
end
msg.data = stop_orders
return msg
end
-------------------------
--- Candles functions ---
-------------------------
--- Возвращаем количество свечей по тегу
function qsfunctions.get_num_candles(msg)
log("Called get_num_candles" .. msg.data, 2)
local spl = split(msg.data, "|")
local tag = spl[1]
msg.data = getNumCandles(tag) * 1
return msg
end
--- Возвращаем все свечи по идентификатору графика. График должен быть открыт
function qsfunctions.get_candles(msg)
log("Called get_candles" .. msg.data, 2)
local spl = split(msg.data, "|")
local tag = spl[1]
local line = tonumber(spl[2])
local first_candle = tonumber(spl[3])
local count = tonumber(spl[4])
if count == 0 then
count = getNumCandles(tag) * 1
end
log("Count: " .. count, 2)
local t,n,l = getCandlesByIndex(tag, line, first_candle, count)
log("Candles table size: " .. n, 2)
log("Label: " .. l, 2)
local candles = {}
for i = 0, count - 1 do
table.insert(candles, t[i])
end
msg.data = candles
return msg
end
--- Возвращаем все свечи по заданному инструменту и интервалу
function qsfunctions.get_candles_from_data_source(msg)
local ds, is_error = create_data_source(msg)
if not is_error then
--- датасорс изначально приходит пустой, нужно некоторое время подождать пока он заполниться данными
repeat sleep(1) until ds:Size() > 0
local count = tonumber(split(msg.data, "|")[4]) --- возвращаем последние count свечей. Если равен 0, то возвращаем все доступные свечи.
local class, sec, interval = get_candles_param(msg)
local candles = {}
local start_i = count == 0 and 1 or math.max(1, ds:Size() - count + 1)
for i = start_i, ds:Size() do
local candle = fetch_candle(ds, i)
candle.sec = sec
candle.class = class
candle.interval = interval
table.insert(candles, candle)
end
ds:Close()
msg.data = candles
end
return msg
end
function create_data_source(msg)
local class, sec, interval = get_candles_param(msg)
local ds, error_descr = CreateDataSource(class, sec, interval)
local is_error = false
if(error_descr ~= nil) then
msg.cmd = "lua_create_data_source_error"
msg.lua_error = error_descr
is_error = true
elseif ds == nil then
msg.cmd = "lua_create_data_source_error"
msg.lua_error = "Can't create data source for " .. class .. ", " .. sec .. ", " .. tostring(interval)
is_error = true
end
return ds, is_error
end
function fetch_candle(data_source, index)
local candle = {}