-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsimple advisor v1_3_save_file_normal.lua
More file actions
432 lines (280 loc) · 12.9 KB
/
simple advisor v1_3_save_file_normal.lua
File metadata and controls
432 lines (280 loc) · 12.9 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
function OnInit()
tikers = {
"AFKS" , "AFLT" , "AGRO" , "ALRS" , "CBOM" , "CHMF" , "ENPG" , "FEES" , "FIVE" , "FIXP" , "GAZP" , "GLTR" , "GMKN" , "HYDR" , "IRAO" , "LKOH" , "MAGN" , "MGNT" , "MOEX" , "MTSS" , "NLMK" , "NVTK" , "OZON" , "PHOR" , "PIKK" , "PLZL" , "POLY" , "ROSN" , "RTKM" , "RUAL" , "SBER" , "SBERP" , "SGZH" , "SNGS" , "SNGSP" , "TATN" , "TATNP" , "TCSG" , "TRNFP" , "VKCO" , "VTBR" , "YNDX"
}
progname = "simple advisor v.1.3 : "
timeout = 10000
timeout2 = 25000 -- пауза 25 секунд для проверки старта скрипта
startind = 0 -- индекс старта скрипта (первой итерации)
-- цветовые константы
mBlack = RGB(0, 0, 0)
mWhite = RGB(255, 255, 255)
mRed = RGB(255, 204, 250)
mGreen = RGB(199, 254, 236)
mGray = RGB(226, 226, 226)
mBlue = RGB(166, 220, 243)
starttime = 100100
nomorningtime = 103000
nonewsignalstime = 180000
finishtime = 184000
discreport = 0
-- наименование файла: название скрипта дата и время
filename = string.sub(progname, 1, 14).." "..os.date('%d%m%Y_%H%M%S')
end
function OnStop()
DestroyTable(m_t)
do_it = false
message(progname.." Финиш.")
end
function open_file()
--создаем/открываем файл для записи
DirectionSaveFile=tostring("C:\\files\\"..filename..".csv")
my_csv=io.open(DirectionSaveFile,"a+")
end
function print_file(text)
my_csv:write(text..";\n")
end
function close_file()
-- закрытие файла
my_csv:flush()
my_csv:close()
end
function mprint(numb)
return string.format("%.2f", numb)
end
function hhmmss(date_time)
local Hour = date_time.hour
if Hour<10 then Hour = "0"..Hour end
local Min = date_time.min
if Min<10 then Min = "0"..Min end
local Sec=date_time.sec
if Sec<10 then Sec="0"..Sec end
return tonumber(Hour..Min..Sec)
end
function table_print(table_text, table_result_ind)
if table_result_ind <=15 then
SetCell(table_result, table_result_ind, 1, table_text)
else
InsertRow(table_result,-1)
SetCell(table_result, table_result_ind, 1, table_text)
end
end
function main()
open_file()
message(progname.." Старт.")
message(progname.." starttime = "..starttime)
print_file(progname.." starttime = "..starttime)
message(progname.." nomorningtime = "..nomorningtime)
print_file(progname.." nomorningtime = "..nomorningtime)
message(progname.." nonewsignalstime = "..nonewsignalstime)
print_file(progname.." nonewsignalstime = "..nonewsignalstime)
message(progname.." finishtime = "..finishtime)
print_file(progname.." finishtime = "..finishtime)
-- обработка starttime
while hhmmss(os.sysdate()) < starttime do
local time1 = hhmmss(os.sysdate())
message('Ожидаем время старта скрипта : '..starttime..", текущее время: "..time1)
print_file('Ожидаем время старта скрипта : '..starttime..", текущее время: "..time1)
sleep(timeout2)
end
close_file()
do_it = true
size_table = 15 -- количество строк таблицы
table_result_ind = 1 -- индекс строки
if table_result==nil then
table_result = AllocTable()
AddColumn(table_result, 1, "Сигналы и результаты:", true, QTABLE_STRING_TYPE, 85)
CreateWindow(table_result)
SetWindowPos(table_result,0,440,600,320)
SetWindowCaption(table_result, progname.." сообщения скрипта")
for u = 1, size_table do
InsertRow(table_result,-1)
end
end
if m_t==nil then
m_t = AllocTable()
AddColumn(m_t, 1, "Тикер", true, QTABLE_STRING_TYPE, 10)
AddColumn(m_t, 2, "Бумага", true, QTABLE_STRING_TYPE, 20)
AddColumn(m_t, 3, "Тек.Цена", true, QTABLE_DOUBLE_TYPE, 10)
AddColumn(m_t, 4, "Закрытие", true, QTABLE_DOUBLE_TYPE, 10)
AddColumn(m_t, 5, "Продажи", true, QTABLE_INT64_TYPE, 10)
AddColumn(m_t, 6, "Покупки", true, QTABLE_INT64_TYPE, 10)
AddColumn(m_t, 7, "Сигнал", true, QTABLE_STRING_TYPE, 33)
CreateWindow(m_t)
SetWindowPos(m_t,700,0,690,780)
SetWindowCaption(m_t, progname.." создание советника") -- показать таблицу, пишем заголовок
-- добавляем строки циклом
for u = 1, #tikers do
InsertRow(m_t,-1)
end
end
closeprice = {} -- создаем массив цен закрытия
signal = {}
lots = {} -- лоты по инструменту
buypos = {} -- покупки
sellpos = {} -- продажи
for x = 1, #tikers do
closeprice[x] = getParamEx("TQBR", tikers[x], "PREVLEGALCLOSEPR") -- цена закрытия предыдущего дня
signal[x] = 0 -- статус сигнала по инструменту
lots[x] = getParamEx("TQBR", tikers[x], "LOTSIZE")
buypos[x] = 0 -- добавляем нулевые значения в массив цен покупок
sellpos[x] = 0 -- и в массив цен продаж
end
while do_it do
-- заполнение таблицы
for i = 1, #tikers do
local tLast = getParamEx("TQBR", tikers[i], "LAST")
local tOffer = getParamEx("TQBR", tikers[i], "OFFERDEPTHT")
local tBid = getParamEx("TQBR", tikers[i], "BIDDEPTHT")
if startind == 0 then -- вывод неизменямой части таблицы
local tName = getParamEx("TQBR", tikers[i], "SHORTNAME")
SetCell(m_t, i, 1, tikers[i])
SetCell(m_t, i, 2, tName.param_image)
SetCell(m_t, i, 4, closeprice[i].param_image)
end
SetCell(m_t, i, 3, tLast.param_image)
SetCell(m_t, i, 5, tOffer.param_image)
SetCell(m_t, i, 6, tBid.param_image)
time3 = hhmmss(os.sysdate())
connect = isConnected()
if connect == 1 then
if discreport == 1 then
open_file()
discreport = 0
textdisc = " связь с сервером восстановлена"
message(progname..textdisc)
table_print(time3..textdisc, table_result_ind)
table_result_ind = table_result_ind + 1
print_file(time3..textdisc)
close_file()
startind = 0 -- доп.условие на пропуск при обновлении связи
nomorningtime = time3 - 1
end
if tonumber(tOffer.param_value) > 2 * tonumber(tBid.param_value) then
SetColor(m_t, i, 5, mRed, mBlack, mRed, mBlack)
SetColor(m_t, i, 6, mWhite, mBlack, mWhite, mBlack)
-- отработка сигналов
if tonumber(tLast.param_value) < tonumber(closeprice[i].param_value) and signal[i] == 0 then
if startind == 0 and time3 > nomorningtime then
SetCell(m_t, i, 7, "noSHORT (start pass)")
table_text = "Пропуск на старте SHORT сигнала "..tikers[i].." по цене "..tLast.param_image
signal[i] = -3
else
if time3 < nonewsignalstime then
SetCell(m_t, i, 7, "SHORT")
table_text = "сигнал SHORT по "..tikers[i].." по цене "..tLast.param_image
signal[i] = -1
sellpos[i] = tonumber(tLast.param_value)
else
SetCell(m_t, i, 7, "noSHORT (time out pass)")
table_text ="пропускаем сигнал на SHORT по таймеру "..nonewsignalstime.." по "..tikers[i].." по цене "..tLast.param_image
signal[i] = -2
end
end
message(progname..table_text)
table_print(time3.." "..table_text, table_result_ind)
table_result_ind = table_result_ind + 1
open_file()
print_file(time3.." "..table_text)
close_file()
end
elseif 2 * tonumber(tOffer.param_value) < tonumber(tBid.param_value) then
SetColor(m_t, i, 5, mWhite, mBlack, mWhite, mBlack)
SetColor(m_t, i, 6, mGreen, mBlack, mGreen, mBlack)
if tonumber(tLast.param_value) > tonumber(closeprice[i].param_value) and signal[i] == 0 then
if startind == 0 and time3 > nomorningtime then
SetCell(m_t, i, 7, "noLONG (start pass)")
table_text = "Пропуск на старте LONG сигнала по "..tikers[i].." по цене "..tLast.param_image
signal[i] = 3
else
if time3 < nonewsignalstime then
SetCell(m_t, i, 7, "LONG")
table_text = "сигнал LONG по "..tikers[i].." по цене "..tLast.param_image
signal[i] = 1
buypos[i] = tonumber(tLast.param_value)
else
SetCell(m_t, i, 7, "noLONG (time out pass)")
table_text = "пропускаем сигнал LONG по таймеру "..nonewsignalstime.." по "..tikers[i].." по цене "..tLast.param_image
signal[i] = 2
end
end
message(progname..table_text)
table_print(time3.." "..table_text, table_result_ind)
table_result_ind = table_result_ind + 1
open_file()
print_file(time3.." "..table_text)
close_file()
end
else
SetColor(m_t, i, 5, mWhite, mBlack, mWhite, mBlack)
SetColor(m_t, i, 6, mWhite, mBlack, mWhite, mBlack)
--отработка выхода из сигналов
if signal[i] ~= 0 then
if math.abs(signal[i]) == 3 then
if signal[i] > 0 then
table_text = "Пропуск закрытия позиции LONG (nomorningtime) по "..tikers[i].." по цене "..tLast.param_image
else
table_text = "Пропуск закрытия позиции SHORT (nomorningtime) по "..tikers[i].." по цене "..tLast.param_image
end
textfinres = "Ближайший сигнал по "..tikers[i].." будет отработан на вход"
end
if signal[i] == 1 then
table_text = "закрытие позиции LONG по "..tikers[i].." по цене "..tLast.param_image
sellpos[i] = tonumber(tLast.param_value)
finres = (sellpos[i] - buypos[i]) * lots[i].param_value
textfinres = tikers[i]..".LONG: покупка по: "..buypos[i].." продажа по: "..sellpos[i].." мин.лот = "..lots[i].param_image.." результат = "..mprint(finres).." руб."
end
if signal[i] == -1 then
table_text = "закрытие позиции SHORT по "..tikers[i].." по цене "..tLast.param_image
buypos[i] = tonumber(tLast.param_value)
finres = (sellpos[i] - buypos[i]) * lots[i].param_value
textfinres = tikers[i]..".SHORT: продажа по: "..sellpos[i].." покупка по: "..buypos[i].." мин.лот = "..lots[i].param_image.." результат = "..mprint(finres).." руб."
end
open_file()
message(progname..table_text)
table_print(time3.." "..table_text, table_result_ind)
table_result_ind = table_result_ind + 1
print_file(time3.." "..table_text)
if math.abs(signal[i]) ~= 2 then
message(progname..textfinres)
table_print(textfinres, table_result_ind)
table_result_ind = table_result_ind + 1
print_file(textfinres)
end
close_file()
signal[i] = 0
end
SetCell(m_t, i, 7, " ")
end
Highlight(m_t, i, QTABLE_NO_INDEX, mGray, mBlack, 500) -- выделение цветом вносимых изменений
else
if discreport == 0 then
textdisc = " разрыв связи с сервером"
open_file()
message(progname..textdisc)
table_print(time3..textdisc, table_result_ind)
table_result_ind = table_result_ind + 1
print_file(time3..textdisc)
discreport = 1
close_file()
sleep(100)
end
end
if startind == 0 and i == #tikers then startind = 1 end -- убираем индекс первой итерации
sleep(100)
end
--остановка по таймеру
local time2 = hhmmss(os.sysdate())
if time2>= finishtime then
open_file()
table_text = "скрипт остановлен по таймеру: "..time2
message(progname..table_text)
print_file(table_text)
SetCell(table_result, table_result_ind, 1, table_text)
close_file()
OnStop()
end
if IsWindowClosed(m_t) then OnStop() end
sleep(timeout)
end
end