добрый день помгите пожалуйста, делаю индикатор для quik Хай лоу пред дня + ATR , вот как мне прибавить значение atr к линии хая и лоя? что совсем не пойму
Settings =
{ Name = "1-HLDAY+ATR",
line =
{ { Name = "HIGH",
Color = RGB(0,200,0),
Type = TYPE_DASH,
Width = 2
},
{ Name = "LOW",
Color = RGB(200,0,0),
Type = TYPE_DASH,
Width = 2
}
}
}
function Init()
func = ATR()
return #Settings.line
end
function OnCalculate(Index)
local Out = ConvertValue(Settings, func(Index, Settings))
local HL = tonumber(Settings.Horizontal_line)
if Out then
local prev = GetValue(Index-1, 2) or GetValue(Index-1, 3) or 0
if Out > prev then
return HL,Out,nil
else
return HL,nil,Out
end
else
return HL,nil,nil
end
end
function ATR() --Average True Range ("ATR")
local f_TR = TR()
local tmp = {pp=nil, p=nil}
local it = {p=0, l=0}
return function (I, Fsettings, ds)
local Fsettings=(Fsettings or {})
local P = (Fsettings.Period or 14)
if (P > 0) then
if I == 1 then
tmp = {pp=nil, p=nil}
it = {p=0, l=0}
end
local tr = f_TR(I,ds)
if CandleExist(I,ds) then
if I~=it.p then
it = {p=I, l=it.l+1}
tmp.pp = tmp.p
end
if it.l < P then
tmp.p = (tmp.pp or 0) + tr
elseif it.l == P then
tmp.p = ((tmp.pp or 0) + tr) / P
return tmp.p
elseif it.l > P then
tmp.p = ((tmp.pp or 0) * (P-1) + tr) / P
return tmp.p
end
end
end
return nil
end
end
function TR() --True Range ("TR")
local it = {pp=0, p=0, l=0}
return function (I, ds)
if I == 1 then
it = {pp=0, p=0, l=0}
end
if CandleExist(I,ds) then
if I~=it.p then it={pp=it.p, p=I, l=it.l+1} end
if it.l == 1 then
return math.abs(GetValueEX(it.p,DIFFERENCE, ds))
else
return math.max(math.abs(GetValueEX(it.p,DIFFERENCE, ds)),
math.abs(GetValueEX(it.p,HIGH,ds) - GetValueEX(it.pp,CLOSE,ds)),
math.abs(GetValueEX(it.pp,CLOSE,ds)-GetValueEX(it.p,LOW,ds)))
end
end
return nil
end
end
SMA,MMA,EMA,WMA,SMMA,VMA = "SMA","MMA","EMA","WMA","SMMA","VMA"
OPEN,HIGH,LOW,CLOSE,VOLUME,MEDIAN,TYPICAL,WEIGHTED,DIFFERENCE,ANY = "O","H","L","C","V","M","T","W","D","A"
function CandleExist(I,ds)
return (type©=="function" and C(I)~=nil) or
(type(ds)=="table" and (ds[I]~=nil or (type(ds.Size)=="function" and (I>0) and (I<=ds:Size()))))
end
function Squeeze(I,P)
return math.fmod(I-1,P+1)
end
function ConvertValue(T,...)
local function r(V, R)
if R and string.upper®== "ON" then R=0 end
if V and tonumber® then
if V >= 0 then return math.floor(V * 10^R + 0.5) / 10^R
else return math.ceil(V * 10^R - 0.5) / 10^R end
else return V end
end
local arg = {...}
arg.n = select('#', ...)
if arg.n > 0 then
for i = 1, arg.n do
arg[i]=arg[i] and r(arg[i] * ((T and T.Multiply) or 1), (T and T.Round) or "off")
end
return unpack(arg)
else return nil end
end
function GetValueEX(I,VT,ds)
VT=(VT and string.upper(string.sub(VT,1,1))) or ANY
if VT == OPEN then --Open
return (O and O(I)) or (ds and ds:O(I))
elseif VT == HIGH then --High
return (H and H(I)) or (ds and ds:H(I))
elseif VT == LOW then --Low
return (L and L(I)) or (ds and ds:L(I))
elseif VT == CLOSE then --Close
return (C and C(I)) or (ds and ds:C(I))
elseif VT == VOLUME then --Volume
return (V and V(I)) or (ds and ds:V(I))
elseif VT == MEDIAN then --Median
return ((GetValueEX(I,HIGH,ds) + GetValueEX(I,LOW,ds)) / 2)
elseif VT == TYPICAL then --Typical
return ((GetValueEX(I,MEDIAN,ds) * 2 + GetValueEX(I,CLOSE,ds))/3)
elseif VT == WEIGHTED then --Weighted
return ((GetValueEX(I,TYPICAL,ds) * 3 + GetValueEX(I,OPEN,ds))/4)
elseif VT == DIFFERENCE then --Difference
return (GetValueEX(I,HIGH,ds) - GetValueEX(I,LOW,ds))
else --Any
return (ds and ds[I])
end
return nil
end
function Init()
return #Settings.line
end
local atr = tonumber(function ATR())
local hl =
{ -- year
-- month
-- day
-- high
-- low
}
function OnCalculate(index)
local dt = T(index)
if not C(index) then
return
end
local trdt = getTradeDate()
if dt.day ~= trdt.day or dt.month ~= trdt.month or dt.year ~= trdt.year then
if dt.day ~= hl.day or dt.month ~= hl.month or dt.year ~= hl.year then
hl.year = dt.year
hl.day = dt.day
hl.month = dt.month
hl.high = H(index)
hl.low = L(index)
else
hl.high = (math.max(hl.high,H(index))) + atr
hl.low = math.min(hl.low,L(index))
end
else
return hl.high,hl.low
end
end
Settings =
{ Name = "1-HLDAY+ATR",
line =
{ { Name = "HIGH",
Color = RGB(0,200,0),
Type = TYPE_DASH,
Width = 2
},
{ Name = "LOW",
Color = RGB(200,0,0),
Type = TYPE_DASH,
Width = 2
}
}
}
function Init()
func = ATR()
return #Settings.line
end
function OnCalculate(Index)
local Out = ConvertValue(Settings, func(Index, Settings))
local HL = tonumber(Settings.Horizontal_line)
if Out then
local prev = GetValue(Index-1, 2) or GetValue(Index-1, 3) or 0
if Out > prev then
return HL,Out,nil
else
return HL,nil,Out
end
else
return HL,nil,nil
end
end
function ATR() --Average True Range ("ATR")
local f_TR = TR()
local tmp = {pp=nil, p=nil}
local it = {p=0, l=0}
return function (I, Fsettings, ds)
local Fsettings=(Fsettings or {})
local P = (Fsettings.Period or 14)
if (P > 0) then
if I == 1 then
tmp = {pp=nil, p=nil}
it = {p=0, l=0}
end
local tr = f_TR(I,ds)
if CandleExist(I,ds) then
if I~=it.p then
it = {p=I, l=it.l+1}
tmp.pp = tmp.p
end
if it.l < P then
tmp.p = (tmp.pp or 0) + tr
elseif it.l == P then
tmp.p = ((tmp.pp or 0) + tr) / P
return tmp.p
elseif it.l > P then
tmp.p = ((tmp.pp or 0) * (P-1) + tr) / P
return tmp.p
end
end
end
return nil
end
end
function TR() --True Range ("TR")
local it = {pp=0, p=0, l=0}
return function (I, ds)
if I == 1 then
it = {pp=0, p=0, l=0}
end
if CandleExist(I,ds) then
if I~=it.p then it={pp=it.p, p=I, l=it.l+1} end
if it.l == 1 then
return math.abs(GetValueEX(it.p,DIFFERENCE, ds))
else
return math.max(math.abs(GetValueEX(it.p,DIFFERENCE, ds)),
math.abs(GetValueEX(it.p,HIGH,ds) - GetValueEX(it.pp,CLOSE,ds)),
math.abs(GetValueEX(it.pp,CLOSE,ds)-GetValueEX(it.p,LOW,ds)))
end
end
return nil
end
end
SMA,MMA,EMA,WMA,SMMA,VMA = "SMA","MMA","EMA","WMA","SMMA","VMA"
OPEN,HIGH,LOW,CLOSE,VOLUME,MEDIAN,TYPICAL,WEIGHTED,DIFFERENCE,ANY = "O","H","L","C","V","M","T","W","D","A"
function CandleExist(I,ds)
return (type©=="function" and C(I)~=nil) or
(type(ds)=="table" and (ds[I]~=nil or (type(ds.Size)=="function" and (I>0) and (I<=ds:Size()))))
end
function Squeeze(I,P)
return math.fmod(I-1,P+1)
end
function ConvertValue(T,...)
local function r(V, R)
if R and string.upper®== "ON" then R=0 end
if V and tonumber® then
if V >= 0 then return math.floor(V * 10^R + 0.5) / 10^R
else return math.ceil(V * 10^R - 0.5) / 10^R end
else return V end
end
local arg = {...}
arg.n = select('#', ...)
if arg.n > 0 then
for i = 1, arg.n do
arg[i]=arg[i] and r(arg[i] * ((T and T.Multiply) or 1), (T and T.Round) or "off")
end
return unpack(arg)
else return nil end
end
function GetValueEX(I,VT,ds)
VT=(VT and string.upper(string.sub(VT,1,1))) or ANY
if VT == OPEN then --Open
return (O and O(I)) or (ds and ds:O(I))
elseif VT == HIGH then --High
return (H and H(I)) or (ds and ds:H(I))
elseif VT == LOW then --Low
return (L and L(I)) or (ds and ds:L(I))
elseif VT == CLOSE then --Close
return (C and C(I)) or (ds and ds:C(I))
elseif VT == VOLUME then --Volume
return (V and V(I)) or (ds and ds:V(I))
elseif VT == MEDIAN then --Median
return ((GetValueEX(I,HIGH,ds) + GetValueEX(I,LOW,ds)) / 2)
elseif VT == TYPICAL then --Typical
return ((GetValueEX(I,MEDIAN,ds) * 2 + GetValueEX(I,CLOSE,ds))/3)
elseif VT == WEIGHTED then --Weighted
return ((GetValueEX(I,TYPICAL,ds) * 3 + GetValueEX(I,OPEN,ds))/4)
elseif VT == DIFFERENCE then --Difference
return (GetValueEX(I,HIGH,ds) - GetValueEX(I,LOW,ds))
else --Any
return (ds and ds[I])
end
return nil
end
function Init()
return #Settings.line
end
local atr = tonumber(function ATR())
local hl =
{ -- year
-- month
-- day
-- high
-- low
}
function OnCalculate(index)
local dt = T(index)
if not C(index) then
return
end
local trdt = getTradeDate()
if dt.day ~= trdt.day or dt.month ~= trdt.month or dt.year ~= trdt.year then
if dt.day ~= hl.day or dt.month ~= hl.month or dt.year ~= hl.year then
hl.year = dt.year
hl.day = dt.day
hl.month = dt.month
hl.high = H(index)
hl.low = L(index)
else
hl.high = (math.max(hl.high,H(index))) + atr
hl.low = math.min(hl.low,L(index))
end
else
return hl.high,hl.low
end
end