-
Notifications
You must be signed in to change notification settings - Fork 64
/
maLib.lua
3885 lines (3262 loc) · 129 KB
/
maLib.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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local O = _G['O']
local C = _G['C']
local H = _G['H']
local L = _G['L']
local V = _G['V']
local Size = _G['Size']
local table_remove = table.remove
local string_upper = string.upper
local string_sub = string.sub
local string_len = string.len
local string_match = string.match
local math_floor = math.floor
local math_ceil = math.ceil
local math_max = math.max
local math_min = math.min
local math_abs = math.abs
local math_pow = function(x, y) return x^y end
local math_fmod = math.fmod
local math_sqrt = math.sqrt
local math_exp = math.exp
local math_log = math.log
local os_time = os.time
local os_date = os.date
local math_huge = math.huge
_G.unpack = rawget(table, "unpack") or _G.unpack
local M = {}
M.LICENSE = {
_VERSION = 'MA lib 2023.08.15',
_DESCRIPTION = 'quik lib',
_AUTHOR = 'nnh: [email protected]'
}
local function is_date(val)
local status = pcall(function() return type(val) == "table" and os_time(val); end)
return status
end
---@param strT string
local function FixStrTime(strT)
strT=tostring(strT)
local hour, min, sec = 0, 0, 0
local len = string_len(strT)
if len==8 then
hour,min,sec = string_match(strT,"(%d%d)%p(%d%d)%p(%d%d)")
elseif len==7 then
hour,min,sec = string_match(strT,"(%d)%p(%d%d)%p(%d%d)")
elseif len==6 then
hour,min,sec = string_match(strT,"(%d%d)(%d%d)(%d%d)")
elseif len==5 then
hour,min,sec = string_match(strT,"(%d)(%d%d)(%d%d)")
elseif len==4 then
hour,min = string_match(strT,"(%d%d)(%d%d)")
end
return hour,min,sec
end
-- Приводит время из строкового формата ЧЧ:ММ:CC к формату datetime
---@param str_time string
local function StrToTime(str_time, sdt)
if type(str_time) ~= 'string' then return os_date('*t') end
if not is_date(sdt) then os_date('*t') end
local h,m,s = FixStrTime(str_time)
sdt.hour = tonumber(h)
sdt.min = tonumber(m)
sdt.sec = s==nil and 0 or tonumber(s)
return sdt
end
-- Приводит время из строкового формата ЧЧ:ММ[:CC] к формату datetime
---@param str_time string
---@param sdt table|nil
local function GetStringTime(str_time, sdt)
return str_time==0 and {} or (StrToTime(#tostring(str_time)<6 and tostring(str_time)..':00' or tostring(str_time), sdt))
end
------------------------------------------------------------------
--Moving Average
------------------------------------------------------------------
local function Slice(input, start, finish)
start = start or 1
finish = finish or #input
if start == 1 and finish == #input then return input end
local output = {}
for i=start, finish or #input do
output[#output + 1] = input[i]
end
return output
end
local function Sum(input, start, finish)
start = start or 1
finish = finish or #input
local output = 0
for i=start, finish or #input do
output = output + input[i]
end
return output
end
local function wSum(input, start, finish)
start = start or 1
finish = finish or #input
local output = 0
for i=start, finish or #input do
output = output + input[i]*(start-i+1)
end
return output
end
local function Normalize(input, start, finish)
start = start or 1
finish = finish or #input
local output = {}
local max_i = input[start]
local min_i = input[start]
for i=start, finish or #input do
output[#output + 1] = input[i]
if input[i] > max_i then max_i = #output end
if input[i] < min_i then min_i = #output end
end
table_remove(output, min_i)
table_remove(output, max_i-1)
return output
end
-- Среднеквадратическое отклонение
local function Sigma(input, avg, start, finish, not_shifted)
start = math_max(start or 1, 1)
finish = finish or #input
local period = finish - start + 1
avg = avg or Sum(Slice(input, start, finish), 1)/period
local sq = 0
for i = start, finish do
if input[i] then
sq = sq + math_pow(input[i] - avg, 2)
end
end
return math_sqrt(sq/(not_shifted and period or (period-1)))
end
-- Коэффициент корреляции Пирсона|автокорреляции
-- Ковариация
---@param input table
---@param cmp table|number
---@param start number
---@param finish number
---@return number|nil
---@return number|nil
local function Correlation(input, cmp, start, finish)
local tc = type(cmp)
local shift = 0
local compare
if tc == 'table' and not cmp[start] then return end
if tc == 'number' then
compare = input
shift = cmp
else
compare = cmp
end
local num = 0
local sx = 0
local sy = 0
local sxx = 0
local syy = 0
local sxy = 0
for i = start+shift, finish do
if compare[i-shift] and input[i] then
sx = sx + input[i]
sy = sy + compare[i-shift]
sxx = sxx + input[i]*input[i]
syy = syy + compare[i-shift]*compare[i-shift]
sxy = sxy + input[i]*compare[i-shift]
num = num + 1
end
end
local lrc = ((num*sxx - sx*sx)*(num*syy - sy*sy) > 0) and (num*sxy - sx*sy)/math_sqrt((num*sxx - sx*sx)*(num*syy - sy*sy)) or 0
local cov = sxy/num - (sx/num)*(sy/num)
return lrc, cov
end
local function rounding(num, round, scale)
scale = scale or 0
if not round or string_upper(round)== "OFF" then return num end
if num and tonumber(scale) then
local mult = 10^scale
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
local function Value(index, data_type, ds)
local Out
if tostring(data_type):upper() == 'TIME' then
return (ds and ds:T(index))
end
data_type = (data_type and string_upper(string_sub(data_type,1,1))) or "A"
if data_type ~= "A" and index >= 1 then
if data_type == "O" then --Open
Out = (ds and ds:O(index)) or (O and O(index))
elseif data_type == "H" then --High
Out = (ds and ds:H(index)) or (H and H(index))
elseif data_type == "L" then --Low
Out = (ds and ds:L(index)) or (L and L(index))
elseif data_type == "C" then --Close
Out = (ds and ds:C(index)) or (C and C(index))
elseif data_type == "V" then --Volume
Out = (ds and ds:V(index)) or (V and V(index))
elseif data_type == "M" then --Median
Out = ((Value(index,"H",ds) + Value(index,"L",ds)) / 2)
elseif data_type == "T" then --Typical
Out = ((Value(index,"M",ds) * 2 + Value(index,"C",ds))/3)
elseif data_type == "W" then --Weighted
Out = ((Value(index,"T",ds) * 3 + Value(index,"O",ds))/4)
elseif data_type == "D" then --Difference
Out = (Value(index,"H",ds) - Value(index,"L", ds))
end
elseif data_type == "A" then --Any
Out = ds and ds[index]
end
return Out or 0
end
local function dsSize(data_type, ds)
data_type = (data_type and string_upper(string_sub(data_type,1,1))) or "A"
if data_type == 'A' and ds then
return #ds
end
if data_type ~= 'A' then
if Size then
return Size()
end
if ds and ds.Size then
return ds:Size()
end
end
return 0
end
local function CheckIndex(index, ds, data_type)
data_type = (data_type and string_upper(string_sub(data_type,1,1))) or "C"
if data_type == 'A' and ds then
return ds and ds[index]
end
if data_type ~= 'A' then
return (C and C(index)) or ((ds and ds.C) and ds:C(index)) or (ds and ds[index])
end
end
local function GetIndex(index, shift, ds, data_type)
while (index-shift) > 1 and not CheckIndex(index-shift, ds, data_type) do
shift = shift -1
end
return index-shift
end
local function wave_processor(ds, waves_buffer, wave_data)
wave_data = wave_data or {}
local zz_waves = {}
local n_waves = 0
local shift = 0
local last_zz
waves_buffer = waves_buffer or 0
local WAV_SDMA, err
if waves_buffer > 1 then
WAV_SDMA, err = M.new({method = "SD", ma_method = "SMA", not_shifted = true, data_type = 'Any', period = waves_buffer}, zz_waves)
if not WAV_SDMA then
return nil, err
end
end
local count_index = {}
local function update_wave(index, high, low)
if (wave_data.max or high) < high then
wave_data.max = high
wave_data.max_index = index
wave_data.cur_wave = (wave_data.max - wave_data.min)
zz_waves[n_waves] = wave_data.cur_wave
end
if (wave_data.min or low) > low then
wave_data.min = low
wave_data.min_index = index
wave_data.cur_wave = (wave_data.max - wave_data.min)
zz_waves[n_waves] = wave_data.cur_wave
end
end
return function(index, trend, high, low, online)
shift = online and 1 or 0
count_index[index] = (count_index[index] or 0) + 1
if not wave_data.begin_index then
wave_data.trend = trend[index]
wave_data.begin_index = index
wave_data.max = high
wave_data.max_index = index
wave_data.min = low
wave_data.min_index = index
wave_data.cur_wave = (wave_data.max - wave_data.min)
end
if trend[index] == trend[index-1] then
update_wave(index, high, low)
end
if online or count_index[index] > 1 then
return last_zz, wave_data
end
if trend[index-shift] ~= trend[index-shift-1] then
if n_waves > 0 then
if WAV_SDMA then
local w_sd, w_ma = WAV_SDMA(n_waves)
wave_data.sd = w_sd[n_waves]
wave_data.ma = w_ma[n_waves]
end
last_zz = wave_data[trend[index-shift-1] == 1 and 'max' or 'min']
wave_data.last_zz = last_zz
wave_data.last_wave = (wave_data.max - wave_data.min)
wave_data.end_index = index-shift-1
end
wave_data.trend = trend[index-shift]
wave_data.begin_index = index-shift
wave_data.max = high
wave_data.max_index = index-shift
wave_data.min = low
wave_data.min_index = index-shift
local h1 = M.Value(index-1, 'High', ds)
if shift > 0 then
if wave_data.max < h1 then
wave_data.max = h1
wave_data.max_index = index-1
end
local l1 = M.Value(index-1, 'low', ds)
if wave_data.min > l1 then
wave_data.min = l1
wave_data.min_index = index-1
end
end
wave_data.cur_wave = (wave_data.max - wave_data.min)
n_waves = n_waves + 1
zz_waves[n_waves] = wave_data.cur_wave
zz_waves[n_waves - waves_buffer] = nil
end
return last_zz, wave_data
end, wave_data
end
local function HilbertTransform(index, src)
return 0.0962 * src[index] + 0.5769 * (src[index - 2] or 0) - 0.5769 * (src[index - 4] or 0) - 0.0962 * (src[index- 6] or 0)
end
--[[Ehlers Adaptive alfa
]]
local function EthlerAlpha(settings, ds)
local fastLimit = (settings.fastLimit or 0.5)
local slowLimit = (settings.slowLimit or 0.05)
local data_type = (settings.data_type or 'Close')
local atan = math.atan
local pi = math.pi
local mesaPeriod = {}
local smooth = {}
local detrender = {}
local I1 = {}
local Q1 = {}
local I2 = {}
local Q2 = {}
local Re = {}
local Im = {}
local phase = {}
local deltaPhase = {}
local alpha
local function computeComponent(index, src, k)
return HilbertTransform(index, src)*k
end
return function(index)
mesaPeriod[index] = mesaPeriod[index-1] or 0
smooth[index] = smooth[index-1] or 0
detrender[index] = detrender[index-1] or 0
I1[index] = I1[index-1] or 0
Q1[index] = Q1[index-1] or 0
I2[index] = I2[index-1] or 0
Q2[index] = Q2[index-1] or 0
Re[index] = Re[index-1] or 0
Im[index] = Im[index-1] or 0
phase[index] = phase[index-1] or 0
deltaPhase[index] = deltaPhase[index-1] or 0
if not CheckIndex(index, ds, data_type) then
return alpha
end
local mesaPeriodMult = 0.075*(mesaPeriod[index - 1] or 0) + 0.54
smooth[index] = (4*Value(index, data_type, ds) + 3*(Value(GetIndex(index, 1, ds, data_type), data_type, ds)) + 2*(Value(GetIndex(index, 2, ds, data_type), data_type, ds)) + (Value(GetIndex(index, 3, ds, data_type), data_type, ds)))/10
detrender[index] = computeComponent(index, smooth, mesaPeriodMult)
--Compute InPhase and Quadrature components
I1[index] = detrender[index-3] or 0
Q1[index] = computeComponent(index, detrender, mesaPeriodMult)
--Advance the phase of I1 and Q1 by 90 degrees
local jI = computeComponent(index, I1, mesaPeriodMult)
local jQ = computeComponent(index, Q1, mesaPeriodMult)
--Phasor addition for 3 bar averaging
I2[index] = I1[index] - jQ
Q2[index] = Q1[index] + jI
--Smooth the I and Q components before applying the discriminator
I2[index] = 0.2*I2[index] + 0.8*(I2[index - 1] or 0)
Q2[index] = 0.2*Q2[index] + 0.8*(Q2[index - 1] or 0)
--Homodyne Discriminator
Re[index] = I2[index]*(I2[index - 1] or 0) + Q2[index]*(Q2[index - 1] or 0)
Im[index] = I2[index]*(Q2[index - 1] or 0) - Q2[index]*(I2[index - 1] or 0)
Re[index] = 0.2*Re[index] + 0.8*(Re[index - 1] or 0)
Im[index] = 0.2*Im[index] + 0.8*(Im[index - 1] or 0)
if Re[index] ~= 0 and Im[index] ~= 0 then
mesaPeriod[index] = 2*pi/atan(Im[index]/Re[index])
end
if mesaPeriod[index] > 1.5*(mesaPeriod[index - 1] or 0) then
mesaPeriod[index] = 1.5*(mesaPeriod[index - 1] or 0)
end
if mesaPeriod[index] < 0.67*(mesaPeriod[index - 1] or 0) then
mesaPeriod[index] = 0.67*(mesaPeriod[index - 1] or 0)
end
if mesaPeriod[index] < 6 then
mesaPeriod[index] = 6
end
if mesaPeriod[index] > 50 then
mesaPeriod[index] = 50
end
mesaPeriod[index] = 0.2*mesaPeriod[index] + 0.8*(mesaPeriod[index - 1] or 0)
if I1[index] ~= 0 then
phase[index] = (180/pi)*atan(Q1[index]/I1[index])
end
deltaPhase[index] = (phase[index - 1] or 0) - phase[index]
if deltaPhase[index] < 1 then
deltaPhase[index] = 1
end
alpha = fastLimit/deltaPhase[index]
if alpha < slowLimit then
alpha = slowLimit
end
return alpha
end
end
--[[
Ehlers Deviation-Scaled filters
]]
local function Get2PoleSSF(settings, ds)
local period = (settings.period or 20)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = math_max((settings.save_bars or period), 3)
local pi = math.pi
local arg = math.sqrt(2)*pi/(period)
local a1 = math.exp(-arg)
local b1 = 2*a1*math.cos(arg)
local c2 = b1
local c3 = -(a1*a1)
local c1 = 1 - c2 - c3
local SSF = {}
return function(index)
SSF[index] = SSF[index-1] or 0
if not CheckIndex(index, ds) then
return SSF
end
SSF[index] = rounding(c1*Value(index, data_type, ds) + c2*(SSF[index-1] or 0) + c3*(SSF[index-2] or 0), round, scale)
SSF[index-save_bars] = nil
return SSF
end, SSF
end
local function Get3PoleSSF(settings, ds)
local period = (settings.period or 20)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = math_max((settings.save_bars or period), 4)
local pi = math.pi
local arg = pi/(period)
local a1 = math.exp(-arg)
local b1 = 2*a1*math.cos(1.738*arg)
local c1 = a1*a1
local coef2 = b1 + c1
local coef3 = -(c1 + b1*c1)
local coef4 = c1*c1
local coef1 = 1 - coef2 - coef3 - coef4
local SSF = {}
return function(index)
SSF[index] = SSF[index-1] or 0
if not CheckIndex(index, ds) then
return SSF
end
SSF[index] = rounding(coef1*Value(index, data_type, ds) + coef2*(SSF[index-1] or 0) + coef3*(SSF[index-2] or 0) + coef4*(SSF[index-3] or 0), round, scale)
SSF[index-save_bars] = nil
return SSF
end, SSF
end
--[[Average True Range
]]
local function F_ATR(settings, ds)
local period = (settings.period or 9)
local save_bars = (settings.save_bars or period)
local ATR = {}
local p_index
local l_index
return function(index)
ATR[index] = ATR[index-1] or 0
if not CheckIndex(index, ds) then
return ATR
end
if index ~= l_index then p_index = l_index end
local high = Value(index, 'High', ds)
local low = Value(index, 'low', ds)
local p_close = Value(p_index or 1, 'Close', ds)
ATR[index] = high - low
if p_index then
ATR[index] = (ATR[index-1]*(period-1) + math_max(math_abs(high - low), math_abs(high - p_close), math_abs(p_close - low)))/period
end
ATR[index-save_bars] = nil
l_index = index
return ATR
end, ATR
end
--[[PRICE RANGE
]]
local function F_P_RANGE(settings, ds)
local period = (settings.period or 9)
local RANGE = {}
local LOW = {}
local HIGH = {}
local l_high, l_low, s_index, ip
return function(index)
if not CheckIndex(index, ds) then
return RANGE
end
s_index = s_index or index
ip = math_fmod(index - s_index, period)+1
LOW[ip] = Value(index, 'Low', ds)
HIGH[ip] = Value(index, 'High', ds)
l_high = math_max(unpack(HIGH))
l_low = math_min(unpack(LOW))
RANGE[index] = l_high - l_low
return RANGE, HIGH, LOW
end, RANGE, l_high, l_low
end
--[[Sum od values
sum(Pi)
]]
local function F_SUM(settings)
local period = (settings.period or 9)
local save_bars = (settings.save_bars or period)
local S_ACC = {}
local S_TMP = {}
local bars = 0
local l_index
return function(index, val)
S_TMP[index] = S_TMP[index-1] or 0
if not val then
return S_TMP
end
S_ACC[#S_ACC + (l_index == index and 0 or 1)] = (S_ACC[#S_ACC - (l_index == index and 1 or 0)] or 0) + (val or 0)
if l_index ~= index then
l_index = index
bars = bars + 1
end
if bars > period then
if #S_ACC > period + 1 then table_remove(S_ACC, 1) end
S_TMP[index] = S_ACC[#S_ACC] - (S_ACC[1] or 0)
else
S_TMP[index] = S_ACC[#S_ACC]
end
S_TMP[index-save_bars] = nil
return S_TMP
end, S_TMP
end
--[[Simple Moving Average (SMA)
SMA = sum(Pi) / n
]]
local function F_SMA(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local fSum = F_SUM(settings)
local SMA_TMP = {}
local bars = 0
local l_index
return function(index)
SMA_TMP[index] = SMA_TMP[index-1] or 0
local sum = fSum(index, (Value(index, data_type, ds) or 0))[index]
if not CheckIndex(index, ds) then
return SMA_TMP
end
if l_index ~= index then
l_index = index
bars = bars + 1
end
bars = bars < period and bars or period
SMA_TMP[index] = rounding(sum/bars, round, scale)
SMA_TMP[index-save_bars] = nil
return SMA_TMP
end, SMA_TMP
end
--[[Произвольная Weighted Moving Average (LWMA) — Произвольная (функция)-взвешенная скользящая средняя
]]
local function F_LWMA(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local lambda = type(settings.weight_func) == 'function' and settings.weight_func or function(i) return i end
local LWMA_TMP = {}
local bars = 0
local l_index
return function(index)
LWMA_TMP[index] = LWMA_TMP[index-1] or 0
if l_index ~= index then
l_index = index
bars = bars + 1
end
bars = bars < period and bars or period
local w
local sum, n = 0, 0
for i = 1, bars do
if CheckIndex(index-bars+i, ds) then
w = lambda(i)
sum = sum + (Value(index-bars+i, data_type, ds) or 0)*w
n = n + w
end
end
LWMA_TMP[index] = rounding(sum/n, round, scale)
LWMA_TMP[index-save_bars] = nil
return LWMA_TMP
end, LWMA_TMP
end
--[[Standard Deviation
]]
local function F_SD(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local ma_method = (settings.ma_method or 'SMA')
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local not_shifted = settings.not_shifted
local calc_avg = settings.calc_avg
if calc_avg == nil then calc_avg = true end
local fMA, cavg
if calc_avg then
fMA, cavg = M.new({period = period, data_type = data_type, method = ma_method, round = round, scale = scale}, ds)
end
local SD = {}
local input = {}
return function(index, avg)
SD[index] = SD[index-1] or 0
input[index] = input[index-1] or 0
avg = avg or fMA(index)
if not CheckIndex(index, ds) or not avg[index] then
return SD, avg
end
input[index] = Value(index, data_type, ds) or 0
local sq = 0
for i = index - period + 1, index do
if input[i] then
sq = sq + math_pow(input[i] - avg[index], 2)
end
end
SD[index] = math_sqrt(sq/(not_shifted and period or (period-1)))
SD[index-save_bars] = nil
input[index-save_bars] = nil
return SD, cavg
end, SD, cavg
end
--[[Fractal Adaptive Moving Average]]
local function F_FRAMA(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or (2*period))
local bars = 0
local FRAMA_TMP = {}
local h_buff = {}
local l_buff = {}
local HH = function(index, length)
return math_max(unpack(h_buff, index-length+1, index))
end
local LL = function(index, length)
return math_min(unpack(l_buff, index-length+1, index))
end
local N = function(index, length)
return (HH(index, length) - LL(index, length))/length
end
local D = function(index)
return (math_log(N(index, period) + N(index-period, period)) - math_log(N(index, 2*period)))/math_log(2)
end
local A = function(index)
return math_exp(-4.6*(D(index)-1))
end
local l_index
return function(index)
local val = Value(index, data_type, ds)
FRAMA_TMP[index] = FRAMA_TMP[index-1] or val
h_buff[index] = h_buff[index-1] or 0
l_buff[index] = l_buff[index-1] or 0
if not CheckIndex(index, ds) then
return FRAMA_TMP
end
h_buff[index] = Value(index, 'High', ds)
l_buff[index] = Value(index, 'Low', ds)
if l_index ~= index then
l_index = index
bars = bars + 1
end
if bars >= 2*period then
local a = A(index)
FRAMA_TMP[index] = rounding(a*val + (1-a)*FRAMA_TMP[index-1], round, scale)
end
FRAMA_TMP[index-save_bars] = nil
return FRAMA_TMP
end, FRAMA_TMP
end
--[[Exponential Moving Average (EMA)
EMAi = (EMAi-1*(n-1)+2*Pi) / (n+1)
]]
local function F_EMA(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local EMA_TMP = {}
local val
return function(index)
EMA_TMP[index] = EMA_TMP[index-1] or 0
if not CheckIndex(index, ds) then
return EMA_TMP
end
val = Value(index, data_type, ds)
EMA_TMP[index] = EMA_TMP[index-1] and rounding((EMA_TMP[index-1]*(period-1) + 2*val)/(period+1), round, scale) or rounding(val, round, scale)
EMA_TMP[index-save_bars] = nil
return EMA_TMP
end, EMA_TMP
end
--[[
William Moving Average (WMA)
( Previous WILLMA * ( period - 1 ) + Data ) / period
]]
local function F_WILLMA(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local WMA_TMP = {}
return function(index)
WMA_TMP[index] = WMA_TMP[index-1] or 0
if not CheckIndex(index, ds) then
return WMA_TMP
end
if WMA_TMP[index-1] == nil then
WMA_TMP[index] = rounding(Value(index, data_type, ds), round, scale)
else
WMA_TMP[index] = rounding((WMA_TMP[index-1]*(period-1) + Value(index, data_type, ds))/period, round, scale)
end
WMA_TMP[index-save_bars] = nil
return WMA_TMP
end, WMA_TMP
end
--[[
Hull Moving Average
HMA= LWMA(2*LWMA(n/2) ? LWMA(n)),sqrt(n))
]]
local function F_HMA(settings, ds)
local period = (settings.period or 9)
local divisor = (settings.divisor or 2)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local fLwma = F_LWMA({period = period, data_type = data_type}, ds)
local fLwma2 = F_LWMA({period = M.rounding(period/divisor, 'on'), data_type = data_type}, ds)
local swma = {}
local fHMA = F_LWMA({period = M.rounding(math_sqrt(period), 'on'), data_type = 'Any'}, swma)
local HMA_TMP = {}
return function(index)
HMA_TMP[index] = HMA_TMP[index-1] or 0
if not CheckIndex(index, ds) then
return HMA_TMP
end
if HMA_TMP[index-1] == nil then
HMA_TMP[index] = rounding(Value(index, data_type, ds), round, scale)
else
swma[index] = 2*fLwma2(index)[index] - fLwma(index)[index]
HMA_TMP[index] = rounding(fHMA(index)[index], round, scale)
end
HMA_TMP[index-save_bars] = nil
return HMA_TMP
end, HMA_TMP
end
--[[Arnaud Legoux Moving Average (ALMA)
]]
local function F_ALMA(settings, ds)
local period = (settings.period or 9)
local offset = (settings.offset or 0.85)
local sigma = (settings.sigma or 6)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local ALMA = {}
local m = (offset * (period - 1))
local s = (period/sigma)
local w = {}
for k = 0, period-1 do
w[k+1] = math.exp(-((k-m)^2)/(2*(s^2)))
end
local fLwma = F_LWMA({period = period, data_type = data_type, round = round, scale = scale, weight_func = function(i) return w[i] end}, ds)
return function (index)
ALMA[index] = ALMA[index-1] or 0
if not CheckIndex(index, ds) then
return ALMA
end
ALMA[index] = fLwma(index)[index]
ALMA[index-save_bars] = nil
return ALMA
end, ALMA
end
--[[
Jurik Moving Average
]]
local function F_JMA(settings, ds)
local period = (settings.period or 7)
local phase = (settings.phase or 3)
local power = (settings.power or 1)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local phase_ratio = phase < -100 and 0.5 or (phase > 100 and 2.5 or phase/100 + 1.5)
local beta = 0.45 * (period - 1) / (0.45 * (period - 1) + 2)
local alpha = math_pow(beta, power)
local JMA = {}
local J0 = {}
local J1 = {}
local J2 = {}
return function(index)
JMA[index] = JMA[index-1] or 0
J0[index] = J0[index-1] or 0
J1[index] = J1[index-1] or 0
J2[index] = J2[index-1] or 0
if not CheckIndex(index, ds) then
return JMA
end
local val = Value(index, data_type, ds)
J0[index] = (1 - alpha)*val + alpha*(J0[index-1] or 0)
J1[index] = (val - J0[index]) * (1 - beta) + beta*(J1[index-1] or 0)
J2[index] = (J0[index] + phase_ratio*J1[index] - (JMA[index-1] or 0)) * math_pow(1 - alpha, 2) + math_pow(alpha, 2)*(J2[index-1] or 0)
if JMA[index-1] == nil then
JMA[index] = rounding(Value(index, data_type, ds), round, scale)
else
JMA[index] = rounding(J2[index] + (JMA[index-1] or 0), round, scale)
end
JMA[index-save_bars] = nil
J0[index-save_bars] = nil
J1[index-save_bars] = nil
J2[index-save_bars] = nil
return JMA
end, JMA
end
--[[Weighted Moving Average (WMA)
WMA = sum(Pi*i) / sum(i)
]]
local function F_WMA(settings, ds)
local period = (settings.period or 9)
local data_type = (settings.data_type or "Close")
local round = (settings.round or "OFF")
local scale = (settings.scale or 0)
local save_bars = (settings.save_bars or period)
local WMA = {}
local w = {}
for k = 1, period do
w[k] = k
end
local fLwma = F_LWMA({period = period, data_type = data_type, round = round, scale = scale, weight_func = function(i) return w[i] end}, ds)
return function (index)
WMA[index] = WMA[index-1] or 0
if not CheckIndex(index, ds) then
return WMA
end
WMA[index] = fLwma(index)[index]
WMA[index-save_bars] = nil
return WMA
end, WMA
end