Moderator: admin
-- Indicator profile initialization routine
function Init()
indicator:name("Gann High Low Activator");
indicator:description("No description");
indicator:requiredSource(core.Bar);
indicator:type(core.Indicator);
indicator.parameters:addInteger("N", "Number of Periods", "", 3);
indicator.parameters:addColor("GHLA_color", "Color of the Line", "", core.rgb(255, 0, 0));
end
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
local N;
local first;
local source = nil;
-- Streams block
local GHLA = nil; -- indicator result
local pdir = nil; -- the previous direction
-- Routine
function Prepare()
N = instance.parameters.N;
source = instance.source;
first = source:first() + N;
local name = profile:id() .. "(" .. source:name() .. ", " .. N .. ")";
instance:name(name);
GHLA = instance:addStream("GHLA", core.Line, name, "GHLA", instance.parameters.GHLA_color, first);
pdir = instance:addInternalStream(0, 0);
end
-- Indicator calculation routine
function Update(period)
pdir[period] = 0;
if period >= first and source:hasData(period) then
-- calculate average high and low for the previous N periods (excluding the current).
local range = core.rangeTo(period - 1, N);
local avgh, avgl;
local switch = 0;
avgh = core.avg(source.high, range);
avgl = core.avg(source.low, range);
-- change direction in case the current close is above average high
-- or below average low
if (source.close[period] > avgh) then
switch = 1;
elseif (source.close[period] < avgl) then
switch = -1;
end
if (switch ~= 0) then
-- remember the direction in case we have to switch
pdir[period] = switch;
dir = switch;
else
-- or use the previous direction
dir = pdir[period - 1];
end
-- and get average of the last N high or low values depending on the direction
range = core.rangeTo(period, N);
if (dir == -1) then
GHLA[period] = core.avg(source.high, range);
else
GHLA[period] = core.avg(source.low, range);
end
end
end
Return to Indicator and Signal Requests
Users browsing this forum: No registered users and 35 guests