-
Notifications
You must be signed in to change notification settings - Fork 64
/
smTMMS.lua
254 lines (217 loc) · 8.57 KB
/
smTMMS.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--[[
https://github.com/nick-nh/qlua
smTMMS Oscillator (Trading Made More Simpler)
]]
_G.unpack = rawget(table, "unpack") or _G.unpack
_G.load = _G.loadfile or _G.load
local maLib = load(_G.getWorkingFolder().."\\Luaindicators\\maLib.lua")()
local logFile = nil
-- logFile = io.open(_G.getWorkingFolder().."\\LuaIndicators\\smTMMS.txt", "w")
local message = _G['message']
local RGB = _G['RGB']
local isDark = _G.isDarkTheme()
local line_color = isDark and RGB(240, 240, 240) or RGB(0, 0, 0)
local os_time = os.time
_G.Settings= {
Name = "*smTMMS Oscillator",
['Период RSI'] = 14,
['Период K Stochastic(1)'] = 8,
['Период D Stochastic(1)'] = 3,
['Метод MA Stochastic(1)'] = 'EMA',
['Период K Stochastic(2)'] = 14,
['Период D Stochastic(2)'] = 3,
['Метод MA Stochastic(2)'] = 'EMA',
['Период Q SMI'] = 9,
['Период R SMI'] = 9,
['Период S SMI'] = 9,
['Метод MA SMI'] = 'EMA',
['Показывать гистограмму'] = 'STOCH1; *STOCH2',
['Показывать линию Hull'] = 0,
['Период Hull'] = 12,
['Делитель Hull'] = 2,
['Тип данных'] = 'Close',
line = {
{
Name = 'UP',
Color = RGB(0, 180, 0),
Type = _G['TYPE_HISTOGRAM'],
Width = 2
},
{
Name = 'DW',
Color = RGB(180, 0, 0),
Type = _G['TYPE_HISTOGRAM'],
Width = 2
},
{
Name = 'NO',
Color = RGB(128, 128, 128),
Type = _G['TYPE_HISTOGRAM'],
Width = 2
},
{
Name = 'SMI',
Color = line_color,
Type = _G['TYPE_LINE'],
Width = 1
},
{
Name = 'Hull UP',
Color = RGB(0, 128, 255),
Type = _G['TYPE_POINT'],
Width = 3
},
{
Name = 'Hull DW',
Color = RGB(255, 128, 0),
Type = _G['TYPE_POINT'],
Width = 3
},
{
Name = "change dir up",
Type = _G['TYPE_TRIANGLE_UP'],
Width = 3,
Color = RGB(89,213, 107)
},
{
Name = "change dir dw",
Type = _G['TYPE_TRIANGLE_DOWN'],
Width = 3,
Color = RGB(255, 58, 0)
}
}
}
local PlotLines = function(index) return index end
local error_log = {}
local lines = #_G.Settings.line
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 period_rsi = Fsettings['Период RSI'] or 14
local period_k1 = Fsettings['Период K Stochastic(1)'] or 8
local period_d1 = Fsettings['Период D Stochastic(1)'] or 3
local st_ma_method1 = Fsettings['Метод MA Stochastic(1)'] or 'SMA'
local period_k2 = Fsettings['Период K Stochastic(2)'] or 14
local period_d2 = Fsettings['Период D Stochastic(2)'] or 3
local st_ma_method2 = Fsettings['Метод MA Stochastic(2)'] or 'SMA'
local period_q_smi = Fsettings['Период Q SMI'] or 10
local period_r_smi = Fsettings['Период R SMI'] or 3
local period_s_smi = Fsettings['Период S SMI'] or 3
local smi_ma_method = Fsettings['Метод MA SMI'] or 'EMA'
local show_hull = Fsettings['Показывать линию Hull'] == 1
local period_hull = Fsettings['Период Hull']
local divisor_hull = Fsettings['Делитель Hull']
local data_type = Fsettings['Тип данных'] or 'Close'
local show_hist = Fsettings['Показывать гистограмму'] or 'STOCH2'
for val in string.gmatch(Fsettings['Показывать гистограмму'] or 'STOCH2', "([^;]+)") do
if (val:find('*')) then
show_hist = val:gsub('*', ''):gsub("^%s*(.-)%s*$", "%1")
break
end
end
myLog(' show_hist', show_hist, Fsettings['Показывать гистограмму'])
error_log = {}
local out = {}
local begin_index
local fSTOCH1, fSTOCH2, fRSI, fSMI, fHULL
local hist
local smi, hull
local threshold = 50
local limit = 0
local trend
return function(index)
local status, res = pcall(function()
out = {}
if not maLib then return end
if not fSTOCH1 or index == begin_index then
begin_index = index
hist = {}
fRSI = maLib.new({method = 'RSI', period = period_rsi, data_type = data_type}, ds)
hist['RSI'] = fRSI(index)
fSTOCH1 = maLib.new({method = 'STOCH', period = period_k1, shift = period_d1, ma_method = st_ma_method1, data_type = data_type}, ds)
hist['STOCH1'] = fSTOCH1(index)
fSTOCH2 = maLib.new({method = 'STOCH', period = period_k2, shift = period_d2, ma_method = st_ma_method2, data_type = data_type}, ds)
hist['STOCH2'] = fSTOCH2(index)
fSMI = maLib.new({method = 'SMI', period_q = period_q_smi, period_r = period_r_smi, period_s = period_s_smi, ma_method = smi_ma_method, divisor = 1, data_type = data_type}, ds)
smi = fSMI(index)
if show_hull then
fHULL = maLib.new({method = 'HMA', period = period_hull, divisor = divisor_hull, data_type = data_type}, ds)
hull = fHULL(index)
end
trend = {}
trend[index] = 0
return
end
trend[index] = trend[index-1]
if not maLib.CheckIndex(index, ds) then return end
hist['RSI'][index] = fRSI(index)[index] - threshold
hist['STOCH1'][index] = fSTOCH1(index)[index] - threshold
hist['STOCH2'][index] = fSTOCH2(index)[index] - threshold
if (hist['RSI'][index]>limit and hist['STOCH1'][index]>limit and hist['STOCH2'][index]>limit) then
out[1] = hist[show_hist][index]
else
if (hist['RSI'][index]<limit and hist['STOCH1'][index]<limit and hist['STOCH2'][index]<limit) then
out[2] = hist[show_hist][index]
else
out[3] = hist[show_hist][index]
end
end
out[4] = fSMI(index)[index]
if show_hull then
fHULL(index)
if hull[index] < hull[index-1] then
out[6] = 0
else
out[5] = 0
end
end
if trend[index] <= 0 and hist[show_hist][index] > smi[index-1] then
out[7] = smi[index]
trend[index] = 1
end
if trend[index] >= 0 and hist[show_hist][index] < smi[index-1] then
out[8] = smi[index]
trend[index] = -1
end
-- myLog(tostring(index)..' '..os.date('%Y.%m.%d %H:%M', os.time(_G.T(index)))..' out', unpack(out, 1, lines))
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 unpack(out, 1, lines)
end
end
function _G.Init()
PlotLines = Algo(_G.Settings)
return lines
end
function _G.OnChangeSettings()
_G.Init()
end
function _G.OnCalculate(index)
return PlotLines(index)
end