Why does Function Update run twice in Indicators

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Why does Function Update run twice in Indicators

Postby LordTwig » Sun Apr 11, 2010 8:25 am

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
Attachments
TriggerOsc1-HighLow.lua
(1.81 KiB) Downloaded 805 times
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Why does Function Update run twice in Indicators

Postby LordTwig » Mon Apr 12, 2010 1:18 am

Okay, someone fixed it, debugger and Marketscope now only runs through program once.
and now my triggers are working.
Problem was with previous sdk & marketscope

I would suggest everyone download the new SDK from here, even if they previously downloaded it
http://www.fxcodebase.com/code/viewtopic.php?f=16&t=586

And take note!! Nikolay has added a restart in dropdown of debugger and it also saves your watches as long as you keep it open & don't change indicator your debugging, thank you Nikolay for that it is way better now.
Thanks
LordTwig
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am

Re: Why does Function Update run twice in Indicators

Postby Nikolay.Gekht » Mon Apr 12, 2010 12:44 pm

I'm not sure whether I helped you rather than introducing the problem by a such fix. :cry:
The indicator must be ready that the Update function will be called twice, 3 times, 100 times for the same candle. It could happen for the last candle of the alive chart (i.e. chart which received the current offers). Each offer can change the high and low prices and always changes the close price, to Update function is called for each new offer.

If you want to make your application working for the completely closed candle only, please follow to this recommendation:

1) Specify -1 in the extent parameter of the stream. This will cut the latest number in the output stream, which corresponds for the unclosed candle.

2) Add a global variable last_candle, which will point the last candle processed. Put nil initially to this variable.

3) In the Update function get serial value of the source (source:serial(period)). The serial is an unique number which identifier a bar of the source. If the last_candle is not nil and is equal to the serial of the current period - return. Else - remember the serial and then process the previous bar.

I can provide an simple example (for instance MVA which does not calculate the open candle) in case the explanation is not clear enough.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Why does Function Update run twice in Indicators

Postby LordTwig » Wed Apr 14, 2010 11:39 am

Hi Nikolay, I will take on board what you say, thanks for that.

But I haven't had the same occur since I downloaded latest versions of SDK & Marketscope.

I did however reinstall earlier version of SDK and the Debugger did the same thing as described above, so something is screwy in it, Just glad the new version works.

One thing that annoys me even in the new SDK debugger is when you put in a 'breakpoint' and hit F5, the program runs though and streams all the data until the end of data file, but gives all streams other than date & OHLC a 'n/a' then it goes back to where you had your 'breakpoint' when you want to cycle through using F8 you have to keep scolling up (can be hundreds of lines) to beginning (date of next stream output) to see the result.

Every time the F8 is pushed you have to do this.(after F5 has been used). Very annoying indeed.

LordTwig
Cheers
LordTwig
LordTwig
FXCodeBase: Confirmed User
 
Posts: 157
Joined: Fri Mar 12, 2010 3:15 am


Return to Discussions

Who is online

Users browsing this forum: No registered users and 10 guests