Why does 'Function Update' run twice each time in Indicators
Probably something simple to answer, but can someone tell me why the 'Function Update[period]' section runs through the program twice?
I can't see anything different in it doing this. I have run it through debugger and it runs through the same lines of program and gives same results twice, but only outputs results once (stream) for real.
(it actually streams it, then deletes rewrites over it again)
How do I get it to only run once?
because I have triggers (in my program) that are activated the first time that alter the outcome the second time it goes through!
Example of what I mean.....for ease of explanation, take the indicator - TriggerOsc1-HighLow.lua pasted below.
using the GBP/USD 15m data in SDK
it runs through the calculation routine once and has varibles assigned the various values depending on conditions. It gets to end of program (nils all the variables) goes back to function update[period] and runs through the same conditions again (take note that 'dif' & 'difHL' have been reset to 0.00000 they were 0.000640 & 0.001320 on first run) 'dif' & 'difHL' are now given the same varible as the first time, 0.000640 & 0.001320 (. program ends and returns to Function update which now shows different variables and then runs through twice giving the same result, and it does this twice for each function update.
period = 0 ....runs through ....period = 0 ....runs through again.... period = 1...runs...period = 1 ...runs.... period = 2 ...and so on......
Why?
Is this normal?
LordTwig
-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
indicator:name("Trigger - High and Low");
indicator:description("Indicates periods where the price changed by more than the specified number of pips. Works with High and Low rates.");
indicator:requiredSource(core.Bar);
indicator:type(core.Oscillator);
indicator.parameters:addColor("Bar_color", "Bar Color", "The color of the bars.", core.rgb(255, 0, 0));
indicator.parameters:addInteger("N", "Number of pips", "The number of the pips.", 10);
end
-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block
local first;
local source = nil;
local pips;
-- Streams block
local destStream = nil;
-- Routine
function Prepare()
source = instance.source;
first = source:first();
local n = instance.parameters.N;
local pipsize = source:pipSize();
pips = n * pipsize;
local name = profile:id() .. "(" .. source:name() .. "," .. n .. ")";
instance:name(name);
destStream = instance:addStream("Trigger", core.Bar, name, "Trigger", instance.parameters.Bar_color, first);
destStream:addLevel(1);
destStream:addLevel(0);
destStream:addLevel(-1);
end
-- Indicator calculation routine
function Update(period)
if period >= first and source:hasData(period) then
local dif = source.close[period] - source.open[period];
local difHL = source.high[period] - source.low[period];
if difHL > pips then
if dif >= 0 then
destStream[period] = 1;
else
destStream[period] = -1;
end
else
destStream[period] = 0;
end
end
end