forked from nick-nh/qlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regRangeBar.lua
466 lines (389 loc) · 12.8 KB
/
regRangeBar.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
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
-- Glukk Inc ©
-- индикатор, выводящий диапазоны проторговки
--logfile=io.open(getWorkingFolder().."\\LuaIndicators\\regRangeBar.txt", "w")
min_price_step = 0
Settings=
{
Name = "*regRangeBar",
bars = 27,
ratioFactor = 0.7,
kstd = 1.8,
ATR_factor = 3.1,
line =
{
{
Name = "upRange",
Color = RGB(89, 213, 107),
Type = TYPET_BAR, --TYPE_DASHDOT,
Width = 2
},
{
Name = "downRange",
Color = RGB(251,82,0),
Type = TYPET_BAR,
Width = 2
},
{
Name = "signal",
Color = RGB(128,64,64),
Type = TYPE_POINT,
Width = 3
}
}
}
function Init()
myFunc = rangeBar()
return #Settings.line
end
function OnCalculate(index)
if index == 1 then
DSInfo = getDataSourceInfo()
min_price_step = getParamEx(DSInfo.class_code, DSInfo.sec_code, "SEC_PRICE_STEP").param_value
if tonumber(min_price_step) == 0 or min_price_step == nil then
min_price_step = 1
end
end
return myFunc(index, Settings)
end
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
function FindExistCandle(I)
local out = I
while not CandleExist(out) and out > 0 do
out = out -1
end
return out
end
function dValue(i,param)
local v = param or "C"
if not CandleExist(i) then
return nil
end
if v == "O" then
return O(i)
elseif v == "H" then
return H(i)
elseif v == "L" then
return L(i)
elseif v == "C" then
return C(i)
elseif v == "V" then
return V(i)
elseif v == "M" then
return (H(i) + L(i))/2
elseif v == "T" then
return (H(i) + L(i)+C(i))/3
elseif v == "W" then
return (H(i) + L(i)+2*C(i))/4
elseif v == "ATR" then
local previous = i-1
if not CandleExist(previous) then
previous = FindExistCandle(previous)
end
if H(i)==nil or L(i) == nil or C(previous) == nil then return 0; end
return math.max(math.abs(H(i) - L(i)), math.abs(H(i) - C(previous)), math.abs(C(previous) - L(i)))
else
return C(i)
end
end
function rangeBar()
local upRange={}
local dwnRange={}
local cacheL={}
local cacheH={}
local cacheC={}
local sx={}
local ATR={}
local calculated_buffer={}
local prevRangeStart = {}
local rangeStart = {}
local lastRange = {}
local lastSignal = {}
return function(ind, Fsettings, ds)
local Fsettings=(Fsettings or {})
local index = ind
local bars = Fsettings.bars or 64
local ratioFactor = Fsettings.ratioFactor or 3
local kstd = Fsettings.kstd or 1
local ATR_factor = Fsettings.ATR_factor or 3
local degree = 1
local periodATR = bars
local out1 = nil
local out2 = nil
local out3 = nil
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 mi = 0
local ai={{1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}}
local b={}
local x={}
local index = ind-1
index = math.max(index, 1)
p = bars
nn = degree+1
if index == 1 then
upRange = {}
upRange[index] = 0
dwnRange = {}
dwnRange[index] = 0
cacheL = {}
cacheL[index] = 0
cacheH = {}
cacheH[index] = 0
cacheC = {}
cacheC[index] = 0
rangeStart = {}
rangeStart[index] = nil
prevRangeStart = {}
prevRangeStart[index] = nil
ATR = {}
ATR[index] = 0
calculated_buffer = {}
lastRange = {}
lastRange[index] = {0, 0}
lastSignal = {}
lastSignal[index] = {index, 0, nil}
--- sx
sx={}
sx[1] = p+1
for mi=1, nn*2-2 do
sum=0
for n=1, p do
sum = sum + math.pow(n,mi)
end
sx[mi+1]=sum
end
return nil
end
if calculated_buffer[index] == nil then
upRange[index] = upRange[index-1]
dwnRange[index] = dwnRange[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]
lastSignal[index] = lastSignal[index-1]
end
ATR[index] = ATR[index-1]
if not CandleExist(index) then
return nil
end
if index<periodATR then
ATR[index] = 0
elseif index==periodATR then
local sum=0
for i = 1, periodATR do
if CandleExist(i) then
sum = sum + dValue(i, 'ATR')
end
end
ATR[index]=sum / periodATR
elseif index>periodATR then
ATR[index]=(ATR[index-1] * (periodATR-1) + dValue(index, 'ATR')) / periodATR
--ATR[index] = kawg*dValue(index)+(1-kawg)*ATR[index-1]
end
if index <= Size()-500 then
return nil
end
cacheH[index] = H(index)
cacheL[index] = L(index)
cacheC[index] = C(index)
if calculated_buffer[index] == nil then
--WriteLog ("---------------------------------")
--WriteLog ("OnCalc() ".."CandleExist("..index.."): "..tostring(CandleExist(index)).."; T("..index.."); "..isnil(toYYYYMMDDHHMMSS(T(index))," - "))
--WriteLog ("C(index) "..tostring(C(index)).." H(index) "..tostring(H(index)).." L(index) "..tostring(L(index)))
previous = rangeStart[index] or index-bars
if not CandleExist(previous) 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 fx_buffer={}
--- syx
for mi = 1, nn do
sum = 0
for n=0, p do
if CandleExist(index+n-bars) then
if mi==1 then
sum = sum + C(index+n-bars)
else
sum = sum + 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
calculated_buffer[index] = true
-- Std
sq=0.0
for n = 1, p do
if CandleExist(index+n-bars) then
sq = sq + math.pow(C(index+n-bars)-fx_buffer[n],2)
end
end
sq = math.sqrt(sq/(p-1))*kstd
lastSignal[index][3] = nil
local deltaRatio = math.abs(fx_buffer[#fx_buffer]-fx_buffer[1])*100/fx_buffer[1]
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*ATR[index]) or ATR_factor == 0)
then
if rangeStart[index] == nil then
if prevRangeStart[index]~=nil then
if previous - prevRangeStart[index] < bars then
if lastSignal[index][1] > previous and lastSignal[index][2]~=0 then
SetValue(lastSignal[index][1], 3, nil)
lastSignal[lastSignal[index][1]][3] = nil
end
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
out1 = maxC
out2 = minC
lastRange[index] = {out1, out2}
for i=rangeStart[index],index do
SetValue(i, 1, out1)
SetValue(i, 2, out2)
end
lastSignal[index] = {index, 0, nil}
else
if rangeStart[index] ~=nil then
prevRangeStart[index] = rangeStart[index]
end
rangeStart[index] = nil
end
if lastRange[index]~=nil then
if (cacheC[index-1] > lastRange[index][1] and cacheC[index-2] <= lastRange[index][1] and lastSignal[index][2]~=1) then
lastSignal[index] = {index, 1, O(index)}
SetValue(lastSignal[index][1], 3, lastSignal[index][3])
end
if (cacheC[index-1] < lastRange[index][2] and cacheC[index-2] >= lastRange[index][2] and lastSignal[index][2]~=-1) then
lastSignal[index] = {index, -1, O(index)}
SetValue(lastSignal[index][1], 3, lastSignal[index][3])
end
end
end
return nil
end
end
function WriteLog(text)
logfile:write(tostring(os.date("%c",os.time())).." "..text.."\n");
logfile:flush();
LASTLOGSTRING = text;
end;
function round(num, idp)
if idp and num then
local mult = 10^(idp or 0)
if num >= 0 then return math.floor(num * mult + 0.5) / mult
else return math.ceil(num * mult - 0.5) / mult end
else return num end
end
function toYYYYMMDDHHMMSS(datetime)
if type(datetime) ~= "table" then
return ""
else
local Res = tostring(datetime.year)
if #Res == 1 then Res = "000"..Res end
local month = tostring(datetime.month)
if #month == 1 then Res = Res.."/0"..month; else Res = Res..'/'..month; end
local day = tostring(datetime.day)
if #day == 1 then Res = Res.."/0"..day; else Res = Res..'/'..day; end
local hour = tostring(datetime.hour)
if #hour == 1 then Res = Res.." 0"..hour; else Res = Res..' '..hour; end
local minute = tostring(datetime.min)
if #minute == 1 then Res = Res..":0"..minute; else Res = Res..':'..minute; end
local sec = tostring(datetime.sec);
if #sec == 1 then Res = Res..":0"..sec; else Res = Res..':'..sec; end;
return Res
end
end --toYYYYMMDDHHMMSS
function isnil(a,b)
if a == nil then
return b
else
return a
end;
end