Skip to content

Commit e2152d4

Browse files
author
everget
committed
Add new indicators
1 parent f669212 commit e2152d4

10 files changed

+582
-9
lines changed

‎src/movings/ehlers_deviation_scaled_moving_average.pine

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,48 @@
44
study("Ehlers Deviation-Scaled Moving Average", shorttitle="EDSMA", overlay=true)
55

66
length = input(title="Length", type=integer, defval=40, minval=2)
7+
ssfLength = input(title="Super Smoother Filter Length", type=integer, defval=20, minval=1)
8+
numberOfPoles = input(title="Number of Poles", type=integer, defval=2, options=[2, 3])
79
src = input(title="Source", type=source, defval=close)
810

911
PI = 2 * asin(1)
1012

11-
// Compute Super Smoother coefficients
12-
a1 = exp(-sqrt(2) * PI / (0.5 * length))
13-
b1 = 2 * a1 * cos(sqrt(2) * PI / (0.5 * length))
14-
c2 = b1
15-
c3 = -a1 * a1
16-
c1 = 1 - c2 - c3
13+
twoPoleSuperSmootherFilter(src, length) =>
14+
arg = sqrt(2) * PI / length
15+
a1 = exp(-arg)
16+
b1 = 2 * a1 * cos(arg)
17+
c2 = b1
18+
c3 = -pow(a1, 2)
19+
c1 = 1 - c2 - c3
20+
21+
ssf = 0.0
22+
ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
23+
24+
threePoleSuperSmootherFilter(src, length) =>
25+
arg = PI / length
26+
a1 = exp(-arg)
27+
b1 = 2 * a1 * cos(1.738 * arg)
28+
c1 = pow(a1, 2)
29+
30+
coef2 = b1 + c1
31+
coef3 = -(c1 + b1 * c1)
32+
coef4 = pow(c1, 2)
33+
coef1 = 1 - coef2 - coef3 - coef4
34+
35+
ssf = 0.0
36+
ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 * nz(ssf[3])
1737

1838
zeros = src - nz(src[2])
1939

2040
// Ehlers Super Smoother Filter
21-
ssf = 0.0
22-
ssf := c1 * (zeros + zeros[1]) / 2 + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
41+
ssf = numberOfPoles == 2
42+
? twoPoleSuperSmootherFilter((zeros + zeros[1]) / 2, ssfLength)
43+
: threePoleSuperSmootherFilter((zeros + zeros[1]) / 2, ssfLength)
2344

2445
// Rescale filter in terms of Standard Deviations
25-
scaledFilter = ssf / stdev(ssf, length)
46+
scaledFilter = stdev(ssf, length) != 0
47+
? ssf / stdev(ssf, length)
48+
: 0
2649

2750
alpha = abs(scaledFilter) * 5 / length
2851

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@version=3
2+
// Copyright (c) 2018-present, Alex Orekhov (everget)
3+
// Ehlers Leading Indicator script may be freely distributed under the MIT license.
4+
study("Ehlers Leading Indicator", shorttitle="ELI", overlay=true)
5+
6+
alpha1 = input(title="Alpha 1", type=float, step=0.1, defval=0.25)
7+
alpha2 = input(title="Alpha 2", type=float, step=0.1, defval=0.33)
8+
src = input(title="Source", type=source, defval=hl2)
9+
10+
lead = 0.0
11+
lead := 2 * src + (alpha1 - 2) * nz(src[1]) + (1 - alpha1) * nz(lead[1])
12+
13+
leadingIndicator = 0.0
14+
leadingIndicator := alpha2 * lead + (1 - alpha2) * nz(leadingIndicator[1])
15+
16+
plot(leadingIndicator, title="ELI", linewidth=2, color=#741b47, transp=0)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//@version=3
2+
// Copyright (c) 2018-present, Alex Orekhov (everget)
3+
// Ehlers MESA Adaptive Moving Averages (MAMA & FAMA) script may be freely distributed under the MIT license.
4+
study("Ehlers MESA Adaptive Moving Averages (MAMA & FAMA)", shorttitle="MESA", overlay=true)
5+
6+
fastLimit = input(title="Fast Limit", type=float, defval=0.5)
7+
slowLimit = input(title="Slow Limit", type=float, defval=0.05)
8+
src = input(title="Source", type=source, defval=hl2)
9+
10+
computeMESAComponent(src, mesaPeriodMult) =>
11+
(0.962 * src + 0.5769 * nz(src[2]) - 0.5769 * nz(src[4]) - 0.0962 * nz(src[6])) * mesaPeriodMult
12+
13+
computeAlpha(src, fastLimit, slowLimit) =>
14+
mesaPeriod = 0.0
15+
mesaPeriodMult = 0.075 * nz(mesaPeriod[1]) + 0.54
16+
17+
smooth = 0.0
18+
smooth := (4 * src + 3 * nz(src[1]) + 2 * nz(src[2]) + nz(src[3])) / 10
19+
20+
detrender = 0.0
21+
detrender := computeMESAComponent(smooth, mesaPeriodMult)
22+
23+
// Compute InPhase and Quadrature components
24+
I1 = nz(detrender[3])
25+
Q1 = computeMESAComponent(detrender, mesaPeriodMult)
26+
27+
// Advance the phase of I1 and Q1 by 90 degrees
28+
jI = computeMESAComponent(I1, mesaPeriodMult)
29+
jQ = computeMESAComponent(Q1, mesaPeriodMult)
30+
31+
I2 = 0.0
32+
Q2 = 0.0
33+
34+
// Phasor addition for 3 bar averaging
35+
I2 := I1 - jQ
36+
Q2 := Q1 + jI
37+
38+
// Smooth the I and Q components before applying the discriminator
39+
I2 := 0.2 * I2 + 0.8 * nz(I2[1])
40+
Q2 := 0.2 * Q2 + 0.8 * nz(Q2[1])
41+
42+
// Homodyne Discriminator
43+
Re = I2 * nz(I2[1]) + Q2 * nz(Q2[1])
44+
Im = I2 * nz(Q2[1]) - Q2 * nz(I2[1])
45+
46+
Re := 0.2 * Re + 0.8 * nz(Re[1])
47+
Im := 0.2 * Im + 0.8 * nz(Im[1])
48+
49+
if Re != 0 and Im != 0
50+
mesaPeriod := 360 / atan(Im / Re)
51+
52+
if mesaPeriod > 1.5 * nz(mesaPeriod[1])
53+
mesaPeriod := 1.5 * nz(mesaPeriod[1])
54+
55+
if mesaPeriod < 0.67 * nz(mesaPeriod[1])
56+
mesaPeriod := 0.67 * nz(mesaPeriod[1])
57+
58+
if mesaPeriod < 6
59+
mesaPeriod := 6
60+
61+
if mesaPeriod > 50
62+
mesaPeriod := 50
63+
64+
mesaPeriod := 0.2 * mesaPeriod + 0.8 * nz(mesaPeriod[1])
65+
66+
smoothPeriod = 0.0
67+
smoothPeriod := 0.33 * mesaPeriod + 0.67 * nz(smoothPeriod[1])
68+
69+
phase = 0.0
70+
71+
if I1 != 0
72+
phase := atan(Q1 / I1)
73+
74+
deltaPhase = nz(phase[1]) - phase
75+
76+
if deltaPhase < 1
77+
deltaPhase := 1
78+
79+
alpha = fastLimit / deltaPhase
80+
81+
if alpha < slowLimit
82+
alpha := slowLimit
83+
84+
alpha
85+
86+
mama(src, fastLimit, slowLimit) =>
87+
alpha = computeAlpha(src, fastLimit, slowLimit)
88+
89+
mama = 0.0
90+
mama := alpha * src + (1 - alpha) * nz(mama[1])
91+
92+
fama(src, fastLimit, slowLimit) =>
93+
alpha = computeAlpha(src, fastLimit, slowLimit)
94+
mama = mama(src, fastLimit, slowLimit)
95+
96+
fama = 0.0
97+
fama := (alpha / 2) * mama + (1 - alpha / 2) * nz(fama[1])
98+
99+
plot(mama(src, fastLimit, slowLimit), title="MAMA", linewidth=2, color=#674ea7, transp=0)
100+
plot(fama(src, fastLimit, slowLimit), title="FAMA", linewidth=2, color=#f6b26b, transp=0)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//@version=3
2+
// Copyright (c) 2018-present, Alex Orekhov (everget)
3+
// Ehlers Super Smoother Filter script may be freely distributed under the MIT license.
4+
study("Ehlers Super Smoother Filter", shorttitle="ESSF", overlay=true)
5+
6+
length = input(title="Length", type=integer, defval=15, minval=1)
7+
numberOfPoles = input(title="Number of Poles", type=integer, defval=2, options=[2, 3])
8+
srcInput = input(title="Source", defval='cc2', options=['open', 'high', 'low', 'close', 'oo2', 'oh2', 'ol2', 'oc2', 'hh2', 'hl2', 'hc2', 'll2', 'lc2', 'cc2', 'hlc3', 'ohlc4', 'wc'])
9+
10+
getSource(input) =>
11+
src = 0.0
12+
13+
if input == 'open'
14+
src := open
15+
if input == 'high'
16+
src := high
17+
if input == 'low'
18+
src := low
19+
if input == 'close'
20+
src := close
21+
if input == 'oo2'
22+
src := (open + nz(open[1])) / 2
23+
if input == 'oh2'
24+
src := (open + high) / 2
25+
if input == 'ol2'
26+
src := (open + low) / 2
27+
if input == 'oc2'
28+
src := (open + close) / 2
29+
if input == 'hh2'
30+
src := (high + nz(high[1])) / 2
31+
if input == 'hl2' // Median Price
32+
src := (high + low) / 2
33+
if input == 'hc2'
34+
src := (high + close) / 2
35+
if input == 'll2'
36+
src := (low + nz(low[1])) / 2
37+
if input == 'lc2'
38+
src := (low + close) / 2
39+
if input == 'cc2'
40+
src := (close + nz(close[1])) / 2
41+
if input == 'hlc3' // Typical Price
42+
src := (high + low + close) / 3
43+
if input == 'ohlc4'
44+
src := (open + high + low + close) / 4
45+
if input == 'wc' // Weighted Close
46+
src := (2 * close + high + low) / 4
47+
src
48+
49+
src = getSource(srcInput)
50+
51+
PI = 2 * asin(1)
52+
53+
twoPoleSuperSmootherFilter(src, length) =>
54+
arg = sqrt(2) * PI / length
55+
a1 = exp(-arg)
56+
b1 = 2 * a1 * cos(arg)
57+
c2 = b1
58+
c3 = -pow(a1, 2)
59+
c1 = 1 - c2 - c3
60+
61+
ssf = 0.0
62+
ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
63+
64+
threePoleSuperSmootherFilter(src, length) =>
65+
arg = PI / length
66+
a1 = exp(-arg)
67+
b1 = 2 * a1 * cos(1.738 * arg)
68+
c1 = pow(a1, 2)
69+
70+
coef2 = b1 + c1
71+
coef3 = -(c1 + b1 * c1)
72+
coef4 = pow(c1, 2)
73+
coef1 = 1 - coef2 - coef3 - coef4
74+
75+
ssf = 0.0
76+
ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 * nz(ssf[3])
77+
78+
essf = numberOfPoles == 2
79+
? twoPoleSuperSmootherFilter(src, length)
80+
: threePoleSuperSmootherFilter(src, length)
81+
82+
plot(essf, title="ESSF", linewidth=2, color=#741b47, transp=0)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//@version=3
2+
// Copyright (c) 2018-present, Alex Orekhov (everget)
3+
// Spencer 15-Point Moving Average script may be freely distributed under the MIT license.
4+
study("Spencer 15-Point Moving Average", shorttitle="SpMA 15", overlay=true)
5+
6+
src = input(title="Source", type=source, defval=close)
7+
8+
getWeight(i) =>
9+
weight = 0.0
10+
11+
if i == 0
12+
weight := -3
13+
if i == 1
14+
weight := -6
15+
if i == 2
16+
weight := -5
17+
if i == 3
18+
weight := 3
19+
if i == 4
20+
weight := 21
21+
if i == 5
22+
weight := 46
23+
if i == 6
24+
weight := 67
25+
if i == 7
26+
weight := 74
27+
if i == 8
28+
weight := 67
29+
if i == 9
30+
weight := 46
31+
if i == 10
32+
weight := 21
33+
if i == 11
34+
weight := 3
35+
if i == 12
36+
weight := -5
37+
if i == 13
38+
weight := -6
39+
if i == 14
40+
weight := -3
41+
42+
weight
43+
44+
spma15(src) =>
45+
sum = 0.0
46+
weightSum = 0.0
47+
48+
for i = 0 to 14
49+
weight = getWeight(i)
50+
sum := sum + nz(src[i]) * weight
51+
weightSum := weightSum + weight
52+
53+
sum / weightSum
54+
55+
plot(spma15(src), title="SpMA 15", linewidth=2, color=#741b47, transp=0)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//@version=3
2+
// Copyright (c) 2018-present, Alex Orekhov (everget)
3+
// Spencer 21-Point Moving Average script may be freely distributed under the MIT license.
4+
study("Spencer 21-Point Moving Average", shorttitle="SpMA 21", overlay=true)
5+
6+
src = input(title="Source", type=source, defval=close)
7+
8+
getWeight(i) =>
9+
weight = 0.0
10+
11+
if i == 0
12+
weight := -1
13+
if i == 1
14+
weight := -3
15+
if i == 2
16+
weight := -5
17+
if i == 3
18+
weight := -5
19+
if i == 4
20+
weight := -2
21+
if i == 5
22+
weight := 6
23+
if i == 6
24+
weight := 18
25+
if i == 7
26+
weight := 33
27+
if i == 8
28+
weight := 47
29+
if i == 9
30+
weight := 57
31+
if i == 10
32+
weight := 60
33+
if i == 11
34+
weight := 57
35+
if i == 12
36+
weight := 47
37+
if i == 13
38+
weight := 33
39+
if i == 14
40+
weight := 18
41+
if i == 15
42+
weight := 6
43+
if i == 16
44+
weight := -2
45+
if i == 17
46+
weight := -5
47+
if i == 18
48+
weight := -5
49+
if i == 19
50+
weight := -3
51+
if i == 20
52+
weight := -1
53+
54+
weight
55+
56+
spma21(src) =>
57+
sum = 0.0
58+
weightSum = 0.0
59+
60+
for i = 0 to 20
61+
weight = getWeight(i)
62+
sum := sum + nz(src[i]) * weight
63+
weightSum := weightSum + weight
64+
65+
sum / weightSum
66+
67+
plot(spma21(src), title="SpMA 21", linewidth=2, color=#741b47, transp=0)

0 commit comments

Comments
 (0)