forked from nick-nh/qlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstantaneousTrend.lua
178 lines (142 loc) · 3.91 KB
/
InstantaneousTrend.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
Settings =
{
Name = "*Ehlers Instantaneous Trend",
alpha = 0.03,
shift = 5,
cycletype = 1, -- 0 - simle, 1 - adaptive
line=
{
{
Name = "Trend",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 2
}
,
{
Name = "Trigger",
Color = RGB(255, 0, 0),
Type = TYPE_LINE,
Width = 2
}
}
}
----------------------------------------------------------
function CyberCycle()
local Price={}
local Smooth={}
local Cycle={}
local it={}
local Trigger={}
local CyclePeriod={}
local InstPeriod={}
local Q1={}
local I1={}
local DeltaPhase={}
--local SMA=fSMA()
return function(ind, _a, _t, _s)
local index = ind
local alpha = _a
local alpha1 = _a
local cycletype = _t
local shift = _s
local DC, MedianDelta
if index == 1 then
Price = {}
Smooth={}
Cycle={}
it={}
Trigger={}
CyclePeriod={}
InstPeriod={}
Q1={}
I1={}
DeltaPhase={}
Price[index] = (H(index) + L(index))/2
Smooth[index]=0
Cycle[index]=0
it[index]=0
Trigger[index]=0
CyclePeriod[index]=0
InstPeriod[index]=0
Q1[index]=0
I1[index]=0
DeltaPhase[index]=0
return nil, nil
end
Price[index] = (H(index) + L(index))/2
if index < 4 then
Cycle[index]=0
it[index]=0
CyclePeriod[index]=0
InstPeriod[index]=0
Q1[index]=0
I1[index]=0
DeltaPhase[index]=0
return nil, nil
end
Cycle[index]=(Price[index]-2.0*Price[index - 1]+Price[index - 2])/4.0
if cycletype == 1 then
Smooth[index] = (Price[index]+2*Price[index - 1]+2*Price[index - 2]+Price[index - 3])/6.0
if index < 7 then
it[index]=0
CyclePeriod[index]=0
InstPeriod[index]=0
Q1[index]=0
I1[index]=0
DeltaPhase[index]=0
return nil, nil
end
Cycle[index]=(1.0-0.5*alpha) *(1.0-0.5*alpha) *(Smooth[index]-2.0*Smooth[index - 1]+Smooth[index - 2])
+2.0*(1.0-alpha)*Cycle[index - 1]-(1.0-alpha)*(1.0-alpha)*Cycle[index - 2]
Q1[index] = (0.0962*Cycle[index]+0.5769*Cycle[index-2]-0.5769*Cycle[index-4]-0.0962*Cycle[index-6])*(0.5+0.08*(InstPeriod[index-1] or 0))
I1[index] = Cycle[index-3]
if Q1[index]~=0.0 and Q1[index-1]~=0.0 then
DeltaPhase[index] = (I1[index]/Q1[index]-I1[index-1]/Q1[index-1])/(1.0+I1[index]*I1[index-1]/(Q1[index]*Q1[index-1]))
else DeltaPhase[index] = 0
end
if DeltaPhase[index] < 0.1 then
DeltaPhase[index] = 0.1
end
if DeltaPhase[index] > 0.9 then
DeltaPhase[index] = 0.9
end
MedianDelta = Median(DeltaPhase[index],DeltaPhase[index-1], Median(DeltaPhase[index-2], DeltaPhase[index-3], DeltaPhase[index-4]))
if MedianDelta == 0.0 then
DC = 15.0
else
DC = 6.28318/MedianDelta + 0.5
end
InstPeriod[index] = 0.33 * DC + 0.67 * (InstPeriod[index-1] or 0)
CyclePeriod[index] = 0.15 * InstPeriod[index] + 0.85 * CyclePeriod[index-1]
alpha1 = 2.0/(CyclePeriod[index]+1.0)
end
it[index]=(alpha1-((alpha1*alpha1)/4.0))*Price[index]+0.5*alpha1*alpha1*Price[index-1]-(alpha1-0.75*alpha1*alpha1)*Price[index-2]+
2*(1-alpha1)*(it[index-1] or Cycle[index])-(1-alpha1)*(1-alpha1)*(it[index-2] or Cycle[index])
lag = 2.0*it[index]-(it[index-shift] or 0)
return it[index], lag
end
end
function Init()
myCyberCycle = CyberCycle()
return #Settings.line
end
function OnCalculate(index)
return myCyberCycle(index, Settings.alpha, Settings.cycletype, Settings.shift)
end
function Median(x, y, z)
return (x+y+z) - math.min(x,math.min(y,z)) - math.max(x,math.max(y,z))
end
function fSMA()
return function (Index, Period, bb)
local Out = 0
if Index >= Period then
local sum = 0
for i = Index-Period+1, Index do
sum = sum + bb[i]
end
Out = sum/Period
end
return Out
end
end