-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathVolatilityCycle.lua
More file actions
137 lines (113 loc) · 3.77 KB
/
VolatilityCycle.lua
File metadata and controls
137 lines (113 loc) · 3.77 KB
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
--[[
nick-h@yandex.ru
https://github.com/nick-nh/qlua
Volatility Cycle
]]
_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\\VolatlityCycle.txt", "w")
local message = _G['message']
local RGB = _G['RGB']
local isDark = _G.isDarkTheme()
local zero_color = isDark and RGB(123, 123, 123) or RGB(70, 70, 70)
local line_color = isDark and RGB(255, 193, 193) or RGB(20, 128, 255)
_G.Settings= {
Name = "*Volatility Cycle",
data_type = 'Close',
str_period = 20, -- Период расчета
atr_period = 20, -- Период расчета
low = 0.75, -- Уровень сжатия волатильности
high = 3, -- Уровень сильной волатильности
line = {
{
Name = 'low',
Color = zero_color,
Type = _G['TYPE_LINE'],
Width = 1
},
{
Name = 'high',
Color = zero_color,
Type = _G['TYPE_LINE'],
Width = 1
},
{
Name = 'vol',
Color = line_color,
Type = _G['TYPE_LINE'],
Width = 2
}
}
}
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(log_tostring(...).."\n");
logFile:flush();
end
local function Algo(Fsettings, ds)
Fsettings = (Fsettings or {})
local data_type = (Fsettings.data_type or "Close")
local std_period = (Fsettings.std_period or 20)
local atr_period = (Fsettings.atr_period or 20)
local round = (Fsettings.round or "off")
local scale = (Fsettings.scale or 0)
local high = Fsettings.high or 3
local low = Fsettings.low or 0.75
error_log = {}
local fSTD, sd
local fATR, atr
local out
local begin_index
return function (index)
local status, res = pcall(function()
out = nil
if fSTD == nil or index == begin_index then
begin_index = index
fSTD = maLib.new({method = 'SD', period = std_period, ma_method = 'SMA', not_shifted = true, data_type = data_type, round = round, scale = scale}, ds)
fSTD(index)
fATR = maLib.new({method = 'ATR', period = atr_period, round = round, scale = scale}, ds)
return
end
sd = fSTD(index)[index]
atr = fATR(index)[index]
if atr and sd then
out = sd/atr
-- myLog(index, os.date('%Y.%m.%d %H:%M', os.time(_G.T(index))), 'sd', sd, 'atr', atr, 'out', out)
end
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 high, low, out
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