Все равно мало что понял. Код индикатора:
Код |
---|
function OnCalculate(idx)
if idx<Settings.Period_ATR then
return nil
else
if idx==Settings.Period_ATR then
psar={}
psar[idx]=L(idx)
long=true
hmax=H(idx)
per_ATR=Settings.Period_ATR
local TR=0
for js=(idx-per_ATR+1),idx do
TR=(TR+H(js)-L(js))
end
Old_ATR=TR/per_ATR
revers=true
else
if idx~=old_idx then
local TR=0
for js=(idx-per_ATR+1),idx do
TR=(TR+H(js)-L(js))
end
local ATR=TR/per_ATR
af=ATR/(Old_ATR+ATR)
af=af/10
Old_ATR=ATR
if long then
if hmax<H(idx-1) then
hmax=H(idx-1)
end
psar[idx]=psar[idx-1]+af*(hmax-psar[idx-1])
end
if short then
if lmin>L(idx-1) then
lmin=L(idx-1)
end
psar[idx]=psar[idx-1]+af*(lmin-psar[idx-1])
end
revers=true
end
if long and L(idx)<psar[idx] and revers then
psar[idx]=hmax
short=true
long=false
lmin=L(idx)
af=Step
revers=false
end
if short and H(idx)>psar[idx] and revers then
psar[idx]=lmin
long=true
short=false
hmax=H(idx)
af=Step
revers=false
end
end
old_idx=idx
return psar[idx]
end |
Дальше я хочу поместить его в свой скрипт, чтобы получилось примерно следующее:
Код |
---|
function OnInit()
function PSAR(idx)
if idx<Settings.Period_ATR then
return nil
else
if idx==Settings.Period_ATR then
psar={}
psar[idx]=L(idx)
long=true
hmax=H(idx)
per_ATR=Settings.Period_ATR
local TR=0
for js=(idx-per_ATR+1),idx do
TR=(TR+H(js)-L(js))
end
Old_ATR=TR/per_ATR
revers=true
else
if idx~=old_idx then
local TR=0
for js=(idx-per_ATR+1),idx do
TR=(TR+H(js)-L(js))
end
local ATR=TR/per_ATR
af=ATR/(Old_ATR+ATR)
af=af/10
Old_ATR=ATR
if long then
if hmax<H(idx-1) then
hmax=H(idx-1)
end
psar[idx]=psar[idx-1]+af*(hmax-psar[idx-1])
end
if short then
if lmin>L(idx-1) then
lmin=L(idx-1)
end
psar[idx]=psar[idx-1]+af*(lmin-psar[idx-1])
end
revers=true
end
if long and L(idx)<psar[idx] and revers then
psar[idx]=hmax
short=true
long=false
lmin=L(idx)
af=Step
revers=false
end
if short and H(idx)>psar[idx] and revers then
psar[idx]=lmin
long=true
short=false
hmax=H(idx)
af=Step
revers=false
end
end
old_idx=idx
return psar[idx]
end
end
function main()
function getCandles(ds, count) local candles = {}
local size = ds:Size()
for i = 0, count do
if size >= 1 then
candles[i] = {
high = ds:H(size - i) or 0,
low = ds:L(size - i) or 0,
open = ds:O(size - i) or 0,
close = ds:C(size - i) or 0
}
end
end
return candles
end
local ds_H1 = CreateDataSource(CLASS_CODE, SEC_CODE, INTERVAL_H1)
local h1Candles = getCandles(ds_H1, 5)
psar=PSAR(h1Candles) --только в цикле по количеству свечей
end
|
Но, не понимаю как передавать свечи в функцию индикатора, да еще и в цикле получается.