Показывает мин и макс зоны горизонтальной проторговки

--[[
индикатор: строится средняя с учетом уровней проторговки
параметры:
proc - процент минимального отклонения уровней
xdelta - история
--]]
Settings={
Name="LVLS_v4",
proc=2.0,
xdelta = 500,
line=
{
{
Name = "cur1",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0, 0, 0)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255, 0, 0)
},
{
Name = "cur3",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0, 255, 0)
}
}
}
function Init()
return 3
end
function OnCalculate(index)
xdelt = 30
if index >= Size() - Settings.xdelta then
if index == Size() - Settings.xdelta then
ma = {}
ma[index] = O(index)
mas = {}
mas[index] = O(index)
xpos = {}
xpos[index] = index
ypos = {}
ypos[index] = O(index)
xfrom = {}
xfrom[index] = index
end
if index > Size() - Settings.xdelta then
mas[index] = mas[index-1] + O(index)
ma[index] = ma[index-1]
xpos[index] = xpos[index-1]
ypos[index] = ypos[index-1]
xfrom[index] = xfrom[index-1]
end
xfrm = xfrom[index]
for i = index, xfrm, -1 do
ysum1 = 0
ycnt1 = 0
for j = xpos[index], i-1 do
ysum1 = ysum1 + O(j)
ycnt1 = ycnt1 + 1
end
if ycnt1 > 0 then
ysum1 = ysum1/ycnt1
end
ysum2 = 0
ycnt2 = 0
for j = i, index do
ysum2 = ysum2 + O(j)
ycnt2 = ycnt2 + 1
end
if ycnt2 > 0 then
ysum2 = ysum2/ycnt2
end
if Settings.proc*ysum1 < 100*math.abs(ysum2 - ysum1) and ysum1 ~= 0 and ysum2 ~= 0 and ycnt2 > xdelt then
xpos[index] = i
ypos[index] = ysum2
xfrom[index] = i -- index
else
if i == index then
ypos[index] = ysum1
end
end
end
if index > 1 then
--if xpos[index] ~= xpos[index-1] then
if ypos[index] ~= 0 then
val = ypos[index]
for i = xpos[index], index do
SetValue(i, 1, val)
end
end
--end
end
vmax = nil
vmin = nil
if index == Size() then
if ypos[index] ~= 0 then
vmax = ypos[index]
vmin = ypos[index]
for i = xpos[index], index do
if vmax < O(i) and ypos[index] < O(i) then
vmax = O(i)
end
if vmin > O(i) and ypos[index] > O(i) then
vmin = O(i)
end
end
for i = xpos[index], index do
SetValue(i, 2, vmax)
SetValue(i, 3, vmin)
end
end
end
end
if index >= Size() - Settings.xdelta then
return ypos[index], vmax, vmin
else
return nil, nil, nil
end
end<br /><br />

--[[
индикатор: строится средняя с учетом уровней проторговки
параметры:
proc - процент минимального отклонения уровней
xdelta - история
--]]
Settings={
Name="LVLS_v5",
proc=2.0,
xdelta = 500,
line=
{
{
Name = "cur1",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0, 0, 0)
},
{
Name = "cur2",
Type =TYPE_LINE,
Width = 2,
Color = RGB(255, 0, 0)
},
{
Name = "cur3",
Type =TYPE_LINE,
Width = 2,
Color = RGB(0, 255, 0)
}
}
}
function Init()
return 3
end
function OnCalculate(index)
xdelt = 30
if index >= Size() - Settings.xdelta then
if index == Size() - Settings.xdelta then
ma = {}
ma[index] = O(index)
mas = {}
mas[index] = O(index)
xpos = {}
xpos[index] = index
ypos = {}
ypos[index] = O(index)
xfrom = {}
xfrom[index] = index
end
if index > Size() - Settings.xdelta then
mas[index] = mas[index-1] + O(index)
ma[index] = ma[index-1]
xpos[index] = xpos[index-1]
ypos[index] = ypos[index-1]
xfrom[index] = xfrom[index-1]
end
xfrm = xfrom[index]
for i = index, xfrm, -1 do
ysum1 = 0
ycnt1 = 0
for j = xpos[index], i-1 do
ysum1 = ysum1 + O(j)
ycnt1 = ycnt1 + 1
end
if ycnt1 > 0 then
ysum1 = ysum1/ycnt1
end
ysum2 = 0
ycnt2 = 0
for j = i, index do
ysum2 = ysum2 + O(j)
ycnt2 = ycnt2 + 1
end
if ycnt2 > 0 then
ysum2 = ysum2/ycnt2
end
if Settings.proc*ysum1 < 100*math.abs(ysum2 - ysum1) and ysum1 ~= 0 and ysum2 ~= 0 and ycnt2 > xdelt then
xpos[index] = i
ypos[index] = ysum2
xfrom[index] = i -- index
else
if i == index then
ypos[index] = ysum1
end
end
end
--[[
if index > 1 then
--if xpos[index] ~= xpos[index-1] then
if ypos[index] ~= 0 then
val = ypos[index]
for i = xpos[index], index do
SetValue(i, 1, val)
end
end
--end
end
--]]
vmax = nil
vmin = nil
val = nil
if index == Size() then
if ypos[index] ~= 0 then
--[[
vmax = ypos[index]
vmin = ypos[index]
for i = xpos[index], index do
if vmax < O(i) and ypos[index] < O(i) then
vmax = O(i)
end
if vmin > O(i) and ypos[index] > O(i) then
vmin = O(i)
end
end
for i = xpos[index], index do
SetValue(i, 2, vmax)
SetValue(i, 3, vmin)
end
--]]
n=0
xy = 0
x=0
y=0
x2=0
for i = xpos[index], index do
n = n + 1
xy = xy + (i-xpos[index]+1)*O(i)
x=x+(i-xpos[index]+1)
y=y+O(i)
x2=x2+(i-xpos[index]+1)*(i-xpos[index]+1)
end
if n ~= 0 and n*x2-x*x ~= 0 then
a=(n*xy-x*y)/(n*x2-x*x)
b=(y-a*x)/n
vmax = 0
vmin = 0
for i = xpos[index], index do
val = a*(i-xpos[index]+1)+b
if vmax < O(i)-val and val < O(i) then
vmax = O(i)-val
end
if vmin < val-O(i) and val > O(i) then
vmin = val-O(i)
end
end
for i = xpos[index], index do
val = a*(i-xpos[index]+1)+b
SetValue(i, 1, val)
SetValue(i, 2, val+vmax)
SetValue(i, 3, val-vmin)
end
end
end
end
end
if index >= Size() - Settings.xdelta then
return val, val+vmax, val-vmin
else
return nil, nil, nil
end
end