The simplest thing doesn't seem to work, can someone help?  [SOLVED]

Questions about MultiCharts and user contributed studies.
davidib
Posts: 4
Joined: 23 Feb 2013
Has thanked: 3 times
Been thanked: 1 time

The simplest thing doesn't seem to work, can someone help?

Postby davidib » 21 Mar 2013

Hi Guys,

this is so simple I'm almost ashamed to post it but I've pulling my hair out trying to figure out what is going on.

I have a simple function:

Code: Select all

inputs: ftype(NumericSimple), parm0(NumericSimple);
vars: count(0);

switch (ftype)
begin
//init the counter
case 0:
count = 0;
KR_Test0=0;
//increment the counter
case 1:
count = count + 1;
print("<A> count is "+NumToStr(count,0));
KR_Test0 = 0;
//get the counter
case 2:
print("<B> count is "+NumToStr(count,0));
KR_Test0 = count;
end;
My signal (just a test to call this function) looks like this:

Code: Select all

vars: cnt(0);
//init the function on 1st bar
if(currentbar = 1 ) then
begin
KR_Test0(0,0);
end;

//increment the counter
KR_Test0(1,0);
//get the counter
cnt = KR_Test0(2,0);
But for some reason the function variable count always is set to 0 in case stmt 2. This is a snippet of the output (tag <A> is when count is incced, tag <B> is when count is returned):
<A> count is 1
<B> count is 0
<A> count is 2
<B> count is 0
<A> count is 3
<B> count is 0
<A> count is 4
<B> count is 0
<A> count is 5
<B> count is 0

I really have my tail between my legs on this, I know I'm missing something very obvious, but not sure what it could be. I've tried setting the function type to numeric, series, auto, no change.

Well any help greatly appreciated!

Thanks in advance

David

mips4pips
Posts: 10
Joined: 27 Nov 2012
Has thanked: 10 times
Been thanked: 2 times

Re: The simplest thing doesn't seem to work, can someone hel  [SOLVED]

Postby mips4pips » 21 Mar 2013

Hi David,

The problem here is that EL Functions can not be used to keep track of counters or any other variables between calls to the function, not even if you declare them as intrabarpersist. It's not part of their job.

The fact that in your example it seems to work for case 1, but not for case 2, is just an odd coincidence. If you want to prove this, make 4 or 5 consecutive calls with parameter 1, and you will see that the counter is not really kept by the function.

If you create and maintain your intrabarpersist counter within a signal, then it will keep the correct value. If you need the counter accessible to multiple signals, then I would suggest using Global Variables.

Regards,
Mips4Pips

davidib
Posts: 4
Joined: 23 Feb 2013
Has thanked: 3 times
Been thanked: 1 time

Re: The simplest thing doesn't seem to work, can someone hel

Postby davidib » 21 Mar 2013

Hey Mips,

thanks ALOT. I knew I was missing some key concept when even the simplest of functions would not retain their values. I've been thru the EasyLanguage manual, don't recall seeing this explicitly stated anywhere, and it's such a foundational concept for all other languages that it's easy to assume EasyLanguage will behave the same way; no wonder why we hate it! But seriously it's my bad for forgetting this most basics of EasyLanguage (I used to do quite a bit of EL programming some time ago).


So I'll just pass in parms by reference and maintain them in the signal, its not as nice but it should do the trick.

arjfca
Posts: 1292
Joined: 23 Nov 2010
Has thanked: 725 times
Been thanked: 223 times

Re: The simplest thing doesn't seem to work, can someone hel

Postby arjfca » 22 Mar 2013

Hey Mips,

thanks ALOT. I knew I was missing some key concept when even the simplest of functions would not retain their values. I've been thru the EasyLanguage manual, don't recall seeing this explicitly stated anywhere, and it's such a foundational concept for all other languages that it's easy to assume EasyLanguage will behave the same way; no wonder why we hate it! But seriously it's my bad for forgetting this most basics of EasyLanguage (I used to do quite a bit of EL programming some time ago).


So I'll just pass in parms by reference and maintain them in the signal, its not as nice but it should do the trick.
You could also use Global variable to keep your values

Martin :)


Return to “MultiCharts”