Осциллятор ZIG_OSC_v3
--[[
параметры:
Procent - процент зигзага
--]]
Settings={
Name="ZIG_OSC_v3",
Procent=5,
ln=10, -- period ema
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 = "cur3",
Type =TYPE_LINE,
Width = 1,
Color = RGB(0,0, 0)
}
}
}
function Init()
ema = {}
y1 = nil
y2 = nil
x1 = 1
x2 = 1
a = nil
b = nil
n = {}
sxy = {}
sx = {}
sy = {}
sxx = {}
d = {}
cnt = {}
val={}
return 3
end
function initkoef(_x1) --, n, sxy, sx, sy, sxx
n[_x1] = 0
sxy[_x1] = 0
sx[_x1] = 0
sy[_x1] = 0
sxx[_x1] = 0
d[_x1] = 0
cnt[_x1] = 0
end
function calc(_x1, _x2)
if C(i) == nil then
initkoef(_x1)
else
for i = _x1, _x1 do
n[i] = 1
sxy[i] = i*C(i)
sx[i] = i
sy[i] = C(i)
sxx[i] = i*i
end
end
for i = _x1+1, _x2 do
n[i] = n[i-1] + 1
sxy[i] = sxy[i-1] + i*C(i)
sx[i] = sx[i-1] + i
sy[i] = sy[i-1] + C(i)
sxx[i] = sxx[i-1] + i*i
end
end
function calcd(_x1, _x2)
for i = _x1, _x1 do
curv = i*geta(_x2) + getb(_x2)
if C(x2) > C(x1) then
if C(i) > curv then
d[i] = H(i) - curv
cnt[i] = 1
else
d[i] = 0
cnt[i] = 0
end
else
if C(i) < curv then
d[i] = L(i) - curv
cnt[i] = 1
else
d[i] = 0
cnt[i] = 0
end
end
end
for i = _x1+1, _x2 do
curv = i*geta(_x2) + getb(_x2)
if C(x2) > C(x1) then
if C(i) > curv then
--d[i] = d[i-1] + H(i) - curv
if H(i) - curv > d[i-1] then
d[i] = H(i) - curv
else
d[i] = d[i-1]
end
cnt[i] = cnt[i-1] + 1
else
d[i] = d[i-1]
cnt[i] = cnt[i-1]
end
else
if C(i) < curv then
--d[i] = d[i-1] + L(i) - curv
cnt[i] = cnt[i-1] + 1
if L(i) - curv < d[i-1] then
d[i] = L(i) - curv
else
d[i] = d[i-1]
end
else
d[i] = d[i-1]
cnt[i] = cnt[i-1]
end
end
end
--[[
if cnt ~= 0 then
d[_x2] = 2*d[_x2]/cnt
end --]]
end
function cpy(_x1)
n[_x1] = n[_x1-1]
sxy[_x1] = sxy[_x1-1]
sx[_x1] = sx[_x1-1]
sy[_x1] = sy[_x1-1]
sxx[_x1] = sxx[_x1-1]
d[_x1] = d[_x1-1]
cnt[_x1] = cnt[_x1-1]
end
function prnt(_x1, _x2)
curv = _x2*geta(_x2) + getb(_x2)
--[[
if d[_x2] ~= nil then
dx = d[_x2]
if dx < 0 then
dx = -dx
end
else
dx = 1
end
if C(_x2) ~= nil and curv ~= nil then
val[_x2] = (C(_x2) - curv)/ dx
end
--]]
for i = _x1, _x2 do
curv = i*geta(_x2) + getb(_x2) --+ d[i]
-- SetValue(i, 1, curv)
if d[_x2] ~= nil and cnt[_x2] ~= 0 then
--curv = curv + 2.5*d[_x2]/cnt[_x2]
curv = curv + d[_x2]
end
val[i] = curv
end
end
function geta(_i)
res = 0
if n[_i]~=nil and sxy[_i]~=nil and sx[_i]~=nil and sy[_i]~=nil and sxx[_i]~=nil then
res = (n[_i]*sxy[_i]-sx[_i]*sy[_i])/(n[_i]*sxx[_i]-sx[_i]*sx[_i])
end
return res
end
function getb(_i)
res = 0
if n[_i]~=nil and sx[_i]~=nil and sy[_i]~=nil then
res = (sy[_i] - geta(_i)*sx[_i])/n[_i]
end
return res
end
function OnCalculate(index)
de = Settings.Procent
val[index] = C(index)
if index <= 1 then
y1 = val[index]
y2 = val[index]
--SetValue(index, 1, C(index))
else
if C(index) > y1*(1+de/100) and y1 < y2 then
x2 = x1
y2 = y1
x1 = index
y1 = C(index)
initkoef(x2)
calc(x2, x1)
calcd(x2, x1)
prnt(x2, x1)
--SetValue(index-1, 1, nil)
--SetValue(index-2, 1, nil)
--SetValue(index, 1, C(index))
else
--calc(index, index)
if C(index) > y1 and y1 >= y2 --and C(index) > y2
then
initkoef(x2)
calc(x2, index)
calcd(x2, index)
prnt(index, index)
--SetValue(index, 1, C(index))
x1 = index
y1 = C(index)
--else
end
if C(index) <= y1 and y1 >= y2 then
cpy(index)
prnt(index, index)
-- SetValue(index, 1, C(index))
else
-- SetValue(index, 1, C(index))
end
end
if C(index) < y1*(1-de/100) and y1 > y2 then
x2 = x1
y2 = y1
x1 = index
y1 = C(index)
initkoef(x2)
calc(x2, x1)
calcd(x2, x1)
prnt(x2, x1)
--SetValue(index-1, 1, nil)
--SetValue(index-2, 1, nil)
--SetValue(index, 1, C(index))
else
--calc(index, index)
if C(index) < y1 and y1 <= y2 --and C(index) < y2
then
initkoef(x2)
calc(x2, index)
calcd(x2, index)
prnt(index, index)
--SetValue(index, 1, C(index))
x1 = index
y1 = C(index)
--else
end
if C(index) >= y1 and y1 <= y2 then
cpy(index)
prnt(index, index)
-- SetValue(index, 1, C(index))
else
-- SetValue(index, 1, 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 val[index] > 1.2*C(index) then
return 1.2*C(index)
else
if val[index] < 0.8*C(index) then
return 0.8*C(index)
else
return val[index]
end
end
--]]
ln = Settings.ln
if index-1 > 1 and val[index]~=nil and ema[index-1] ~= nil then
ema[index] = (ema[index-1]*(ln-1) + val[index])/ln
--[[ sum = 0 val[index]+1 --
for i = index-ln+1, index do
sum = sum + val[i]
end
ema[index] = sum /ln
--]]
else
ema[index] = 0--val[index]
end
return C(index) - val[index], ema[index], 0 --index*geta(index) + getb(index) --
--
-- SetValue(index, 1, C(index))
end