Тема: Индикатор корреляции
Помогите подправить код, не пойму где я накосячил)
Settings=
{
Name = "Correl",
tag = "rts",
period = 10,
value_type = "C",
line =
{
{
Name = "MA",
Color = RGB(255, 0, 0),
Type = TYPE_LINE,
Width = 1
}
}
}
function dValue(i,param)
local v = param or "C"
if v == "O" then
return O(i)
elseif v == "H" then
return H(i)
elseif v == "L" then
return L(i)
elseif v == "C" then
return C(i)
elseif v == "V" then
return V(i)
elseif v == "M" then
return (H(i) + L(i))/2
elseif v == "T" then
return (H(i) + L(i)+C(i))/3
elseif v == "W" then
return (H(i) + L(i)+2*C(i))/4
else
return C(i)
end
end
function Init()
return 1
end
function OnCalculate(index)
n = getNumCandles (Settings.tag)
t,n,s = getCandlesByIndex(Settings.tag,0,n-Settings.period,Settings.period)
function dMas(i,param)
local v = param or "C"
if v == "O" then
return t[i].open
elseif v == "H" then
return t[i].high
elseif v == "L" then
return t[i].low
elseif v == "C" then
return t[i].close --говорит, что = nil
elseif v == "M" then
return (t[i].high + t[i].low)/2
elseif v == "T" then
return (t[i].high + t[i].low+t[i].close)/3
elseif v == "W" then
return (t[i].high + t[i].low+t[i].close*2)/4
else
return t[i].close
end
end
if index < Settings.period then
return nil
else
local xy = 0
local x = 0
local y = 0
local x2 = 0
local y2 = 0
for i = index-Settings.period+1, index do
xy = xy + dValue(i, Settings.value_type) * dMas(i, Settings.value_type)
end
for i = index-Settings.period+1, index do
x = x + dValue(i, Settings.value_type)
end
for i = index-Settings.period+1, index do
y = y + dMas(i, Settings.value_type)
end
for i = index-Settings.period+1, index do
x2 = x2 + dValue(i, Settings.value_type)*dValue(i, Settings.value_type)
end
for i = index-Settings.period+1, index do
y2 = y2 + dMas(i, Settings.value_type)*dMas(i, Settings.value_type)
end
r =(Settings.period*xy-x*y)/math.sqrt ((Settings.period*x2-x*x)*(Settings.period*y2-y*y))
return r
end
end