-
Notifications
You must be signed in to change notification settings - Fork 64
/
CenterOfGravity.lua
194 lines (156 loc) · 4.2 KB
/
CenterOfGravity.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
Settings =
{
Name = "*CenterOfGravity",
alpha = 0.07,
cGLength = 10,
cycletype = 1, -- 0 - simle, 1 - adaptive
line=
{
{
Name = "Cycle",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 2
}
,
{
Name = "Trigger Line",
Color = RGB(255, 0, 0),
Type = TYPE_LINE,
Width = 2
}
}
}
----------------------------------------------------------
function CyberCycle()
local Price={}
local Smooth={}
local Cycle={}
local aCycle={}
local Trigger={}
local CyclePeriod={}
local InstPeriod={}
local Q1={}
local I1={}
local DeltaPhase={}
--local SMA=fSMA()
return function(ind, _a, _cg, _t)
local index = ind
local alpha = _a
local cycletype = _t
local cGLength = _cg
local DC, MedianDelta, alpha1
if index == 1 then
Price = {}
Smooth={}
Cycle={}
aCycle={}
Trigger={}
CyclePeriod={}
InstPeriod={}
Q1={}
I1={}
DeltaPhase={}
Price[index] = (H(index) + L(index))/2
Smooth[index]=0
Cycle[index]=0
aCycle[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
local Num = 0.0
local Denom = 0.0
local count = 0
if cycletype == 1 then
if index < 4 then
Cycle[index]=0
aCycle[index]=0
CyclePeriod[index]=0
InstPeriod[index]=0
Q1[index]=0
I1[index]=0
DeltaPhase[index]=0
return nil, nil
end
Smooth[index] = (Price[index]+2*Price[index - 1]+2*Price[index - 2]+Price[index - 3])/6.0
Cycle[index]=(Price[index]-2.0*Price[index - 1]+Price[index - 2])/4.0
if index < 7 then
aCycle[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]
Trigger[index] = Cycle[index-1]
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]
cGLength = math.floor(CyclePeriod[index]/2.0)
end
if index < cGLength then
return nil, nil
end
while count < cGLength do
Num = Num + (1.0+count)*Price[index - count]
Denom = Denom + Price[index - count]
count = count + 1
end
if Denom ~= 0.0 then
aCycle[index] = -Num/Denom+(cGLength+1.0)/2.0
else
aCycle[index] = 0.0
end
Trigger[index] = aCycle[index-1]
return aCycle[index], Trigger[index]
end
end
function Init()
myCyberCycle = CyberCycle()
return #Settings.line
end
function OnCalculate(index)
return myCyberCycle(index, Settings.alpha, Settings.cGLength, Settings.cycletype)
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