forked from nick-nh/qlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macdRenkoATR.lua
220 lines (188 loc) · 8.4 KB
/
macdRenkoATR.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
--[[
https://github.com/nick-nh/qlua
MACD on Adaptive Renko Bars.
]]
_G.load = _G.loadfile or _G.load
local maLib = load(_G.getWorkingFolder().."\\Luaindicators\\maLib.lua")()
local logFile = nil
-- logFile = io.open(_G.getWorkingFolder().."\\LuaIndicators\\macdRenkoATR.txt", "w")
local message = _G['message']
local RGB = _G['RGB']
local TYPE_HISTOGRAM = _G['TYPE_HISTOGRAM']
local TYPE_LINE = _G['TYPE_LINE']
local math_max = math.max
local isDark = _G.isDarkTheme()
local line_color = isDark and RGB(240, 240, 240) or RGB(0, 0, 0)
_G.Settings= {
Name = "*macdRenkoATR",
short_period = 12, -- Период расчета короткой ma
long_period = 26, -- Период расчета длинной ma
method = 'EMA', -- Метод расчета ma
signal_period = 9, -- Период расчета сигнальной ma
signal_method = 'SMA', -- Метод расчета сигнальной ma
percent = 'ON', -- Метод расчета ma
period = 28, -- Период расчета ATR
k = 2.8, -- размер скользящего фильтра, используемый при вычислении размера блока от величины ATR как k*ATR
data_type = 1, -- 0 - Close; 1 - High|Low
br_size = 0, -- Фиксированный размер шага. Если задан, то строится по указанному размеру (в пунктах)
recalc_brick = 1, -- Пересчитывать размер блока каждый период-бар
shift_limit = 1, -- Сдвигать границу по пересчитанному размеру блока
min_recalc_brick = 0, -- Минимизировать размер блока при пересчете
--Для установки значения, необходимо поставить * перед выбранным вариантом.
brickType = '*ATR; Std; Fix', -- Тип расчета Renko; ATR; Std - стандартное отклонение; Fix - фиксированный размер, заданный в br_size
std_ma_method = 'SMA',
line = {
{
Name = "Up",
Color = RGB(0, 128, 128),
Type = TYPE_HISTOGRAM,
Width = 2
},
{
Name = "Down",
Color = RGB(255, 128, 0),
Type = TYPE_HISTOGRAM,
Width = 2
},
{
Name = 'Signal',
Color = line_color,
Type = TYPE_LINE,
Width = 1
}
}
}
local PlotLines = function() 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(log_tostring(...).."\n");
logFile:flush();
end
--Adaptive Renko ATR based
local function Algo(Fsettings, ds)
Fsettings = (Fsettings or {})
local short_period = Fsettings.short_period or 12
local long_period = Fsettings.long_period or 26
local method = (Fsettings.method or "EMA")
local signal_method = (Fsettings.signal_method or "SMA")
local signal_period = (Fsettings.signal_period or 9)
local percent = (Fsettings.percent or 'ON'):upper()
local save_bars = (Fsettings.save_bars or math_max(long_period, short_period, signal_period))
error_log = {}
local out1, out2, out3
local l_index
local sfMA
local lfMA
local fMACD_MA
local sma_data = {}
local lma_data = {}
local t_MACD = {}
local last_delta = 1
local fRenko
local c_rbars
local rbars
local settings = {}
settings.method = 'RENKO'
settings.br_size = (Fsettings.br_size or 0)
settings.period = (Fsettings.period or 0)
settings.data_type = (Fsettings.data_type or 0)
settings.recalc_brick = (Fsettings.recalc_brick or 0)
settings.min_recalc_brick = (Fsettings.min_recalc_brick or 0)
settings.shift_limit = (Fsettings.shift_limit or 0)
settings.std_ma_method = (Fsettings.std_ma_method or 'SMA')
settings.k = (Fsettings.k or 1)
settings.get_bars = true
local brickType = (Fsettings.brickType or 'Std')
for val in string.gmatch(brickType or '*ATR', "([^;]+)") do
if (val:find('*')) then
settings.brickType = val:gsub('*', ''):gsub("^%s*(.-)%s*$", "%1")
break
end
end
return function (index)
local status, res = pcall(function()
out1, out2, out3 = nil, nil, nil
if fRenko == nil or index == 1 then
local ds_info = _G.getDataSourceInfo()
settings.scale = (tonumber(_G.getParamEx(ds_info.class_code, ds_info.sec_code,"SEC_SCALE").param_value) or 0)
fRenko, rbars = maLib.new(settings, ds)
fRenko(index)
c_rbars = 1
l_index = index
sfMA = maLib.new({period = short_period, method = method}, rbars)
sma_data[c_rbars] = sfMA(index)[c_rbars]
lfMA = maLib.new({period = long_period, method = method}, rbars)
lma_data[c_rbars] = lfMA(index)[c_rbars]
t_MACD[index] = 0
fMACD_MA = maLib.new({period = signal_period, method = signal_method, data_type = "Any"}, t_MACD)
return
end
t_MACD[index] = t_MACD[index-1] or 0
local ds_size = maLib.dsSize(settings.data_type, ds)
local calc_bar = (index == ds_size and index - 1 or index)
if calc_bar ~= l_index then
l_index = calc_bar
fRenko(calc_bar)
-- myLog(index, os.date('%Y.%m.%d %H:%M', os.time(_G.T(index))), '#rbars', #rbars)
local new_bars = #rbars - c_rbars
if new_bars > 0 then
while c_rbars < #rbars-1 do
c_rbars = c_rbars + 1
sma_data[c_rbars] = sfMA(c_rbars)[c_rbars]
lma_data[c_rbars] = lfMA(c_rbars)[c_rbars]
end
c_rbars = #rbars
sma_data[c_rbars] = sfMA(c_rbars)[c_rbars]
lma_data[c_rbars] = lfMA(c_rbars)[c_rbars]
-- myLog(index, 'c_rbars', c_rbars, 'sma_data', sma_data[c_rbars], 'lma_data', lma_data[c_rbars])
end
end
if percent == 'OFF' then
t_MACD[index] = sma_data[c_rbars] - lma_data[c_rbars]
else
t_MACD[index] = lma_data[c_rbars] == 0 and 0 or (100*(sma_data[c_rbars] - lma_data[c_rbars])/lma_data[c_rbars])
end
out3 = fMACD_MA(index)[index]
if t_MACD[index] > t_MACD[index-1] or (t_MACD[index] == t_MACD[index-1] and last_delta == 1) then
out1 = t_MACD[index]
last_delta = 1
else
out2 = t_MACD[index]
last_delta = -1
end
t_MACD[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
end
return out1, out2, out3
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