-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsimple advisor v1_2_table1.lua
More file actions
254 lines (177 loc) · 8.18 KB
/
simple advisor v1_2_table1.lua
File metadata and controls
254 lines (177 loc) · 8.18 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
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.2 : "
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
nonewsignalstime = 174500
finishtime = 175503
--nomorningtime = 103000
end
function OnStop()
DestroyTable(m_t)
do_it = false
message(progname.." Финиш.")
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)
SetCell(table_result, table_result_ind, 1, table_text)
if table_result_ind == 1 then
SetColor(table_result, 10, QTABLE_NO_INDEX, mWhite, mBlack, mWhite, mBlack)
SetColor(table_result, table_result_ind, QTABLE_NO_INDEX, mBlue, mBlack, mBlue, mBlack)
else
SetColor(table_result, table_result_ind - 1, QTABLE_NO_INDEX, mWhite, mBlack, mWhite, mBlack)
SetColor(table_result, table_result_ind, QTABLE_NO_INDEX, mBlue, mBlack, mBlue, mBlack)
end
end
function main()
message(progname.." Старт.")
message(progname.." starttime = "..starttime)
message(progname.." nonewsignalstime = "..nonewsignalstime)
message(progname.." finishtime = "..finishtime)
-- обработка starttime
while hhmmss(os.sysdate())<starttime do
local time1 = hhmmss(os.sysdate())
message('Ожидаем время старта скрипта : '..starttime..", текущее время: "..time1)
sleep(timeout2)
end
do_it = true
size_table = 10 -- количество строк таблицы
table_result_ind = 1 -- индекс строки
if table_result==nil then
table_result = AllocTable()
AddColumn(table_result, 1, "Сигналы и результаты:", true, QTABLE_STRING_TYPE, 70)
CreateWindow(table_result)
SetWindowPos(table_result,0,430,500,300)
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, 23)
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 = {}
for x = 1, #tikers do
closeprice[x] = getParamEx("TQBR", tikers[x], "PREVLEGALCLOSEPR") -- цена закрытия предыдущего дня
signal[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())
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 time3 < nonewsignalstime then
SetCell(m_t, i, 7, "SHORT")
table_text = "сигнал SHORT по "..tikers[i].." по цене "..tLast.param_image
signal[i] = -1
else
SetCell(m_t, i, 7, "no signal SHORT")
table_text ="пропускаем сигнал на SHORT по таймеру "..nonewsignalstime.." по "..tikers[i].." по цене "..tLast.param_image
signal[i] = -2
end
message(progname..table_text)
table_print(table_text, table_result_ind)
table_result_ind = table_result_ind + 1
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 time3 < nonewsignalstime then
SetCell(m_t, i, 7, "LONG")
table_text = "сигнал LONG по "..tikers[i].." по цене "..tLast.param_image
signal[i] = 1
else
SetCell(m_t, i, 7, "no signal LONG")
table_text = "пропускаем сигнал LONG по таймеру "..nonewsignalstime.." по "..tikers[i].." по цене "..tLast.param_image
signal[i] = 2
end
message(progname..table_text)
table_print(table_text, table_result_ind)
table_result_ind = table_result_ind + 1
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 signal[i] == 1 then
table_text = "закрытие позиции LONG по "..tikers[i].." по цене "..tLast.param_image
end
if signal[i] == -1 then
table_text = "закрытие позиции SHORT по "..tikers[i].." по цене "..tLast.param_image
end
signal[i] = 0
message(progname..table_text)
table_print(table_text, table_result_ind)
table_result_ind = table_result_ind + 1
end
SetCell(m_t, i, 7, " ")
end
if table_result_ind == 11 then table_result_ind = 1 end
Highlight(m_t, i, QTABLE_NO_INDEX, mGray, mBlack, 500) -- выделение цветом вносимых изменений
if startind == 0 and i == #tikers then startind = 1 end -- убираем индекс первой итерации
sleep(100)
end
--остановка по таймеру
local time2 = hhmmss(os.sysdate())
if time2>= finishtime then
table_text = "скрипт остановлен по таймеру: "..time2
message(progname..table_text)
SetCell(table_result, table_result_ind, 1, table_text)
OnStop()
end
if IsWindowClosed(m_t) then OnStop() end
sleep(timeout)
end
end