An example of the pipsize usage is in the Swing Index standard indicator. Please look at the
Prepare method.
Code:
-- The formula is described in the Kaufman "Trading Systems and Methods" chapter 12 "Charting Systems" (page 286-287)
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
indicator:name(resources:get("name"));
indicator:description(resources:get("description"));
indicator:requiredSource(core.Bar);
indicator:type(core.Oscillator);
indicator.parameters:addColor("clrSI", resources:get("param_clrSI_name"), resources:get("param_clrSI_description"), core.rgb(255, 0, 0));
end
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block
local first;
local source = nil;
-- Streams block
local SI = nil;
local T = nil;
-- Routine
function Prepare()
source = instance.source;
first = source:first() + 1;
local name = profile:id() .. "(" .. source:name() .. ")";
instance:name(name);
SI = instance:addStream("SI", core.Line, name, "SI", instance.parameters.clrSI, first)
T = source:pipSize() * 300;
end
-- Indicator calculation routine
function Update(period)
if period >= first then
local nom = source.close[period] - source.close[period - 1]
+ (.5 * (source.close[period] - source.open[period]))
+ (.25 * (source.close[period - 1] - source.open[period - 1]));
local closePrev = source.close[period - 1];
local highCurr = source.high[period];
local lowCurr = source.low[period];
local hc = math.abs(highCurr - closePrev);
local lc = math.abs(lowCurr - closePrev);
local hl = math.abs(highCurr - lowCurr);
local co = math.abs(closePrev - source.open[period - 1]);
local TR = math.max(hc, lc, hl);
local ER = 0;
if (closePrev > highCurr) then
ER = hc;
elseif (closePrev < lowCurr) then
ER = lc;
end
local SH = co;
local R = TR - 0.5 * ER + 0.25 * SH;
local K = math.max(hc, lc);
if (math.abs(R) < 1e-10) then
SI[period] = 0;
else
SI[period] = 50.0 * nom * (K / T) / R;
end
end
end