-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathmonitorRange.lua
More file actions
418 lines (354 loc) · 15.4 KB
/
Copy pathmonitorRange.lua
File metadata and controls
418 lines (354 loc) · 15.4 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
rangeSettings = {
bars = 27,
ratioFactor = 0.7,
kstd = 1.8,
ATR_factor = 3.1,
Size = 700
}
range_PARAMS_FILE_NAME = getScriptPath().."\\rangeMonitor.csv" -- ИМЯ ЛОГ-ФАЙЛА
rangeParamsFile = nil
range_SEC_CODES = nil
function initRangeBar()
if rangeParamsFile == nil then
rangeParamsFile = io.open(range_PARAMS_FILE_NAME,"r")
if rangeParamsFile ~= nil then
local lineCount = 0
range_SEC_CODES = {}
range_SEC_CODES['sec_codes'] = {} --
range_SEC_CODES['ChartId'] = {} -- ChartId
range_SEC_CODES['interval'] = {} --
range_SEC_CODES['isLong'] = {} --
range_SEC_CODES['isShort'] = {} --
range_SEC_CODES['bars'] = {} -- bars
range_SEC_CODES['ratioFactor'] = {} -- ratioFactor
range_SEC_CODES['kstd'] = {} -- kstd
range_SEC_CODES['ATR_factor'] = {} -- ATR_factor
for line in rangeParamsFile:lines() do
lineCount = lineCount + 1
if lineCount > 1 and line ~= "" then
local per1, per2, per3, per4, per5, per6, per7, per8, per9 = line:match("%s*(.*);%s*(.*);%s*(.*);%s*(.*);%s*(.*);%s*(.*);%s*(.*);%s*(.*);%s*(.*)")
range_SEC_CODES['sec_codes'][lineCount-1] = per1
range_SEC_CODES['ChartId'][lineCount-1] = per2
range_SEC_CODES['interval'][lineCount-1] = tonumber(per3)
range_SEC_CODES['isLong'][lineCount-1] = tonumber(per4)
range_SEC_CODES['isShort'][lineCount-1] = tonumber(per5)
range_SEC_CODES['bars'][lineCount-1] = tonumber(per6)
range_SEC_CODES['ratioFactor'][lineCount-1] = tonumber(per7)
range_SEC_CODES['kstd'][lineCount-1] = tonumber(per8)
range_SEC_CODES['ATR_factor'][lineCount-1] = tonumber(per9)
end
end
rangeParamsFile:close()
end
end
calcAlgoValue = nil
cacheL={}
cacheH={}
cacheC={}
rangeStart = {}
prevRangeStart = {}
lastRange = {}
rATR = {}
end
function findRangeSettings(iSec, interval, _settings)
if range_SEC_CODES ~= nil then
for i,_SEC_CODE in ipairs(range_SEC_CODES['sec_codes']) do
--myLog('interval '..tostring(interval)..'_SEC_CODE '..tostring(_SEC_CODE)..' bars '..tostring(range_SEC_CODES['bars'][i]))
if SEC_CODES['sec_codes'][iSec] == _SEC_CODE and interval == range_SEC_CODES['interval'][i] then
settings = {}
settings.bars = range_SEC_CODES['bars'][i]
settings.ratioFactor = range_SEC_CODES['ratioFactor'][i]
settings.kstd = range_SEC_CODES['kstd'][i]
settings.ATR_factor = range_SEC_CODES['ATR_factor'][i]
settings.ChartId = range_SEC_CODES['ChartId'][i]
settings.isLong = range_SEC_CODES['isLong'][i]
settings.isShort = range_SEC_CODES['isShort'][i]
return settings
end
end
end
return _settings
end
function rangeBar(iSec, ind, _settings, DS, interval)
--local interval = 0
--if ind > 1 and DS:T(ind) ~= nil and DS:T(ind-1) ~= nil then
-- myLog('ind '..tostring(ind)..'DS:T(ind) '..tostring(DS:T(ind).hour)..' DS:T(ind-1) '..tostring(DS:T(ind-1).hour).." delta "..tostring(os.time(DS:T(ind)) - os.time(DS:T(ind-1))))
-- interval = (os.time(DS:T(ind)) - os.time(DS:T(ind-1)))/60
--end
--myLog(SEC_CODES['sec_codes'][iSec].." bars "..tostring(_settings.bars).." ratioFactor "..tostring(_settings.ratioFactor).." kstd "..tostring(_settings.kstd))
local settings = findRangeSettings(iSec, interval, _settings)
local Size = settings.Size or 1000
local bars = settings.bars or 27
local ratioFactor = settings.ratioFactor or 3
local kstd = settings.kstd or 1.8
local ATR_factor = settings.ATR_factor or 3
local degree = 1
--myLog(SEC_CODES['sec_codes'][iSec].." bars "..tostring(bars).." ratioFactor "..tostring(ratioFactor).." kstd "..tostring(kstd))
if ind == nil then ind = DS:Size() end
Size = math.min(Size, DS:Size()) - 2
local p = 0
local n = 0
local f = 0
local qq = 0
local mm = 0
local tt = 0
local ii = 0
local jj = 0
local kk = 0
local ll = 0
local nn = 0
local sq = 0
local i0 = 0
local mi = 0
local ai={{1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}}
local b={}
local x={}
p = bars
nn = degree+1
local periodATR = bars
local index = ind-Size
calcAlgoValue = {}
calcAlgoValue[index] = 0
cacheL = {}
cacheL[index] = 0
cacheH = {}
cacheH[index] = 0
cacheC = {}
cacheC[index] = 0
rangeStart = {}
rangeStart[index] = nil
prevRangeStart = {}
prevRangeStart[index] = nil
rATR = {}
rATR[index] = 0
lastRange = {}
lastRange[index] = {0, 0, 0, 0}
--- sx
sx={}
sx[1] = p+1
for mi=1, nn*2-2 do
sum=0
for n=i0, i0+p do
sum = sum + math.pow(n,mi)
end
sx[mi+1]=sum
end
for index = ind-Size+1, DS:Size() do
calcAlgoValue[index] = calcAlgoValue[index-1]
cacheL[index] = cacheL[index-1]
cacheH[index] = cacheH[index-1]
cacheC[index] = cacheC[index-1]
rangeStart[index] = rangeStart[index-1]
prevRangeStart[index] = prevRangeStart[index-1]
lastRange[index] = lastRange[index-1]
rATR[index] = rATR[index-1]
if DS:C(index) ~= nil then
if index - (ind-Size) > bars then
cacheH[index] = DS:H(index)
cacheL[index] = DS:L(index)
cacheC[index] = DS:C(index)
if index<periodATR then
rATR[index] = 0
elseif index==periodATR then
local sum=0
for i = 1, periodATR do
if DS:C(i) ~= nil then
sum = sum + dValue(i)
end
end
rATR[index]=sum / periodATR
elseif index>periodATR then
rATR[index]=(rATR[index-1] * (periodATR-1) + dValue(index)) / periodATR
end
local fx_buffer={}
--- syx
for mi = 1, nn do
sum = 0
for n=0, p do
if DS:C(index+n-bars)~=nil then
if mi==1 then
sum = sum + DS:C(index+n-bars)
else
sum = sum + DS:C(index+n-bars)*math.pow(n,mi-1)
end
end
end
b[mi]=sum
end
--- Matrix
for jj=1, nn do
for ii=1, nn do
kk=ii+jj-1
ai[ii][jj]=sx[kk]
end
end
--- Gauss
for kk=1, nn-1 do
ll=0
mm=0
for ii=kk, nn do
if math.abs(ai[ii][kk])>mm then
mm=math.abs(ai[ii][kk])
ll=ii
end
end
if ll==0 then
return nil
end
if ll~=kk then
for jj=1, nn do
tt=ai[kk][jj]
ai[kk][jj]=ai[ll][jj]
ai[ll][jj]=tt
end
tt=b[kk]
b[kk]=b[ll]
b[ll]=tt
end
for ii=kk+1, nn do
qq=ai[ii][kk]/ai[kk][kk]
for jj=1, nn do
if jj==kk then
ai[ii][jj]=0
else
ai[ii][jj]=ai[ii][jj]-qq*ai[kk][jj]
end
end
b[ii]=b[ii]-qq*b[kk]
end
end
x[nn]=b[nn]/ai[nn][nn]
for ii=nn-1, 1, -1 do
tt=0
for jj=1, nn-ii do
tt=tt+ai[ii][ii+jj]*x[ii+jj]
x[ii]=(1/ai[ii][ii])*(b[ii]-tt)
end
end
---
for n = 1, p do
sum=0
for kk=1, degree do
sum = sum + x[kk+1]*math.pow(n,kk)
end
fx_buffer[n]=x[1]+sum
end
-- Std
sq=0.0
for n = 1, p do
if DS:C(index+n-bars)~=nil then
sq = sq + math.pow(DS:C(index+n-bars)-fx_buffer[n],2)
end
end
sq = math.sqrt(sq/(p-1))*kstd
local scale = getSecurityInfo(CLASS_CODE, SEC_CODE).scale
calcAlgoValue[index] = round(fx_buffer[#fx_buffer], scale)
previous = rangeStart[index] or index-bars
if DS:C(previous) == nil then
previous = FindExistCandle(previous)
end
local maxC = cacheC[math.max(previous, 1)]
local minC = cacheC[math.max(previous, 1)]
for i=math.max(previous, 1)+1,index-1 do
maxC = math.max(cacheC[i], maxC)
minC = math.min(cacheC[i], minC)
end
local deltaRatio = math.abs(fx_buffer[#fx_buffer]-fx_buffer[1])*100/fx_buffer[1]
--if deltaRatio < ratioFactor and math.abs(DS:C(index) - fx_buffer[#fx_buffer]) < sq then
--if deltaRatio < ratioFactor and fx_buffer[#fx_buffer] < maxC and fx_buffer[#fx_buffer] > minC and math.abs(maxC-minC) < 2*sq then
if deltaRatio < ratioFactor and fx_buffer[#fx_buffer] < maxC and
fx_buffer[#fx_buffer] > minC and math.abs(maxC-minC) < 2*sq and
((ATR_factor~=0 and math.abs(maxC-minC) <= ATR_factor*rATR[index]) or ATR_factor == 0)
then
--lastRange[index] = {maxC, minC, previous, index}
--if rangeStart[index] == nil then
-- rangeStart[index] = previous
--end
--if rangeStart[index] == nil then
-- if prevRangeStart[index]~=nil then
-- if previous - prevRangeStart[index] < bars then
-- previous = prevRangeStart[index]
-- maxC = math.max(unpack(cacheC,math.max(previous, 1),index-1))
-- minC = math.min(unpack(cacheC,math.max(previous, 1),index-1))
-- end
-- end
-- rangeStart[index] = previous
--end
if rangeStart[index] == nil then
if prevRangeStart[index]~=nil then
if previous - prevRangeStart[index] < bars then
previous = prevRangeStart[index]
maxC = cacheC[math.max(previous, 1)]
minC = cacheC[math.max(previous, 1)]
for i=math.max(previous, 1)+1,index-1 do
maxC = math.max(cacheC[i], maxC)
minC = math.min(cacheC[i], minC)
end
end
end
rangeStart[index] = previous
end
lastRange[index] = {maxC, minC, previous, index}
else
if rangeStart[index] ~=nil then
prevRangeStart[index] = rangeStart[index]
end
rangeStart[index] = nil
end
end
end
end
return calcAlgoValue
end
function rangeTest(i, cell, settings, DS, signal)
local index = DS:Size()
local scale = getSecurityInfo(CLASS_CODE, SEC_CODE).scale
if calcAlgoValue[DS:Size()] == nil or DS:Size() == 0 then return end
local testvalue = GetCell(t_id, i, tableIndex["Текущая цена"]).value
local calcValMax = round(lastRange[index][1] or 0, scale)
local calcValMin = round(lastRange[index][2] or 0, scale)
if INTERVALS["visible"][cell] then
local Color = RGB(255, 255, 255)
local returnValue = round(calcAlgoValue[DS:Size()] or 0, scale)
if testvalue<calcValMin then
returnValue = calcValMin
Color = RGB(255,168,164)
end
if testvalue>calcValMax then
returnValue = calcValMax
Color = RGB(165,227,128)
end
SetCell(t_id, i, tableIndex[cell], tostring(returnValue), returnValue)
cellSetColor(i, tableIndex[cell], Color, RGB(0,0,0))
end
if signal then
local isMessage = SEC_CODES['isMessage'][i]
local isPlaySound = SEC_CODES['isPlaySound'][i]
local mes0 = tostring(SEC_CODES['names'][i]).." timescale "..INTERVALS["names"][cell]
local mes = ""
if DS:C(index-1) < lastRange[index][2] and DS:C(index-2) > lastRange[index][2] and DS:C(index) <= lastRange[index][2] then
mes = mes0..": выход из диапазона вниз"
myLog(mes)
if isMessage == 1 then message(mes) end
if isPlaySound == 1 then PaySoundFile(soundFileName) end
end
if DS:C(index-1) > lastRange[index][2] and DS:C(index-2) < lastRange[index][2] and DS:C(index) >= lastRange[index][2]then
mes = mes0..": возврат в диапазон снизу"
myLog(mes)
if isMessage == 1 then message(mes) end
if isPlaySound == 1 then PaySoundFile(soundFileName) end
end
if DS:C(index-1) > lastRange[index][1] and DS:C(index-2) < lastRange[index][1] and DS:C(index) >= lastRange[index][1] then
mes = mes0..": выход из диапазона вверх"
myLog(mes)
if isMessage == 1 then message(mes) end
if isPlaySound == 1 then PaySoundFile(soundFileName) end
end
if DS:C(index-1) < lastRange[index][1] and DS:C(index-2) > lastRange[index][1] and DS:C(index) <= lastRange[index][1] then
mes = mes0..": возврат в диапазон сверху"
myLog(mes)
if isMessage == 1 then message(mes) end
if isPlaySound == 1 then PaySoundFile(soundFileName) end
end
end
end