-
Notifications
You must be signed in to change notification settings - Fork 64
/
rangeHV.lua
660 lines (567 loc) · 16.2 KB
/
rangeHV.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
--logfile=io.open(getWorkingFolder().."\\LuaIndicators\\rangeHV.txt", "w")
min_price_step = 0
math.pow = math.pow or function(x, y) return x^y end
Settings=
{
Name = "*rangeHV",
period = 39,
periodSlow = 39,
bars = 1000,
clasters = 100,
showVWAP = 1,
showEMAVWAP = 1,
emaPeriod = 12,
showSigmaVol = 0,
showSigmaCenterLevel = 0,
showTradeSignal = 0,
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 = "maxVolFast",
Color = RGB(0, 128, 192),
Type = TYPET_BAR, --TYPE_DASHDOT,
Width = 3
},
{
Name = "VWAPFast",
Color = RGB(0, 128, 192),
Type = TYPET_LINE, --TYPE_DASHDOT,
Width = 1
},
{
Name = "EMAVWAPFast",
Color = RGB(0, 128, 192),
Type = TYPET_LINE, --TYPE_DASHDOT,
Width = 1
},
{
Name = "buy",
Color = RGB(40,240,250),
Type = TYPE_POINT,
Width = 4
},
{
Name = "sell",
Color = RGB(255,0,255),
Type = TYPE_POINT,
Width = 4
},
{
Name = "UpSigmaVol",
Color = RGB(0, 0, 0),
Type = TYPE_DASHDOT, --TYPE_DASHDOT,
Width = 1
},
{
Name = "DownSigmaVol",
Color = RGB(0, 0, 0),
Type = TYPE_DASHDOT, --TYPE_DASHDOT,
Width = 1
}
}
}
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
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
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 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 maxPricefast={}
local VWAPfast={}
local UpSigmaVol={}
local DownSigmaVol={}
local UpSigmaVolfast={}
local DownSigmaVolfast={}
local Close = {}
local Open = {}
local High = {}
local Low = {}
local CC={}
local CCfast={}
local calculated_buffer={}
local calcAlgoValue={}
local trend={}
local signal={}
local EMA={}
local vEMA={}
local vfEMA={}
return function(ind, Fsettings, ds)
local Fsettings=(Fsettings or {})
local index = ind
local periodHV = Fsettings.period or 59
local periodHV2 = math.max(Fsettings.periodSlow or 150, Fsettings.period)
local maxPeriodHV = math.max(periodHV,periodHV2)
local bars = Fsettings.bars or 1000
local clasters = Fsettings.clasters or 50
if bars == 0 then bars = Size()-100 end
local showVWAP = Fsettings.showVWAP or 1
local showEMAVWAP = Fsettings.showEMAVWAP or 0
local emaPeriod = Fsettings.emaPeriod or period
local showSigmaVol = Fsettings.showSigmaVol or 0
local showSigmaCenterLevel = Fsettings.showSigmaCenterLevel or 0
local showTradeSignal = Fsettings.showTradeSignal or 0
local kvEMA = 2/(emaPeriod+1)
local MAX = 0
local MAXV = 0
local MIN = 0
local MAXfast = 0
local MAXVfast = 0
local MINfast = 0
local jj = 0
local kk = 0
local outMaxPriceFast = nil
local outVWAP = nil
local outVWAPfast = nil
local outEMAVWAP = nil
local outEMAVWAPfast = nil
local outUpSigmaVol = nil
local outDownSigmaVol = nil
if index == 1 then
maxPrice = {}
maxPrice[index] = 0
VWAP = {}
VWAP[index] = 0
if showSigmaVol == 1 or showSigmaCenterLevel == 1 then
UpSigmaVol = {}
UpSigmaVol[index] = 0
DownSigmaVol = {}
DownSigmaVol[index] = 0
UpSigmaVolfast = {}
UpSigmaVolfast[index] = 0
DownSigmaVolfast = {}
DownSigmaVolfast[index] = 0
end
Close = {}
Close[index] = 0
Open = {}
Open[index] = 0
High = {}
High[index] = 0
Low = {}
Low[index] = 0
calculated_buffer = {}
trend = {}
trend[index] = 1
calcAlgoValue = {}
calcAlgoValue[index] = C(index)
signal = {}
signal[index] = {nil,nil}
if periodHV < periodHV2 then
maxPricefast = {}
maxPricefast[index] = 0
CCfast={}
CCfast[1]={0, C(index)}
VWAPfast = {}
VWAPfast[index] = 0
end
if showTradeSignal == 1 or showEMAVWAP == 1 then
vEMA = {}
vEMA[index] = C(index)
vfEMA = {}
vfEMA[index] = C(index)
end
return nil
end
if calculated_buffer[index] == nil then
maxPrice[index] = maxPrice[index-1]
VWAP[index] = VWAP[index-1]
if periodHV < periodHV2 then
maxPricefast[index] = maxPricefast[index-1]
VWAPfast[index] = VWAPfast[index-1]
end
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]
signal[index] = {nil,nil}
if showSigmaVol == 1 or showSigmaCenterLevel == 1 then
UpSigmaVol[index] = UpSigmaVol[index-1]
DownSigmaVol[index] = DownSigmaVol[index-1]
UpSigmaVolfast[index] = UpSigmaVolfast[index-1]
DownSigmaVolfast[index] = DownSigmaVolfast[index-1]
end
if showTradeSignal == 1 or showEMAVWAP == 1 then
vEMA[index] = vEMA[index-1]
vfEMA[index] = vfEMA[index-1]
end
end
if not CandleExist(index) then
return nil
end
local beginIndex = math.max(Size() - bars, maxPeriodHV, emaPeriod)
if index == beginIndex then
maxPrice[index] = C(index)
VWAP[index] = C(index)
if periodHV < periodHV2 then
maxPricefast[index] = C(index)
VWAPfast[index] = C(index)
end
if showSigmaVol == 1 or showSigmaCenterLevel == 1 then
UpSigmaVol[index] = C(index)
DownSigmaVol[index] = C(index)
UpSigmaVolfast[index] = C(index)
DownSigmaVolfast[index] = C(index)
end
calcAlgoValue[index] = C(index)
if showTradeSignal == 1 or showEMAVWAP == 1 then
vEMA[index] = C(index)
vfEMA[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
return nil
end
outMaxPrice = maxPrice[index]
if periodHV < periodHV2 then
outMaxPriceFast = maxPricefast[index]
end
if showVWAP == 1 then
outVWAP = VWAP[index]
if periodHV < periodHV2 then
outVWAPfast = VWAPfast[index]
end
end
if showEMAVWAP == 1 then
outEMAVWAP = vEMA[index]
if periodHV < periodHV2 then
outEMAVWAPfast = vfEMA[index]
end
end
if showSigmaVol == 1 then
outUpSigmaVol = UpSigmaVol[index]
outDownSigmaVol = DownSigmaVol[index]
end
if calculated_buffer[index]~=nil then
return outMaxPrice, outVWAP, outEMAVWAP, outMaxPriceFast, outVWAPfast, outEMAVWAPfast, signal[index][1], signal[index][2], outUpSigmaVol, outDownSigmaVol
end
local previous = index-maxPeriodHV
local _p = index - previous
if C(previous) == nil then
previous = FindExistCandle(previous)
end
MAX = High[math.max(previous+1, 1)]
MIN = Low[math.max(previous+1, 1)]
for i=math.max(previous+1, 1)+1,index do
MAX = math.max(High[i], MAX)
MIN = math.min(Low[i], MIN)
end
for i = 1, clasters do CC[i]={0, i/clasters*(MAX-MIN)+MIN} end
local previousFast = index-periodHV
local needCalcFast = false
if periodHV < maxPeriodHV then
if C(previousFast) == nil then
previousFast = FindExistCandle(previousFast)
end
MAXfast = High[math.max(previousFast+1, 1)]
MINfast = Low[math.max(previousFast+1, 1)]
for i=math.max(previousFast+1, 1)+1,index do
MAXfast = math.max(High[i], MAXfast)
MINfast = math.min(Low[i], MINfast)
end
for i = 1, clasters do CCfast[i]={0, i/clasters*(MAXfast-MINfast)+MINfast} end
needCalcFast = true
end
local numProf = 0
local avgVol = 0
local numProffast = 0
local avgVolfast = 0
VWAP[index] = 0
local allVolume = 0
VWAPfast[index] = 0
local allVolumefast = 0
for i = 0, _p-1 do
if C(index-i) ~= nil then
jj=math.floor( (H(index-i)-MIN)/(MAX-MIN)*(clasters-1))+1
kk=math.floor( (L(index-i)-MIN)/(MAX-MIN)*(clasters-1))+1
for k=1,(jj-kk) do
if CC[kk+k-1][1] == 0 then numProf = numProf + 1 end
CC[kk+k-1][1]=CC[kk+k-1][1]+V(index-i)/(jj-kk)
VWAP[index] = VWAP[index] + CC[kk+k-1][2]*V(index-i)/(jj-kk)
avgVol = avgVol + V(index-i)/(jj-kk)
allVolume = allVolume + V(index-i)/(jj-kk)
end
if needCalcFast and index-i>=previousFast+1 then
jj=math.floor( (H(index-i)-MINfast)/(MAXfast-MINfast)*(clasters-1))+1
kk=math.floor( (L(index-i)-MINfast)/(MAXfast-MINfast)*(clasters-1))+1
for k=1,(jj-kk) do
if CCfast[kk+k-1][1] == 0 then numProffast = numProffast + 1 end
CCfast[kk+k-1][1]=CCfast[kk+k-1][1]+V(index-i)/(jj-kk)
avgVolfast = avgVolfast + V(index-i)/(jj-kk)
VWAPfast[index] = VWAPfast[index] + CCfast[kk+k-1][2]*V(index-i)/(jj-kk)
allVolumefast = allVolumefast + V(index-i)/(jj-kk)
end
end
end
end
VWAP[index] = VWAP[index]/allVolume
if needCalcFast then
VWAPfast[index] = VWAPfast[index]/allVolumefast
end
if showTradeSignal == 1 or showEMAVWAP == 1 then
vEMA[index]=round(kvEMA*VWAP[index]+(1-kvEMA)*vEMA[index-1], 5)
if needCalcFast then
vfEMA[index]=round(kvEMA*VWAPfast[index]+(1-kvEMA)*vfEMA[index-1], 5)
end
end
local sigma = 0
local sigmafast = 0
local maxClaster = 0
local maxClasterfast = 0
if numProf > 0 then
avgVol = round(avgVol/numProf, 5)
else
avgVol = 0
end
if numProffast > 0 then
avgVolfast = round(avgVolfast/numProffast, 5)
else
avgVolfast = 0
end
for i = 1, clasters do
MAXV = math.max(MAXV, CC[i][1])
sigma = sigma + math.pow(CC[i][1] - avgVol, 2)
if MAXV == CC[i][1] then
maxPrice[index]=CC[i][2]
maxClaster = i
end
if needCalcFast then
MAXVfast = math.max(MAXVfast, CCfast[i][1])
sigmafast = sigmafast + math.pow(CCfast[i][1] - avgVolfast, 2)
if MAXVfast == CCfast[i][1] then
maxPricefast[index]=CCfast[i][2]
maxClasterfast = i
end
end
end
if showSigmaVol == 1 or showSigmaCenterLevel == 1 then
if numProf > 1 then
sigma = round(math.sqrt(sigma/(numProf-1)), 2)
else
sigma = 0
end
if numProffast > 1 then
sigmafast = round(math.sqrt(sigmafast/(numProffast-1)), 2)
else
sigmafast = 0
end
if sigma > 0 then
local find = false
local i = maxClaster+1
for i=maxClaster+1,clasters do
if CC[i][1] < MAXV - sigma and not find then
UpSigmaVol[index] = CC[i][2]
find = true
end
if CC[i][1] > MAXV - sigma and find then
find = false
end
end
find = true
for i=maxClaster-1,1, -1 do
if CC[i][1] < MAXV - sigma and not find then
DownSigmaVol[index] = CC[i][2]
find = true
end
if CC[i][1] > MAXV - sigma and find then
find = false
end
end
end
if sigmafast > 0 then
local find = false
local i = maxClasterfast+1
for i=maxClasterfast+1,clasters do
if CCfast[i][1] < MAXVfast - sigmafast and not find then
UpSigmaVolfast[index] = CCfast[i][2]
find = true
end
if CCfast[i][1] > MAXVfast - sigmafast and find then
find = false
end
end
find = true
for i=maxClasterfast-1,1, -1 do
if CCfast[i][1] < MAXVfast - sigmafast and not find then
DownSigmaVolfast[index] = CCfast[i][2]
find = true
end
if CCfast[i][1] > MAXVfast - sigmafast and find then
find = false
end
end
end
end
calculated_buffer[index] = maxPrice[index]
outMaxPrice = maxPrice[index]
if periodHV < periodHV2 then
outMaxPriceFast = maxPricefast[index]
end
if showVWAP == 1 then
outVWAP = VWAP[index]
if periodHV < periodHV2 then
outVWAPfast = VWAPfast[index]
end
end
if showEMAVWAP == 1 then
outEMAVWAP = vEMA[index]
if periodHV < periodHV2 then
outEMAVWAPfast = vfEMA[index]
end
end
if showSigmaVol == 1 then
outUpSigmaVol = UpSigmaVol[index]
outDownSigmaVol = DownSigmaVol[index]
end
if showSigmaCenterLevel == 1 then
outMaxPrice = (UpSigmaVol[index] + DownSigmaVol[index])/2
if needCalcFast then
outMaxPriceFast = (UpSigmaVolfast[index] + DownSigmaVolfast[index])/2
end
end
if index <= Size() - bars + math.max(maxPeriodHV, emaPeriod) then
return outMaxPrice, outVWAP, outEMAVWAP, outMaxPriceFast, outVWAPfast, outEMAVWAPfast, signal[index][1], signal[index][2], outUpSigmaVol, outDownSigmaVol
end
if showTradeSignal == 1 then
local isUpPinBar = C(index)>O(index) and (H(index)-C(index))/(H(index) - L(index))>=0.5
local isLowPinBar = C(index)<O(index) and (C(index)-L(index))/(H(index) - L(index))>=0.5
local isBuy = trend[index] <= 0 and vEMA[index] > vEMA[index-1]
local isSell = trend[index] >= 0 and vEMA[index] < vEMA[index-1]
if isBuy then
trend[index] = 1
end
if isSell then
trend[index] = -1
end
--WriteLog("index "..tostring(index)..", trend "..tostring(trend[index])..", isBuy "..tostring(isBuy)..", isSell "..tostring(isSell)..", Close "..tostring(Close[index])..", vfEMA "..tostring(vfEMA[index])..", VWAP "..tostring(VWAP[index]))
if trend[index-1]>0 and trend[index-2]<=0 then
signal[index][1] = O(index)
end
if trend[index-1]<0 and trend[index-2]>=0 then
signal[index][2] = O(index)
end
if trend[index-1]==0 and trend[index-2]<0 then
signal[index][1] = O(index)
end
if trend[index-1]==0 and trend[index-2]>0 then
signal[index][2] = O(index)
end
end
return outMaxPrice, outVWAP, outEMAVWAP, outMaxPriceFast, outVWAPfast, outEMAVWAPfast, signal[index][1], signal[index][2], outUpSigmaVol, outDownSigmaVol
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
--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
function isnil(a,b)
if a == nil then
return b
else
return a
end;
end