-
Notifications
You must be signed in to change notification settings - Fork 64
/
kReg.lua
480 lines (428 loc) · 11.9 KB
/
kReg.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
--[[
nick-h@yandex.ru
https://github.com/nick-nh/qlua
Nadaraya-Watson kernel regression
]]
local logFile = nil
-- logFile = io.open(_G.getWorkingFolder().."\\LuaIndicators\\kReg.txt", "w")
_G.unpack = rawget(table, "unpack") or _G.unpack
local math_pow = function(x, y) return x^y end
local message = _G['message']
local SetValue = _G['SetValue']
local Size = _G['Size']
local CandleExist = _G['CandleExist']
local RGB = _G['RGB']
local TYPE_LINE = _G['TYPE_LINE']
local TYPE_DASHLINE = _G['TYPE_DASHLINE']
local TYPE_BAR = _G['TYPET_BAR']
local TYPE_TRIANGLE_UP = _G['TYPE_TRIANGLE_UP']
local TYPE_TRIANGLE_DOWN = _G['TYPE_TRIANGLE_DOWN']
local isDark = _G.isDarkTheme()
local line_color = isDark and RGB(240, 240, 240) or RGB(0, 0, 0)
local os_time = os.time
local math_abs = math.abs
local math_exp = math.exp
local O = _G['O']
local C = _G['C']
local H = _G['H']
local L = _G['L']
_G.Settings =
{
Name = "*kReg",
['Период'] = 500,
['Окно оценки'] = 8,
['Отклонение1'] = 3.0,
['Отклонение2'] = 0.0,
['Отклонение3'] = 0.0,
['Отклонение4'] = 0.0,
['Вариант расчета ядра'] = 1, -- 1- nw kernel, 2 - Gaussian, 3 quartic_biweight, 4 Epanechnikov
['Сдвиг бар'] = 0,
['Выделять цветом'] = 1,
['Вариант данных'] = 'C' -- C, O, H, L, M, T, W
}
local lines_set =
{
{
Name = "iReg",
Color = line_color,
Type = TYPE_LINE,
Width = 1
},
{
Name = "+iReg1",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 1
},
{
Name = "-iReg1",
Color = RGB(192, 0, 0),
Type = TYPE_DASHLINE,
Width = 1
},
{
Name = "+iReg2",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 1
},
{
Name = "-iReg2",
Color = RGB(192, 0, 0),
Type = TYPE_DASHLINE,
Width = 1
},
{
Name = "+iReg3",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 1
},
{
Name = "-iReg3",
Color = RGB(192, 0, 0),
Type = TYPE_DASHLINE,
Width = 1
},
{
Name = "+iReg4",
Color = RGB(0, 128, 0),
Type = TYPE_LINE,
Width = 1
},
{
Name = "-iReg4",
Color = RGB(192, 0, 0),
Type = TYPE_DASHLINE,
Width = 1
},
--10
{
Name = "RegPredictPoint",
Color = line_color,
Type = _G.TYPE_POINT,
Width = 3
},
--11
{
Name = "change dir up",
Type = TYPE_TRIANGLE_UP,
Width = 3,
Color = RGB(89,213, 107)
},
--12
{
Name = "change dir dw",
Type = TYPE_TRIANGLE_DOWN,
Width = 3,
Color = RGB(255, 58, 0)
},
--13
{
Name = "reg up",
Type = TYPE_BAR,
Width = 1,
Color = RGB(89,213, 107)
},
--14
{
Name = "reg dw",
Type = TYPE_BAR,
Width = 1,
Color = RGB(255, 58, 0)
}
}
----------------------------------------------------------
local lines = #lines_set
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
local PlotLines = function(index) return index end
local error_log = {}
----------------------------------------------------------
----------------------------------------------------------
--kernel regression
local k_functor = {}
--nw kernel
k_functor[1] = function(u, h)
local b2h = h*h*2
local k = math_exp(-math_pow(u, 2)/b2h)
return k
end
--gaussian_kernel
k_functor[2] = function(u, h, c, scale)
--2.506628274631000 -- approx sqrt(2*M_PI)
u = u/h
c = c or 0.001
local k = math_exp(-0.5 * math_pow(u, 2))/(2.506628274631000)
if (k < c) then
return 0.0
end
return scale and k/h or k
end
--epanechnikov_kernel
k_functor[3] = function(u, h, scale)
u = u/h
local k = 0.0
if (math_abs(u) < 1) then
k = 0.75 * (1.0 - math_pow(u, 2))
end
return scale and k/h or k
end
--quartic_biweight_kernel
k_functor[4] = function(u, h, scale)
u = u/h
local k = 0.0
if (math_abs(u) < 1) then
k = 0.9375 * math_pow(1.0 - math_pow(u, 2), 2)
end
return scale and k/h or k
end
--data array of x and Y
--[[
data = {
{x = 25, y = 75},
{x = 27, y = 70},
{x = 30, y = 78},
{x = 33, y = 90},
{x = 40, y = 100},
{x = 50, y = 120},
{x = 52, y = 110},
{x = 54, y = 106},
{x = 60, y = 120}
}
]]
local function kernel_regression(data, lookback, k_type)
local kernel_evaluator = k_functor[k_type or 1] or k_functor[1]
if not kernel_evaluator then return end
if not data or #data == 0 then return end
local size = #data
local se = 0
local y = {}
local sum_w, sum_wy
for i = 1, size do
sum_w = 0
sum_wy = 0
for j = 1, size do
local k = kernel_evaluator((data[i].x or i)-(data[j].x or j), lookback)
sum_wy = sum_wy + data[j].y*k
sum_w = sum_w + k
end
y[i] = sum_wy/sum_w
se = se + math_abs(data[i].y - y[i])
end
return y, se/size
end
local function Reg(Fsettings)
Fsettings = (Fsettings or {})
local period = Fsettings['Период'] or 182
local lookback = Fsettings['Окно оценки'] or 50
local kstd1 = Fsettings['Отклонение1'] or 1
local kstd2 = Fsettings['Отклонение2'] or 2
local kstd3 = Fsettings['Отклонение3'] or 3
local kstd4 = Fsettings['Отклонение4'] or 4
local barsshift = Fsettings['Сдвиг бар'] or 0
local data_type = Fsettings['Вариант данных'] or 'C'
local k_type = Fsettings['Вариант расчета ядра'] or 1
local est = {}
local sq = 0
local alpha = 0
local calculated_buffer={}
local predict_index
error_log = {}
local out = {}
local data
local df = {}
df['C'] = function(i) return C(i) end
df['H'] = function(i) return H(i) end
df['L'] = function(i) return L(i) end
df['O'] = function(i) return O(i) end
df['M'] = function(i) return (H(i) + L(i))/2 end
df['T'] = function(i) return (H(i) + L(i) + O(i))/3 end
df['W'] = function(i) return (H(i) + L(i) + O(i) + C(i))/4 end
local trend
local last_cal_bar
local start_index, c_index
local function get_y(index)
return df[data_type](index)
end
return function(index)
local status, res = pcall(function()
if index == 1 then
out = {}
calculated_buffer = {}
est = {}
est[1] = 0
data = {}
last_cal_bar = index
start_index = Size() - barsshift
if barsshift ~= 0 then
predict_index = Size() - barsshift
end
trend = {}
trend[index] = 0
return
end
if index < period then return nil end
if calculated_buffer[index] ~= nil then
return
end
trend[index] = trend[index - 1]
SetValue(index-period-barsshift, 1, nil)
SetValue(index-period-barsshift, 2, nil)
SetValue(index-period-barsshift, 3, nil)
SetValue(index-period-barsshift, 4, nil)
SetValue(index-period-barsshift, 5, nil)
SetValue(index-period-barsshift, 6, nil)
SetValue(index-period-barsshift, 7, nil)
SetValue(index-period-barsshift, 8, nil)
SetValue(index-period-barsshift, 9, nil)
SetValue(index-period-barsshift-1, 1, nil)
SetValue(index-period-barsshift-1, 2, nil)
SetValue(index-period-barsshift-1, 3, nil)
SetValue(index-period-barsshift-1, 4, nil)
SetValue(index-period-barsshift-1, 5, nil)
SetValue(index-period-barsshift-1, 6, nil)
SetValue(index-period-barsshift-1, 7, nil)
SetValue(index-period-barsshift-1, 8, nil)
SetValue(index-period-barsshift-1, 9, nil)
--Calc
out = {}
if not CandleExist(index) or index <= period then
return
end
c_index = index-1
if index < start_index or last_cal_bar == c_index then return nil end
if not predict_index or index <= predict_index then
if not data[1] then
local i = 0
local j = period
while not data[1] and i < c_index do
data[j] = {y = get_y(c_index-i)}
i = i + 1
if data[j].y then
j = j - 1
end
end
end
if last_cal_bar ~= c_index and data[1] then
for kk = last_cal_bar + 1, c_index do
table.remove(data, 1)
data[period] = {y = get_y(kk)}
end
end
last_cal_bar = c_index
est, sq = kernel_regression(data, lookback, k_type)
alpha = (est[#est] - est[#est-1])/(get_y(c_index-1) - est[#est-1])
if predict_index and index == predict_index-period+1 then
out[10] = est[#est]
end
if index == predict_index then
out[10] = est[#est]
end
local h_index, h_up, h_dw, new_up, old_up, new_dw, old_dw
for n=1, period do
h_index = index+n-period
SetValue(h_index, 1, est[n])
if kstd1 > 0 then
SetValue(h_index, 2, est[n]+sq*kstd1)
SetValue(h_index, 3, est[n]-sq*kstd1)
end
if kstd2 > 0 then
SetValue(h_index, 4, est[n]+sq*kstd2)
SetValue(h_index, 5, est[n]-sq*kstd2)
end
if kstd3 > 0 then
SetValue(h_index, 6, est[n]+sq*kstd3)
SetValue(h_index, 7, est[n]-sq*kstd3)
end
if kstd4 > 0 then
SetValue(h_index, 8, est[n]+sq*kstd4)
SetValue(h_index, 9, est[n]-sq*kstd4)
end
if n>1 then
h_up = est[n-1]+sq*kstd1
h_dw = est[n-1]-sq*kstd1
new_up = C(h_index)
old_up = C(h_index-1)
new_dw = C(h_index)
old_dw = C(h_index-1)
if new_dw < h_dw and old_dw >= h_dw then
SetValue(h_index+1, 11, O(h_index+1))
end
if new_up > h_up and old_up <= h_up then
SetValue(h_index+1, 12, O(h_index+1))
end
end
end
else
est[#est+1] = est[#est] + alpha*(get_y(c_index-1) - est[#est])
-- est[#est+1] = alpha*get_y(c_index) + (1 - alpha)*(est[#est] or 0)
end
out[1] = est[#est]
if kstd1 > 0 then
out[2] = out[1]+sq*kstd1
out[3] = out[1]-sq*kstd1
end
if kstd2 > 0 then
out[4] = out[1]+sq*kstd2
out[5] = out[1]-sq*kstd2
end
if kstd3 > 0 then
out[6] = out[1]+sq*kstd3
out[7] = out[1]-sq*kstd3
end
if kstd4 > 0 then
out[8] = out[1]+sq*kstd4
out[9] = out[1]-sq*kstd4
end
if C(index-1) < out[3] and C(index-2) >= out[3] then
out[11] = O(index) or nil
end
if C(index-1) > out[2] and C(index-2) <= out[2] then
out[12] = O(index) or nil
end
calculated_buffer[index] = true
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()
_G.Settings.line = {}
for i, line in ipairs(lines_set) do
_G.Settings.line[i] = line
end
lines = #lines_set
PlotLines = Reg(_G.Settings)
return lines
end
function _G.OnChangeSettings()
_G.Init()
end
function _G.OnCalculate(index)
return PlotLines(index)
end