-- Id: 1287 -- Indicator profile initialization routine -- Defines indicator profile properties and indicator parameters function Init() indicator:name("Stochastic Divergence"); indicator:description("Shows Stochastic Divergence"); indicator:requiredSource(core.Bar); indicator:type(core.Indicator); indicator.parameters:addInteger("K", "K", "Parameter of stochastic", 5); indicator.parameters:addInteger("SD", "SD", "Parameter of stochastic", 3); indicator.parameters:addInteger("D", "D", "Parameter of stochastic", 3); indicator.parameters:addColor("UP_color", "Color of Uptrend", "", core.rgb(255, 0, 0)); indicator.parameters:addColor("DN_color", "Color of Downtend", "", core.rgb(0, 255, 0)); end -- Indicator instance initialization routine -- Processes indicator parameters and creates output streams -- Parameters block local first; local source = nil; -- Streams block local lineid = nil; local dummy; -- Routine function Prepare() UP_color = instance.parameters.UP_color; DN_color = instance.parameters.DN_color; source = instance.source; assert(core.indicators:findIndicator("STOCHASTIC_DIVERGENCE") ~= nil, "STOCHASTIC_DIVERGENCE" .. " indicator must be installed"); Stochastic = core.indicators:create("STOCHASTIC_DIVERGENCE", source, instance.parameters.K,instance.parameters.SD,instance.parameters.D, false); first = Stochastic.DATA:first(); local name = profile:id() .. "(" .. source:name() .. ", " .. instance.parameters.K .. ", " .. instance.parameters.SD .. ", " .. instance.parameters.D .. ")"; instance:name(name); dummy = instance:addStream("D", core.Line, name .. ".D", "D", UP_color, 0); end local pperiod = nil; local pperiod1 = nil; local line_id = 0; -- Indicator calculation routine function Update(period, mode) local l; -- if recaclulation started - remove all if pperiod ~= nil and pperiod > period then core.host:execute("removeAll"); end pperiod = period; -- process only candles which are already closed closed. if pperiod1 ~= nil and pperiod1 == source:serial(period) then return ; end pperiod1 = source:serial(period) period = period - 1; Stochastic:update(mode); if Stochastic:getStream(1):hasData(period - 2) then l = math.abs(Stochastic:getStream(1)[period - 2]); local prev = period - 2 - l; line_id = line_id + 1; core.host:execute("drawLine", line_id, source:date(prev), source.high[prev], source:date(period - 2), source.high[period - 2], UP_color); end if Stochastic:getStream(2):hasData(period - 2) then l = math.abs(Stochastic:getStream(2)[period - 2]); local prev = period - 2 - l; line_id = line_id + 1; core.host:execute("drawLine", line_id, source:date(prev), source.low[prev], source:date(period - 2), source.low[period - 2], DN_color); end end