Индикатор наклонных каналов
--[[
вопросы к автору: https://t.me/autotrade_ru
параметры:
Procent - процент зигзага
--]]
Settings={
Name="ZIG_dec_levels_Template_v3",
Procent=2,
line=
{
{
Name = "cur1",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,0,0)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 1,
Color = RGB(0,0,255)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 1,
Color = RGB(255,0,0)
}
}
}
function Init()
n1 = {}
xy1 = {}
x1 = {}
y1 = {}
xx1 = {}
a1 = {}
b1 = {}
n2 = {}
xy2 = {}
x2 = {}
y2 = {}
xx2 = {}
a2 = {}
b2 = {}
return 3
end
function OnCalculate(index)
de = Settings.Procent
vl = O(index)
if index == 1 then
y10 = vl
y20 = vl
x10 = 1
x20 = 1
n1[index] = 0
xy1[index] = 0
x1[index] = 0
y1[index] = 0
xx1[index] = 0
a1[index] = 0
b1[index] = 0
n2[index] = 0
xy2[index] = 0
x2[index] = 0
y2[index] = 0
xx2[index] = 0
a2[index] = 0
b2[index] = 0
else
--[[ n1[index] = n1[index-1]
xy1[index] = xy1[index-1]
x1[index] = x1[index-1]
y1[index] = y1[index-1]
xx1[index] = xx1[index-1]
a1[index] = a1[index-1]
b1[index] = b1[index-1] --]]
if O(index) > y10*(1+de/100) and y10 < y20 then
n2[index] = n1[index]
xy2[index] = xy1[index]
x2[index] = x1[index]
y2[index] = y1[index]
xx2[index] = xx1[index]
a2[index] = a1[index]
b2[index] = b1[index]
x20 = x10
y20 = y10
x10 = index
y10 = O(index)
if x20 ~= nil then
n = x10 - x20
xy = 0
x = 0
y = 0
xx = 0
for i = x20+1, x10 do
xy = xy + i*O(i)
x = x + i
y = y + O(i)
xx = xx + i*i
end
n1[index] = n
xy1[index] = xy
x1[index] = x
y1[index] = y
xx1[index] = xx
if (n*xx - x*x) ~= 0 then
a = (n*xy - x*y)/(n*xx - x*x)
else
a = 0
end
b = (y - a*x)/n
a1[index] = a
b1[index] = b
end
else
if O(index) < y10*(1-de/100) and y10 > y20 then
n2[index] = n1[index]
xy2[index] = xy1[index]
x2[index] = x1[index]
y2[index] = y1[index]
xx2[index] = xx1[index]
a2[index] = a1[index]
b2[index] = b1[index]
x20 = x10
y20 = y10
x10 = index
y10 = O(index)
if x20 ~= nil then
n = x10 - x20
xy = 0
x = 0
y = 0
xx = 0
for i = x20+1, x10 do
xy = xy + i*O(i)
x = x + i
y = y + O(i)
xx = xx + i*i
end
n1[index] = n
xy1[index] = xy
x1[index] = x
y1[index] = y
xx1[index] = xx
if (n*xx - x*x) ~= 0 then
a = (n*xy - x*y)/(n*xx - x*x)
else
a = 0
end
b = (y - a*x)/n
a1[index] = a
b1[index] = b
end
else
if O(index) < y10 and y10 <= y20 and x10 ~= nil and n1[index-1] ~= nil
then
n = n1[index-1] + index - x10
xy = xy1[index-1]
x = x1[index-1]
y = y1[index-1]
xx = xx1[index-1]
for i = x10+1, index do
xy = xy + i*O(i)
x = x + i
y = y + O(i)
xx = xx + i*i
end
n1[index] = n
xy1[index] = xy
x1[index] = x
y1[index] = y
xx1[index] = xx
if (n*xx - x*x) ~= 0 then
a = (n*xy - x*y)/(n*xx - x*x)
else
a = 0
end
b = (y - a*x)/n
a1[index] = a
b1[index] = b
x10 = index
y10 = O(index)
else
if O(index) > y10 and y10 >= y20 and x10 ~= nil and n1[index-1] ~= nil
then
n = n1[index-1] + index - x10
xy = xy1[index-1]
x = x1[index-1]
y = y1[index-1]
xx = xx1[index-1]
for i = x10+1, index do
xy = xy + i*O(i)
x = x + i
y = y + O(i)
xx = xx + i*i
end
n1[index] = n
xy1[index] = xy
x1[index] = x
y1[index] = y
xx1[index] = xx
if (n*xx - x*x) ~= 0 then
a = (n*xy - x*y)/(n*xx - x*x)
else
a = 0
end
b = (y - a*x)/n
a1[index] = a
b1[index] = b
x10 = index
y10 = O(index)
else
n1[index] = n1[index-1]
xy1[index] = xy1[index-1]
x1[index] = x1[index-1]
y1[index] = y1[index-1]
xx1[index] = xx1[index-1]
a1[index] = a1[index-1]
b1[index] = b1[index-1]
end
end
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
upv = 0
dwv = 0
for i = x20, x10 do
val = a1[index]*i + b1[index]
if O(i) - val > upv then
upv = O(i) - val
end
if val - O(i) > dwv then
dwv = val - O(i)
end
end
val = nil
if a1[index] ~= nil and b1[index] ~= nil then
val = a1[index]*index + b1[index]
end
if upv ~= nil then
upv = val + upv
end
if dwv ~= nil then
dwv = val - dwv
end
return nil, upv, dwv
end