Подкорректировал индикатор
--[[
индикатор:
канал на последнем зигзаге
индикатор строит канал по зигзагу, показывает слом тренда при выходе цены за границы канала
параметры:
Procent - величина зигзага в %
k - коэффизиент ширины канала
Ext - =0-канал стоится по средним =1-канал стоится по экстремуму
--]]
Settings=
{
Name = "Zigzag_channel_fast", -- название индикатора
Procent=5, -- величина зигзага в %
k=2, -- коэффизиент ширины канала
Ext=1, -- =0-канал стоится по средним =1-канал стоится по экстремуму
shif=0,
line=
{
{
Name = "zigzagline",
Type =TYPE_LINE,
Width = 1,
Color = RGB(120,90, 140)
},
{
Name = "cur1",
Type =TYPE_LINE,
Width = 1,
Color = RGB(0,0, 0)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,200, 200)
},
{
Name = "cur3",
Type =TYPE_LINE,
Width = 2,
Color = RGB(200,200, 0)
},
{
Name = "cur4",
Type =TYPE_LINE,
Width = 1,
Color = RGB(0,0, 0)
},
{
Name = "cur5",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0,0, 255)
},
{
Name = "cur6",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0, 0)
},
{
Name = "cur7",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0, 0)
},
{
Name = "cur8",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0, 0)
},
{
Name = "cur9",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0, 0)
},
{
Name = "cur10",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0, 0)
},
{
Name = "cur11",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255,0, 0)
}
}
}
function Init()
y1 = nil
y2 = nil
y3 = nil
x1 = 1
x2 = 1
x3 = 1
oldindex = 1
a_01 = 0
b_01 = 0
a_02 = 0
b_02 = 0
vminus_01 = 0
vplus_01 = 0
vminus_02 = 0
vplus_02 = 0
vFrom_01 = 1
vTo_01 = 1
vFrom_02 = 1
vTo_02 = 1
vminus = 0
vplus = 0
vminus_n = 0
vplus_n = 0
ret = 0
last_ind = 1
v1=nil
v2=nil
v3=nil
v4=nil
v5=nil
v6=nil
v7=nil
v8=nil
v9=nil
v10=nil
return 10
end
function recalcline(num, index, rec)
if index == Size()-sh and rec == 0 then
for i = 1, index do
if m == 1 then
SetValue(i, 3, nil)
SetValue(i, 4, nil)
end
if m == 2 then
SetValue(i, 6, nil)
SetValue(i, 7, nil)
end
end
end
if index == Size()-sh or rec == 1 then
if num == 1 then
vFrom = 0 --[[ --]]
vTo = 0
if x3 < x2 then
vFrom = x3
vTo = x2
else
vFrom = x2
vTo = x3
end
end
if num == 2 then
vFrom = 0
vTo = 0
if x1 < x2 then
vFrom = x1
vTo = x2
else
vFrom = x2
vTo = x1
end
end
if vFrom < 1 then
vFrom = 1
end
a1 = 0
a2 = 0
a3 = 0
a4 = 0
n = 0
a = 0
b = 0
for i = vFrom, vTo do
n=n+1
a1 = a1+i*C(i)
a2 = a2+i
a3 = a3+C(i)
a4 = a4+i*i
end
if((n*a4 - a2*a2) ~= 0) then
-- if oldindex ~= Size() then
a = (n*a1 - a2*a3)/(n*a4 - a2*a2)
b = (a3 - a*a2)/n
vminus = 0
vplus = 0
vminus_n = 0
vplus_n = 0
for i = vFrom, index do
y = a*i + b
if i>0 then
if C(i) < y and i <= vTo then
if ex == 0 then
vminus = vminus + y - C(i)
vminus_n = vminus_n + 1
else
if i == vFrom then
vminus = 0
end
if y-C(i) > vminus then
vminus = y-C(i)
end
end
end
end
if i>0 then
if C(i) > y and i <= vTo then
if ex == 0 then
vplus = vplus + C(i) - y
vplus_n = vplus_n + 1
else
if i == vFrom then
vplus = 0
end
if C(i)-y > vplus then
vplus = C(i)-y
end
end
end
end
m = 0
if C(vTo) > C(vFrom) and y < C(vTo)+0.1*(C(vTo)-C(vFrom)) and y > C(vFrom)-0.1*(C(vTo)-C(vFrom)) then
m = 1
end
if C(vTo) < C(vFrom) and y < C(vFrom)+0.1*(C(vFrom)-C(vTo)) and y > C(vTo)-0.1*(C(vFrom)-C(vTo)) then
m = 1
end
if m == 1 then
--SetValue(i, 2, y)
end
end
--end
if ex == 0 then
if vminus_n ~= 0 then
vminus = kk*vminus/vminus_n
end
if vplus_n ~= 0 then
vplus = kk*vplus/vplus_n
end
end
end
if num == 1 then
a_01 = a
b_01 = b
vminus_01 = vminus
vplus_01 = vplus
vFrom_01 = vFrom
vTo_01 = vTo
end
if num == 2 then
a_02 = a
b_02 = b
vminus_02 = vminus
vplus_02 = vplus
vFrom_02 = vFrom
vTo_02 = vTo
end
return 1
end
return 0
end
function levels(x, y, num, index)
if y ~= nil and x ~= 1 then
for i = 1, index do
if i > x then
SetValue(i, num, y)
else
SetValue(i, num, nil)
end
end
end
end
function OnCalculate(index)
de = Settings.Procent
kk = Settings.k
ex = Settings.Ext
sh = Settings.shif
v1=nil
v2=nil
v3=nil
v4=nil
v5=nil
v6=nil
v7=nil
v8=nil
v9=nil
v10=nil
if index > Size()-sh then
--return nil, nil, nil, nil, nil, nil, nil
return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
end
vl = C(index)
if vl~=nil then
if index == 1 then
y1 = vl
y2 = vl
else
if C(index) > y1*(1+de/100) and y1 < y2 then
x3 = x2
x2 = x1
y3 = y2
y2 = y1
x1 = index
y1 = C(index)
ret = recalcline(1, index, 0)
ret = recalcline(2, index, 0)
levels(x1, y1, 8, index)
levels(x2, y2, 9, index)
levels(x3, y3, 10, index)
end
if C(index) > y1 and C(index) > y2 then
x1 = index
y1 = C(index)
end
if C(index) < y1*(1-de/100) and y1 > y2 then
x3 = x2
x2 = x1
y3 = y2
y2 = y1
x1 = index
y1 = C(index)
ret = recalcline(1, index, 0)
ret = recalcline(2, index, 0)
levels(x1, y1, 8, index)
levels(x2, y2, 9, index)
levels(x3, y3, 10, index)
end
if C(index) < y1 and C(index) < y2 then
x1 = index
y1 = C(index)
end
end
end
--[[
if x1 ~= index then
curfrom = x1
curto = index
else
curfrom = x2
curto = x1
end
if curto ~= curfrom and curfrom ~= nil and curto ~= nil then
if C(curto) ~= nil and C(curfrom) ~= nil then
k = (C(curto)- C(curfrom))/(curto- curfrom)
for i = curfrom, index do
curv = i*k + C(curto) - curto*k
SetValue(i, 1, curv)
end
end
end
--]]
if index == Size()-sh and ret == 0 then
ret = recalcline(1, index, 1)
ret = recalcline(2, index, 1)
levels(x1, y1, 8, index)
levels(x2, y2, 9, index)
levels(x3, y3, 10, index)
end
if index == Size()-sh then --- and index ~= last_ind
if vTo_01 - vFrom_01 > 500 then
vFrom_01 = vTo_01-500
end
for i = 1, index do
SetValue(i, 3, nil)
SetValue(i, 4, nil)
SetValue(i, 6, nil)
SetValue(i, 7, nil)
end
for i = vFrom_01, index do
y = a_01*i + b_01 + vplus_01
m = 0
if C(vTo_01) > C(vFrom_01) and y < C(vTo_01)+0.3*(C(vTo_01)-C(vFrom_01)) and y > C(vFrom_01)-0.3*(C(vTo_01)-C(vFrom_01)) then
m = 1
end
if C(vTo_01) < C(vFrom_01) and y < C(vFrom_01)+0.3*(C(vFrom_01)-C(vTo_01)) and y > C(vTo_01)-0.3*(C(vFrom_01)-C(vTo_01)) then
m = 1
end
if m == 1 then
SetValue(i, 3, y)
if i == index then
v3 = y
end
end
y = a_01*i + b_01 - vminus_01
m = 0
if C(vTo_01) > C(vFrom_01) and y < C(vTo_01)+0.3*(C(vTo_01)-C(vFrom_01)) and y > C(vFrom_01)-0.3*(C(vTo_01)-C(vFrom_01)) then
m = 1
end
if C(vTo_01) < C(vFrom_01) and y < C(vFrom_01)+0.3*(C(vFrom_01)-C(vTo_01)) and y > C(vTo_01)-0.3*(C(vFrom_01)-C(vTo_01)) then
m = 1
end
if m == 1 then
SetValue(i, 4, y)
if i == index then
v4 = y
end
end
end
if vTo_02 - vFrom_02 > 500 then
vFrom_02 = vTo_02-500
end
for i = vFrom_02, index do
y = a_02*i + b_02 + vplus_02
m = 0
if C(vTo_02) > C(vFrom_02) and y < C(vTo_02)+0.1*(C(vTo_02)-C(vFrom_02)) and y > C(vFrom_02)-0.1*(C(vTo_02)-C(vFrom_02)) then
m = 1
end
if C(vTo_02) < C(vFrom_02) and y < C(vFrom_02)+0.1*(C(vFrom_02)-C(vTo_02)) and y > C(vTo_02)-0.1*(C(vFrom_02)-C(vTo_02)) then
m = 1
end
if m == 1 then
SetValue(i, 6, y)
if i == index then
v6 = y
end
end
y = a_02*i + b_02 - vminus_02
m = 0
if C(vTo_02) > C(vFrom_02) and y < C(vTo_02)+0.1*(C(vTo_02)-C(vFrom_02)) and y > C(vFrom_02)-0.1*(C(vTo_02)-C(vFrom_02)) then
m = 1
end
if C(vTo_02) < C(vFrom_02) and y < C(vFrom_02)+0.1*(C(vFrom_02)-C(vTo_02)) and y > C(vTo_02)-0.1*(C(vFrom_02)-C(vTo_02)) then
m = 1
end
if m == 1 then
SetValue(i, 7, y)
if i == index then
v7 = y
end
end
end
last_ind = index
end
return v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
end