-
Notifications
You must be signed in to change notification settings - Fork 64
/
vsa.lua
462 lines (389 loc) · 12.2 KB
/
vsa.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
--[[
nick-h@yandex.ru
https://github.com/nick-nh/qlua
VSA
]]
local sound_file_name = "c:\\windows\\media\\Alarm03.wav"
local w32 = (function() local status, res = pcall(function() return require("w32"); end) if status then return res end; end)()
local logfile = nil
-- logfile = io.open(_G.getWorkingFolder().."\\LuaIndicators\\volume.txt", "w")
local message = _G['message']
local CandleExist = _G['CandleExist']
local O = _G['O']
local C = _G['C']
local H = _G['H']
local L = _G['L']
local V = _G['V']
local T = _G['T']
local Size = _G['Size']
local floor = math.floor
local ceil = math.ceil
local math_pow = function(x, y) return x^y end
local math_max = math.max
local math_abs = math.abs
local math_min = math.min
local os_time = os.time
local max_errors = 10
local PlotLines = function() end
local min_price_step = 1
local scale = 1
local RGB = _G['RGB']
local TYPE_LINE = _G['TYPE_LINE']
local TYPE_HISTOGRAM = _G['TYPE_HISTOGRAM']
local isDark = _G.isDarkTheme()
local line_color = isDark and RGB(240, 240, 240) or RGB(0, 0, 0)
local intervals_rep = {}
intervals_rep[0] = 'TICK'
intervals_rep[1] = 'M1'
intervals_rep[2] = 'M2'
intervals_rep[3] = 'M3'
intervals_rep[4] = 'M4'
intervals_rep[5] = 'M5'
intervals_rep[6] = 'M6'
intervals_rep[10] = 'M10'
intervals_rep[15] = 'M15'
intervals_rep[30] = 'M30'
intervals_rep[60] = 'H1'
intervals_rep[120] = 'H2'
intervals_rep[240] = 'H4'
intervals_rep[1440] = 'D1'
intervals_rep[10080] = 'W'
intervals_rep[23200] = 'M'
_G.Settings =
{
Name = "*VSA",
lookBack = 21,
volumeFactor = 1.0,
useVolumeMA = 1,
volMAPeriod = 21,
volMAKoeff = 1.0,
useChunk = 1,
chunkMA_Factor = 1.0,
useCumulativeDelta = 0,
useClosePrice = 1,
trigger_vol = 0,
line=
{
{
Name = "VolEMA",
Color = line_color,
Type = TYPE_LINE,
Width = 2
}
,
{
Name = "Delta",
Color = line_color,
Type = TYPE_LINE,
Width = 2
}
,
{
Name = "Neutral",
Color = RGB(0,128, 255),
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "Climax High",
Color = RGB(255, 0, 0),
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "Low",
Color = RGB(150, 150, 150),
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "Churn",
Color = RGB(0,219, 108),
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "Climax Low",
Color = line_color,
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "Climax Churn",
Color = RGB(255,0, 255),
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "ChunkUp",
Color = RGB(0,219, 108),
Type = TYPE_HISTOGRAM,
Width = 3
}
,
{
Name = "ChunkDown",
Color = RGB(255, 0, 0),
Type = TYPE_HISTOGRAM,
Width = 3
}
}
}
local lines = #_G.Settings.line
local function round(num, idp)
if num then
local mult = 10^(idp or 0)
if num >= 0 then
return floor(num * mult + 0.5) / mult
else
return ceil(num * mult - 0.5) / mult
end
else
return num
end
end
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 function PaySoundFile()
if not w32 then return end
w32.mciSendString("CLOSE QUIK_MP3")
w32.mciSendString("OPEN \"" .. sound_file_name .. "\" TYPE MpegVideo ALIAS QUIK_MP3")
w32.mciSendString("PLAY QUIK_MP3")
end
----------------------------------------------------------
local function CalculateVolume()
local cache_volEMA
local DeltaBuffer = {}
local DeltaCalculations = {}
local cache_DeltaEMA = {}
local error_log = {}
local errors = 0
local max_errors_reach
local p_index
local l_index
local ds_info
local trigger_index
return function(index, FSettings)
local status, res1, res2, res3, res4, res5, res6, res7, res8, res9, res10 = pcall(function()
FSettings = FSettings or {}
local lookBack = FSettings.lookBack or 21
local volMAPeriod = FSettings.volMAPeriod or 21
local volMAKoeff = FSettings.volMAKoeff or 1
local volumeFactor = FSettings.volumeFactor or 1
local useVolumeMA = FSettings.useVolumeMA or 1
local useChunk = FSettings.useChunk or 0
local chunkMA_Factor = FSettings.chunkMA_Factor or 1
local useCumulativeDelta = FSettings.useCumulativeDelta or 0
local useClosePrice = FSettings.useClosePrice or 1
local trigger_vol = FSettings.trigger_vol or 1
local DeltaFactor = 2
local k = 2/(volMAPeriod + 1)
if index == 1 or cache_volEMA == nil then
ds_info = _G.getDataSourceInfo()
l_index = index
cache_volEMA = {}
DeltaBuffer = {}
DeltaCalculations = {}
cache_DeltaEMA = {}
if CandleExist(index) then
DeltaBuffer[index] = math_pow(V(index), volumeFactor)
DeltaCalculations[index] = math_pow(V(index), volumeFactor)
cache_volEMA[index] = math_pow(V(index), volumeFactor)
cache_DeltaEMA[index] = math_pow(V(index), volumeFactor)
else
cache_volEMA[index] = 0
DeltaBuffer[index] = 0
DeltaCalculations[index] = 0
cache_DeltaEMA[index] = 0
end
return nil
end
cache_volEMA[index] = cache_volEMA[index-1] or 0
DeltaBuffer[index] = DeltaBuffer[index-1] or 0
DeltaCalculations[index] = DeltaCalculations[index-1] or 0
cache_DeltaEMA[index] = cache_DeltaEMA[index-1] or 0
if not CandleExist(index) then
return nil
end
if index ~= l_index then
p_index = l_index
cache_volEMA[l_index - lookBack] = nil
DeltaBuffer[l_index - lookBack] = nil
DeltaCalculations[l_index - lookBack] = nil
cache_DeltaEMA[l_index - lookBack] = nil
end
local priceMax = H(index)
local priceMin = L(index)
if useClosePrice == 1 and useChunk~=1 then
priceMax = math_max(O(index), C(index))
priceMin = math_min(O(index), C(index))
end
if priceMax == priceMin then priceMax = priceMin + min_price_step end
local vol = V(index)
if useChunk == 1 then
vol = round(V(index)*min_price_step/math_abs(priceMax-priceMin), scale)
end
cache_volEMA[index] = k*math_pow(vol, volumeFactor)+(1-k)*cache_volEMA[index-1]
local extraVolume = math_pow(V(index), volumeFactor)
if C(index) > C(p_index) then
DeltaBuffer[index] = extraVolume
else
DeltaBuffer[index] = -1*extraVolume
end
if useCumulativeDelta then
DeltaBuffer[index] = DeltaBuffer[index] / DeltaFactor
else
DeltaBuffer[index] = DeltaBuffer[index] / DeltaFactor
end
DeltaCalculations[index] = DeltaBuffer[index - 1] + extraVolume
cache_DeltaEMA[index] = k*DeltaCalculations[index] + (1-k)*cache_DeltaEMA[index-1]
l_index = index
if index < lookBack then
return nil
end
local out = math_pow(vol, volumeFactor)
local outVolEMA = volMAKoeff*cache_volEMA[index]
local outDeltaEMA = DeltaCalculations[index]
if useVolumeMA==0 then
outVolEMA = nil
end
if useCumulativeDelta==0 then
outDeltaEMA = nil
end
if trigger_vol ~= 0 and index == Size() and trigger_index ~= index and V(index) > trigger_vol then
trigger_index = index
PaySoundFile()
message(tostring(ds_info.class_code)..'|'..tostring(ds_info.sec_code)..', '..tostring(intervals_rep[ds_info.interval])..': '..os.date('%Y.%m.%d %H:%M', os.time(T(index)))..' Объем выше: '..tostring(trigger_vol))
end
if useChunk == 1 then
if vol > chunkMA_Factor*cache_volEMA[index] and C(index) > (priceMax + priceMin)/2 then
return outVolEMA, outDeltaEMA, nil, nil, nil, nil, nil, nil, out, nil --Chunk Up
elseif vol > chunkMA_Factor*cache_volEMA[index] and C(index) < (priceMax + priceMin)/2 then
return outVolEMA, outDeltaEMA, nil, nil, nil, nil, nil, nil, nil, out --Chunk Down
end
return outVolEMA, outDeltaEMA, nil, nil, out, nil, nil, nil, nil, nil --low
end
local volClimaxCurrent = V(index) * (priceMax - priceMin)
local volChurnCurrent = 0
if volClimaxCurrent > 0 then
volChurnCurrent = V(index) / (priceMax - priceMin)
end
local volClimaxLocal = 0
local volChurnLocal = 0
local v_H = 0
local v_L = 0
local min_index = 0
local priceMinLocal
local priceMaxLocal
local climax
local churn
for n = index - lookBack + 1, index do
if CandleExist(n) then
priceMinLocal = L(n)
priceMaxLocal = H(n)
if useClosePrice == 1 then
priceMinLocal = math_min(O(n), C(n))
priceMaxLocal = math_max(O(n), C(n))
end
climax = V(n) * (priceMaxLocal - priceMinLocal)
-- Previous maximal price range can be found here
if climax >= volClimaxLocal
then
volClimaxLocal = climax;
end
-- Previous consolidation can be found here
if climax > 0
then
churn = V(n) / (priceMaxLocal - priceMinLocal)
if churn >= volChurnLocal
then
volChurnLocal = churn;
end
end
if v_H < V(n) then v_H = V(n) end
if v_L > V(n) then
v_L = V(n)
min_index = n
end
end
end
-- When volume is higher than all previous and price is going down - start or end of the down trend
if (volClimaxCurrent == volClimaxLocal and C(index) < (priceMax + priceMin) / 2)
then
return outVolEMA, outDeltaEMA, nil, nil, nil, nil, out, nil, nil, nil --Climax Low
end
-- When volume is extra high and price is not changing - absolute consolidation or fast accummulation / distribution
if (volClimaxCurrent == volClimaxLocal and volChurnCurrent == volChurnLocal)
then
return outVolEMA, outDeltaEMA, nil, nil, nil, nil, nil, out, nil, nil --Climax Churn
end
-- When volume is higher than all previous and price is going up - start or end of the up trend
if (volClimaxCurrent == volClimaxLocal and C(index) > (priceMax + priceMin) / 2)
then
return outVolEMA, outDeltaEMA, nil, out, nil, nil, nil, nil, nil, nil --Climax High
end
-- When volume is equal to one seen before mark it as accummulation / distribution - profit is taken
if (volChurnCurrent == volChurnLocal)
then
return outVolEMA, outDeltaEMA, nil, nil, nil, out, nil, nil, nil, nil--Churn
end
if (index == min_index)
then
return outVolEMA, outDeltaEMA, nil, nil, out, nil, nil, nil, nil, nil --Low
end
return outVolEMA, outDeltaEMA, out, nil, nil, nil, nil, nil, nil, nil --neutral
end)
if not status then
errors = errors + 1
if errors > max_errors and not max_errors_reach then
message(_G.Settings.Name ..': Слишком много ошибок при работе индикатора.')
max_errors_reach = true
end
if not error_log[tostring(res1)] then
error_log[tostring(res1)] = true
myLog('Error CalcFunc: '..tostring(res1))
message('Error CalcFunc: '..tostring(res1))
end
return nil
end
return res1, res2, res3, res4, res5, res6, res7, res8, res9, res10
end
end
function _G.OnCalculate(index)
if index == 1 then
local DSInfo = _G.getDataSourceInfo()
min_price_step = _G.getParamEx(DSInfo.class_code, DSInfo.sec_code, "SEC_PRICE_STEP").param_value
scale = _G.getSecurityInfo(DSInfo.class_code, DSInfo.sec_code).scale
end
return PlotLines(index, _G.Settings)
end
function _G.Init()
PlotLines = CalculateVolume()
return lines
end
function _G.OnChangeSettings()
_G.Init()
end
function _G.OnDestroy()
if logfile then logfile:close() end
end