You can use the following correct implementation of the Stochastic fast until bug is fixed.
Code:
-- Indicator profile initialization routine
function Init()
indicator:name("Stochastic Fast (Modified)");
indicator:description("Stochastic Oscillator is a momentum indicator that shows the location of the current close relative to the high/low range over a set number of periods. Closing levels that are consistently near the top of the range indicate accumulation (buying pressure) and those near the bottom of the range indicate distribution (selling pressure).");
indicator:requiredSource(core.Bar);
indicator:type(core.Oscillator);
indicator.parameters:addInteger("K", "%K", "", 14, 2, 1000);
indicator.parameters:addInteger("D", "%D", "", 3, 2, 1000);
indicator.parameters:addColor("clrFirst", "%K color", "", core.rgb(0, 255, 0));
indicator.parameters:addColor("clrSecond", "%D color", "", core.rgb(255, 0, 0));
end
-- Indicator instance initialization routine
local k;
local d;
local source = nil;
-- Streams block
local iK = nil;
local K = nil;
local D = nil;
-- Routine
function Prepare()
k = instance.parameters.K;
d = instance.parameters.D;
source = instance.source;
local name = profile:id() .. "(" .. source:name() .. ", " .. k .. ", " .. d .. ")";
instance:name(name);
K = instance:addStream("K", core.Line, name .. ".K", "K", instance.parameters.clrFirst, source:first() + k);
D = instance:addStream("D", core.Line, name .. ".D", "D", instance.parameters.clrSecond, source:first() + k + d);
end
-- Indicator calculation routine
function Update(period)
if period >= K:first() then
local kRange = core.rangeTo(period, k);
local ll = core.min(source.low, kRange);
local hh = core.max(source.high, kRange);
K[period] = 100 * ((source.close[period] - ll) / (hh - ll));
end
if period >= D:first() then
local dRange = core.rangeTo(period, d);
D[period] = core.avg(K, dRange);
end
end
Instruction:
- Copy the code above and save it as a text file SFKM.lua
- Choose Charts->Chart->Manage Custom Indicator menu item
- Press Load
- Point to the file saved on the first step
- Now you have SFKM indicator in the list of indicators.
This implementation 100% meets all popular descriptions of this indicator.