forked from nick-nh/qlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MESA.lua
188 lines (161 loc) · 5.12 KB
/
MESA.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
--[[
https://github.com/nick-nh/qlua
MESA Adaptive Moving Averages by John F. Ehlers
]]
_G.load = _G.loadfile or _G.load
local maLib = load(_G.getWorkingFolder().."\\Luaindicators\\maLib.lua")()
local logFile = nil
--logFile = io.open(_G.getWorkingFolder().."\\LuaIndicators\\MESA.txt", "w")
local message = _G['message']
local RGB = _G['RGB']
local TYPE_LINE = _G['TYPE_LINE']
local TYPE_POINT = _G['TYPE_POINT']
local isDark = _G.isDarkTheme()
local up_line_color = isDark and RGB(0, 230, 0) or RGB(0, 210, 0)
local dw_line_color = isDark and RGB(230, 0, 0) or RGB(210, 0, 0)
local os_time = os.time
--local math_abs = math.abs
local table_remove = table.remove
_G.Settings= {
Name = "*MESA",
stdPeriod = 7,
stdK = 1.0,
fastLimit = 0.42,
slowLimit = 0.05,
data_type = 'Median',
line = {
{
Name = 'MAMA',
Color = isDark and RGB(255, 193, 193) or RGB(113, 0, 0),
Type = TYPE_LINE,
Width = 1
},
{
Name = 'FAMA',
Color = isDark and RGB(193, 255, 193) or RGB(0, 113, 26),
Type = TYPE_LINE,
Width = 2
},
{
Name = 'Buy',
Color = up_line_color,
Type = TYPE_POINT,
Width = 4
},
{
Name = 'Sell',
Color = dw_line_color,
Type = TYPE_POINT,
Width = 4
}
}
}
local PlotLines = function() 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
------------------------------------------------------------------
--Moving Average
------------------------------------------------------------------
local function Algo(Fsettings, ds)
Fsettings = (Fsettings or {})
local data_type = (Fsettings.data_type or "Median")
local period = (Fsettings.stdPeriod or 10)
local stdK = (Fsettings.stdK or 1)
error_log = {}
local fAlpha
local out1
local out2
local p_buy
local p_sell
local l_index
local begin_index
local Data
local atr
local trend
local mama
local fama
return function (index)
out1 = nil
out2 = nil
p_buy = nil
p_sell = nil
local status, res = pcall(function()
if not maLib then return end
local val = maLib.Value(index, data_type, ds)
if fAlpha == nil or index == 1 then
begin_index = index
fAlpha = maLib.EthlerAlpha(Fsettings, ds)
fAlpha(index)
mama = {}
mama[index] = val
fama = {}
fama[index] = val
trend = {}
trend[index] = 0
atr = 0
Data = {}
Data[1] = maLib.Value(index, 'Close', ds) or 0
return
end
trend[index] = trend[index-1]
if index ~= l_index then
Data[#Data + 1] = Data[#Data]
if #Data > period then table_remove(Data, 1) end
--Data[#Data] = math.abs(mama[index-1] - fama[index-1])
Data[#Data] = maLib.Value(index-1, 'Close', ds)
atr = maLib.Sigma(Data)*stdK
l_index = index
end
local alpha = fAlpha(index)
local alpha2 = alpha/2
mama[index] = alpha*val + (1 - alpha)*(mama[index - 1] or 0)
fama[index] = alpha2*mama[index] + (1 - alpha2)*(fama[index - 1] or 0)
if index - begin_index > 2 then
if trend[index-1] >= 0 then
p_sell = ((fama[index-1] - mama[index-1]) > atr) and maLib.Value(index, 'Open', ds) or nil
trend[index] = p_sell and -1 or trend[index-1]
else
p_buy = ((mama[index-1] - fama[index-1]) > atr) and maLib.Value(index, 'Open', ds) or nil
trend[index] = p_buy and 1 or trend[index-1]
end
end
out1 = mama[index]
out2 = fama[index]
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 out1, out2, p_buy, p_sell
end
end
function _G.Init()
PlotLines = Algo(_G.Settings)
return 4
end
function _G.OnChangeSettings()
_G.Init()
end
function _G.OnCalculate(index)
return PlotLines(index)
end