Добавить в корзинуПозвонить
Найти в Дзене
Простое МА

продолжение

Так как OnCalculate(idx) срабатывает на каждое изменение цены, добавим условие, которое позволит рассчитывать МА для новой свечи только один раз. Переменная Lastidx принимает значение текущего номера свечи. if Lastidx < idx then Lastidx = idx -- блок -- end Теперь программа выглядит так: Settings={} Settings.Name = "*RobotMA" Settings.B_PeriodMA = "1" function Init() return 8 end function MyInit() APeriodMA = {} APeriodMA[0] = 0 APeriodMA[1] = 0 PeriodMA = Settings.B_PeriodMA+0 LastIdx = 0 end function OnCalculate(idx) if idx == 1 then MyInit() end if LastIdx < idx then LastIdx = idx APeriodMA[idx] = APeriodMA[idx-1] if idx >= PeriodMA then mSum = 0 if idx == PeriodMA then for i = 0, PeriodMA-1, 1 do mSum = mSum + O(idx - i) end APeriodMA[idx] = mSum/PeriodMA end if idx > PeriodMA then APeriodMA[idx] = APeriodMA[idx-1] - O(idx-PeriodMA)/PeriodMA + O(idx)/PeriodMA end end end if idx >= PeriodMA then return APeriodMA[idx] end end

Так как OnCalculate(idx) срабатывает на каждое изменение цены, добавим условие, которое позволит рассчитывать МА для новой свечи только один раз.

Переменная Lastidx принимает значение текущего номера свечи.

if Lastidx < idx then

Lastidx = idx

-- блок --

end

Теперь программа выглядит так:

Settings={}

Settings.Name = "*RobotMA"

Settings.B_PeriodMA = "1"

function Init()

return 8

end

function MyInit()

APeriodMA = {}

APeriodMA[0] = 0

APeriodMA[1] = 0

PeriodMA = Settings.B_PeriodMA+0

LastIdx = 0

end

function OnCalculate(idx)

if idx == 1 then

MyInit()

end

if LastIdx < idx then

LastIdx = idx

APeriodMA[idx] = APeriodMA[idx-1]

if idx >= PeriodMA then

mSum = 0

if idx == PeriodMA then

for i = 0, PeriodMA-1, 1 do

mSum = mSum + O(idx - i)

end

APeriodMA[idx] = mSum/PeriodMA

end

if idx > PeriodMA then

APeriodMA[idx] = APeriodMA[idx-1] - O(idx-PeriodMA)/PeriodMA + O(idx)/PeriodMA

end

end

end

if idx >= PeriodMA then

return APeriodMA[idx]

end

end