-
Notifications
You must be signed in to change notification settings - Fork 64
/
fibo_ema.lua
220 lines (185 loc) · 4.71 KB
/
fibo_ema.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
_G.unpack = rawget(table, "unpack") or _G.unpack
local logFile = nil
-- logFile = io.open(_G.getWorkingFolder().."\\LuaIndicators\\fibo_ema.txt", "w")
local math_max = math.max
local math_min = math.min
local math_abs = math.abs
local message = _G['message']
local RGB = _G['RGB']
local TYPE_LINE = _G['TYPE_LINE']
local TYPE_POINT = _G['TYPE_POINT']
local C = _G['C']
local O = _G['O']
local H = _G['H']
local L = _G['L']
local CandleExist = _G['CandleExist']
local os_time = os.time
_G.Settings =
{
Name = "*FiboEma",
period = 30,
bars = 100,
line=
{
{
Name = "EMA",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 2
}
,
{
Name = "423.6%",
Color = RGB(0, 128, 255),
Type = TYPE_LINE,
Width = 1
}
,
{
Name = "261.8%",
Color = RGB(128, 255, 128),
Type = TYPE_LINE,
Width = 1
}
,
{
Name = "161.8%",
Color = RGB(255, 128, 255),
Type = TYPE_LINE,
Width = 1
}
,
{
Name = "100%",
Color = RGB(0, 0, 0),
Type = TYPE_POINT,
Width = 1
}
,
{
Name = "100%",
Color = RGB(0, 0, 0),
Type = TYPE_POINT,
Width = 1
}
,
{
Name = "161.8%",
Color = RGB(255, 128, 255),
Type = TYPE_LINE,
Width = 1
}
,
{
Name = "261.8%",
Color = RGB(128, 255, 128),
Type = TYPE_LINE,
Width = 1
}
,
{
Name = "423.6%",
Color = RGB(0, 128, 255),
Type = TYPE_LINE,
Width = 1
}
}
}
local PlotLines = function(index) return index end
local error_log = {}
local function log_tostring(...)
local n = select('#', ...)
if n == 1 then
return tostring(select(1, ...))
end
local t = {}
for i = 1, n do
t[#t + 1] = tostring((select(i, ...)))
end
return table.concat(t, " ")
end
local function myLog(...)
if logFile==nil then return end
logFile:write(tostring(os.date("%c",os_time())).." "..log_tostring(...).."\n");
logFile:flush();
end
----------------------------------------------------------
function Algo(Fsettings)
Fsettings = (Fsettings or {})
local period = Fsettings.period or 30
local bars = Fsettings.bars or 100
local save_bars = math_max(period, bars)
local k = 2/(period+1)
local kk = 2/(bars+1)
error_log = {}
local p_index
local l_index
local cache_EMA = {}
local cache_ATR = {}
local cache_H = {}
local cache_L = {}
local inc
return function(index)
local status, res = pcall(function()
if index == 1 then
cache_H = {}
cache_L = {}
cache_H[index] = H(index) or 0
cache_L[index] = L(index) or 0
cache_EMA = {}
cache_ATR = {}
cache_EMA[index] = (C(index)+O(index))/2
cache_ATR[index] = 0
p_index = index
l_index = index
inc = 0
return
end
cache_EMA[index] = cache_EMA[index-1]
cache_ATR[index] = cache_ATR[index-1]
cache_H[index] = cache_H[index-1]
cache_L[index] = cache_L[index-1]
if not CandleExist(index) then
return
end
if index ~= l_index then p_index = l_index end
cache_EMA[index]=k*C(index)+(1-k)*cache_EMA[index-1]
local ATR = math_max(math_abs(H(index) - L(index)), math_abs(H(index) - C(p_index)), math_abs(C(p_index) - L(index)))
cache_ATR[index] = kk*ATR+(1-kk)*cache_ATR[index-1]
l_index = index
if index <= bars then
return
end
cache_H[index] = H(index) - cache_EMA[index]
cache_L[index] = L(index) - cache_EMA[index]
local high = math_max(unpack(cache_H, index-bars+1, index))
local low = math_min(unpack(cache_L, index-bars+1, index))
inc = (math_abs(high) > math_abs(low)) and high or low
inc = math_abs(inc) + cache_ATR[index]*2
cache_EMA[index - save_bars] = nil
cache_ATR[index - save_bars] = nil
cache_H[index - save_bars] = nil
cache_L[index - save_bars] = nil
end)
if not status then
if not error_log[tostring(res)] then
error_log[tostring(res)] = true
myLog(tostring(res))
message(tostring(res))
end
return nil
end
return cache_EMA[index], cache_EMA[index]+inc*0.618, cache_EMA[index]+inc*0.5, cache_EMA[index]+inc*0.382, cache_EMA[index]+inc*0.236, cache_EMA[index]-inc*0.236, cache_EMA[index]-inc*0.382, cache_EMA[index]-inc*0.5, cache_EMA[index]-inc*0.618
end
end
----------------------------
function _G.Init()
PlotLines = Algo(_G.Settings)
return 9
end
function _G.OnChangeSettings()
_G.Init()
end
function _G.OnCalculate(index)
return PlotLines(index)
end