Индикатор показывающий ближайшие горизонтальные уровни на QUIK
--[[
вопросы к автору: https://t.me/autotrade_ru
параметры:
Procent - процент зигзага
--]]
Settings={
Name="ZIG_levels_Template_v2",
Procent=2,
line=
{
{
Name = "cur1",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,0, 0)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,0, 0)
}
}
}
function Init()
y1 = nil
y2 = nil
x1 = 1
x2 = 1
lvs = {}
lvsval = {}
lvsi = {}
return 2
end
function getlevels(index)
uplvs = nil
dwlvs = nil
for i = 3, #lvsval do
if lvsval[i] > O(index) and lvsval[i-1] < O(index) then
uplvs = lvsval[i]
dwlvs = lvsval[i-1]
uplvsi = lvsi[i]
dwlvsi = lvsi[i-1]
if lvsval[i-2] > O(index) and lvsval[i-1] < O(index) and lvsval[i-2] < lvsval[i] then
uplvs = lvsval[i-2]
dwlvs = lvsval[i-1]
uplvsi = lvsi[i-2]
dwlvsi = lvsi[i-1]
end
end
if lvsval[i] < O(index) and lvsval[i-1] > O(index) then
uplvs = lvsval[i-1]
dwlvs = lvsval[i]
uplvsi = lvsi[i-1]
dwlvsi = lvsi[i]
if lvsval[i-2] < O(index) and lvsval[i-1] > O(index) and lvsval[i-2] > lvsval[i] then
uplvs = lvsval[i-1]
dwlvs = lvsval[i-2]
uplvsi = lvsi[i-1]
dwlvsi = lvsi[i-2]
end
end
end
return uplvs, dwlvs, uplvsi, dwlvsi
end
function OnCalculate(index)
de = Settings.Procent
vl = O(index)
if index == 1 then
y1 = vl
y2 = vl
lvs[index] = 0
else
lvs[index] = lvs[index-1]
if O(index) > y1*(1+de/100) and y1 < y2 then
x2 = x1
y2 = y1
x1 = index
y1 = O(index)
lvs[index] = lvs[index]+1
lvsval[lvs[index]] = y2
lvsi[lvs[index]] = x2
else
if O(index) > y1 and y1 >= y2
then
x1 = index
y1 = O(index)
end
end
if O(index) < y1*(1-de/100) and y1 > y2 then
x2 = x1
y2 = y1
x1 = index
y1 = O(index)
lvs[index] = lvs[index]+1
lvsval[lvs[index]] = y2
lvsi[lvs[index]] = x2
else
if O(index) < y1 and y1 <= y2
then
x1 = index
y1 = O(index)
end
end
end
if index == Size() then
uplvs, dwlvs, uplvsi, dwlvsi = getlevels(index)
for i = 1, index do
SetValue(i, 1, nil)
SetValue(i, 2, nil)
end
for i = uplvsi, index do
SetValue(i, 1, uplvs)
end
for i = dwlvsi, index do
SetValue(i, 2, dwlvs)
end
end
return uplvs, dwlvs
end