-
Notifications
You must be signed in to change notification settings - Fork 64
/
rangeFlatHV.lua
588 lines (480 loc) · 22.5 KB
/
rangeFlatHV.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
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
--https://github.com/nick-nh/qlua
--logfile=io.open(getWorkingFolder().."\\LuaIndicators\\rangeHV.txt", "w")
local min_price_step = 0
local DSInfo
local scale = 0
local myFunc = function() end
math.pow = math.pow or function(x, y) return x^y end
Settings=
{
Name = "*rangeFlatHV",
period = 60,
flat_bars = 27,
kstd = 1.5,
ratioFactor = 0.3,
sqFactor = 2,
bars = 1000,
clasters = 100,
showMaxVol = 0,
showVWAP = 0,
showEMAVWAP = 1,
emaPeriod = 12,
line =
{
{
Name = "maxVol",
Color = RGB(127, 127, 127),
Type = TYPET_BAR, --TYPE_DASHDOT,
Width = 3
},
{
Name = "VWAP",
Color = RGB(64, 64, 64),
Type = TYPET_LINE, --TYPE_DASHDOT,
Width = 1
},
{
Name = "EMAVWAP",
Color = RGB(64, 64, 64),
Type = TYPET_LINE, --TYPE_DASHDOT,
Width = 1
},
{
Name = "rangeVWAPMax",
Color = RGB(89, 213, 107),
Type = TYPET_BAR,
Width = 2
},
{
Name = "rangeVWAPMin",
Color = RGB(251,82,0),
Type = TYPET_BAR,
Width = 2
}
}
}
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
local function isnil(a,b)
if a == nil then
return b
else
return a
end;
end
local function toYYYYMMDDHHMMSS(datetime)
if type(datetime) ~= "table" then
--message("в функции toYYYYMMDDHHMMSS неверно задан параметр: datetime="..tostring(datetime))
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
local function WriteLog(text)
if not logfile then return end
logfile:write(tostring(os.date("%c",os.time())).." "..text.."\n");
logfile:flush();
LASTLOGSTRING = text;
end
local function FindExistCandle(I)
local out = I
while not CandleExist(out) and out > 0 do
out = out -1
end
return out
end
local 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 previous ==0 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 maxPrice = {}
local VWAP = {}
local sx = {}
local prevRangeStart = {}
local rangeStart = {}
local lastRange = {}
local Close = {}
local Open = {}
local High = {}
local Low = {}
local CC = {}
local calculated_buffer = {}
local calcAlgoValue = {}
local trend = {}
local vEMA = {}
local outMaxRange = 0
local outMinRange = 0
return function(ind, Fsettings)
local Fsettings=(Fsettings or {})
local index = ind
local periodHV = Fsettings.period or 59
local flat_bars = Fsettings.flat_bars or 27
local kstd = Fsettings.kstd or 1
local ratioFactor = Fsettings.ratioFactor or 3
local sqFactor = Fsettings.sqFactor or 3
local bars = Fsettings.bars or 1000
local clasters = Fsettings.clasters or 50
if bars == 0 then bars = Size()-100 end
local showMaxVol = Fsettings.showMaxVol or 0
local showVWAP = Fsettings.showVWAP or 0
local showEMAVWAP = Fsettings.showEMAVWAP or 0
local emaPeriod = Fsettings.emaPeriod or periodHV
local kvEMA = 2/(emaPeriod+1)
local MAX = 0
local MAXV = 0
local MIN = 0
local outMaxPrice
local outVWAP
local outEMAVWAP
local degree = 1
local p = 0
local qq = 0
local mm = 0
local tt = 0
local jj = 0
local kk = 0
local ll = 0
local nn = 0
local sq = 0
local ai={{1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}}
local b={}
local x={}
p = flat_bars
nn = degree+1
if index == 1 then
maxPrice = {}
maxPrice[index] = 0
VWAP = {}
VWAP[index] = 0
Close = {}
Close[index] = C(index) or 0
Open = {}
Open[index] = O(index) or 0
High = {}
High[index] = H(index) or 0
Low = {}
Low[index] = L(index) or 0
calculated_buffer = {}
trend = {}
trend[index] = 1
calcAlgoValue = {}
calcAlgoValue[index] = C(index)
if showEMAVWAP == 1 then
vEMA = {}
vEMA[index] = C(index) or 0
end
rangeStart = {}
rangeStart[index] = nil
prevRangeStart = {}
prevRangeStart[index] = nil
calculated_buffer = {}
lastRange = {}
lastRange[index] = {0, 0}
--- 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
maxPrice[index] = maxPrice[index-1]
VWAP[index] = VWAP[index-1]
High[index] = High[index-1]
Low[index] = Low[index-1]
Close[index] = Close[index-1]
calcAlgoValue[index] = calcAlgoValue[index-1]
trend[index] = trend[index-1]
if showEMAVWAP == 1 then
vEMA[index] = vEMA[index-1]
end
rangeStart[index] = rangeStart[index-1]
prevRangeStart[index] = prevRangeStart[index-1]
lastRange[index] = lastRange[index-1]
end
if not CandleExist(index) then
return nil
end
local beginIndex = math.max(Size() - bars, periodHV, emaPeriod, flat_bars)
if index == beginIndex then
maxPrice[index] = C(index)
VWAP[index] = C(index)
calcAlgoValue[index] = C(index)
if showEMAVWAP == 1 then
vEMA[index] = C(index)
end
end
Close[index] = C(index)
Open[index] = O(index)
High[index] = H(index)
Low[index] = L(index)
if index < beginIndex then -- - flat_bars
return nil
end
if showMaxVol == 1 then
outMaxPrice = maxPrice[index]
end
if showVWAP == 1 then
outVWAP = VWAP[index]
end
if showEMAVWAP == 1 then
outEMAVWAP = vEMA[index]
end
if calculated_buffer[index]~=nil then
return outMaxPrice, outVWAP, outEMAVWAP, outMaxRange, outMinRange
end
local previous = index-periodHV + 1
local _p = periodHV
if C(previous) == nil then
previous = FindExistCandle(previous)
end
MAX = High[math.max(previous, 1)]
MIN = Low[math.max(previous, 1)]
for i=math.max(previous, 1)+1,index do
MAX = math.max(High[i], MAX)
MIN = math.min(Low[i], MIN)
end
WriteLog('index '..tostring(index)..', MAX '..tostring(MAX)..', MIN '..tostring(MIN))
for i = 1, clasters do CC[i]={0, (i-1)*(MAX-MIN)/(clasters-1)+MIN} end
VWAP[index] = 0
local allVolume = 0
for i = 0, _p-1 do
if C(index-i) ~= nil then
jj=math.floor((H(index-i)-MIN)*(clasters-1)/(MAX-MIN))+1
kk=math.floor((L(index-i)-MIN)*(clasters-1)/(MAX-MIN))+1
--WriteLog(' -- index '..tostring(index-i)..', H '..tostring(H(index-i))..', L '..tostring(L(index-i))..', jj '..tostring(jj)..', kk '..tostring(kk))
for k=1,(jj-kk+1) do
----WriteLog(' ----- k '..tostring(k)..', CC[kk+k-1] '..tostring(CC[kk+k-1]))
CC[kk+k-1][1]=CC[kk+k-1][1]+V(index-i)/(jj-kk+1)
VWAP[index] = VWAP[index] + CC[kk+k-1][2]*V(index-i)/(jj-kk+1)
allVolume = allVolume + V(index-i)/(jj-kk+1)
end
--WriteLog(' ----- V(index-i) '..tostring(V(index-i))..', allVolume '..tostring(allVolume))
end
end
--WriteLog('index: '..tostring(index)..' '..toYYYYMMDDHHMMSS(T(index))..', VWAP: '..tostring(VWAP[index])..', allVolume: '..tostring(allVolume))
VWAP[index] = allVolume == 0 and VWAP[index-1] or VWAP[index]/allVolume
if showEMAVWAP == 1 then
vEMA[index]=round(kvEMA*VWAP[index]+(1-kvEMA)*vEMA[index-1], 5)
end
for i = 1, clasters do
MAXV = math.max(MAXV, CC[i][1])
if MAXV == CC[i][1] then
maxPrice[index]=CC[i][2]
end
end
-- range max price
previous = rangeStart[index] or index-flat_bars
if not CandleExist(previous) then
previous = FindExistCandle(previous)
end
local maxRange = maxPrice[math.max(previous, 1)]
local minRange = maxPrice[math.max(previous, 1)]
----WriteLog('index: '..tostring(index)..', previous: '..tostring(previous)..', maxPrice prev: '..tostring(maxPrice[math.max(previous, 1)])..', maxPrice: '..tostring(maxPrice[index]))
for i=math.max(previous, 1)+1,index-1 do
maxRange = math.max(maxPrice[i], maxRange)
minRange = math.min(maxPrice[i], minRange)
end
----WriteLog('index: '..tostring(index)..', rangeStart: '..tostring(rangeStart[index])..', maxRange: '..tostring(maxRange)..', minRange: '..tostring(minRange))
if index < beginIndex then
return nil
end
local fx_buffer={}
--- syx
for mi = 1, nn do
sum = 0
for n=0, p do
if CandleExist(index+n-flat_bars) then
if mi==1 then
sum = sum + maxPrice[index+n-flat_bars]
else
sum = sum + maxPrice[index+n-flat_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 CandleExist(index+n-flat_bars) then
sq = sq + math.pow(maxPrice[index+n-flat_bars]-fx_buffer[n],2)
end
end
sq = math.sqrt(sq/(p-1))*kstd
local deltaRatio = math.abs(fx_buffer[#fx_buffer]-fx_buffer[1])*100/fx_buffer[1]
----WriteLog('fx_buffer: '..tostring(fx_buffer[#fx_buffer])..', fx_buffer[1]: '..tostring(fx_buffer[1])..', deltaRatio: '..tostring(deltaRatio)..', sq: '..tostring(sq))
if deltaRatio < ratioFactor and math.abs(maxRange-minRange) < sqFactor*sq
then
----WriteLog('deltaRatio < ratioFactor: '..tostring(deltaRatio < ratioFactor))
if rangeStart[index] == nil then
if prevRangeStart[index]~=nil then
if previous - prevRangeStart[index] < flat_bars then
previous = prevRangeStart[index]
maxRange = maxPrice[math.max(previous, 1)]
minRange = maxPrice[math.max(previous, 1)]
for i=math.max(previous, 1)+1,index-1 do
maxRange = math.max(maxPrice[i], maxRange)
minRange = math.min(maxPrice[i], minRange)
end
end
end
rangeStart[index] = previous
end
lastRange[index] = {maxRange, minRange}
for i=rangeStart[index],index do
SetValue(i, 4, maxRange)
SetValue(i, 5, minRange)
end
else
if rangeStart[index] ~=nil then
prevRangeStart[index] = rangeStart[index]
end
rangeStart[index] = nil
outMaxRange = nil
outMinRange = nil
end
-- range max price
calculated_buffer[index] = maxPrice[index]
if showMaxVol == 1 then
outMaxPrice = maxPrice[index]
end
if showVWAP == 1 then
outVWAP = VWAP[index]
end
if showEMAVWAP == 1 then
outEMAVWAP = vEMA[index]
end
--WriteLog('index: '..tostring(index)..', outEMAVWAP: '..tostring(outEMAVWAP))
return outMaxPrice, outVWAP, outEMAVWAP, outMaxRange, outMinRange
end
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 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
scale = getSecurityInfo(DSInfo.class_code, DSInfo.sec_code).scale
end
return myFunc(index, Settings)
end
function OnDestroy()
end