Anomaly of interpreter  [SOLVED]

Questions about MultiCharts and user contributed studies.
Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Anomaly of interpreter

Postby Smarty » 26 Feb 2018

I have an anomaly of interpreter. The function FUN is called by the indicator, but it is always called, ignoring the logical, mutually exclusive conditions that should have the function run only once.

This is indicator:

Code: Select all

inputs: calcMode(0), dataSeries1(1), dataSeries2(2);
vars: xSeries(0), ySeries(0), calc(0);

xSeries = Close of data(dataSeries1);
ySeries = Close of data(dataSeries2);

if calcMode = 1 then
   calc = fun(calcMode,xSeries,ySeries)   
else if calcMode = 2 then
   calc = fun(calcMode,xSeries,ySeries)      
else if calcMode = 3 then
   calc = fun(calcMode,xSeries,ySeries)         
else if calcMode = 4 then
   calc = fun(calcMode,xSeries,ySeries)         
else if calcMode = 5 then
   calc = fun(calcMode,xSeries,ySeries)         
else if calcMode = 6 then
   calc = fun(calcMode,xSeries,ySeries)         
else if calcMode = 7 then
   calc = fun(calcMode,xSeries,ySeries)         
else if calcMode = 8 then
   calc = fun(calcMode,xSeries,ySeries);      
   
if LastBarOnChart then
Print(ELDateToString(date),", fun ... calcMode: ",calcMode,", calc: ",calc);


Plot3(calc,"calcolo");


And this is the function:

Code: Select all

inputs: calcMode(numericsimple), xSeries(numericseries), ySeries(numericseries);
vars: double supVal(0.0), double infVal(0.0), value(0);

supVal = xSeries;
infVal = ySeries;

If infVal[1] <> 0 then value = supVal/infVal[1];

if LastBarOnChart = true then Print("fun: calcMode: ",calcMode);
fun = value;


final effect is this:

Code: Select all

fun: calcMode:    4.00
02/23/2018, switch_fun ... calcMode:    4.00, calc:    4.80
fun: calcMode:    4.00
fun: calcMode:    4.00
fun: calcMode:    4.00
fun: calcMode:    4.00
fun: calcMode:    4.00
fun: calcMode:    4.00
fun: calcMode:    4.00


the execution flow is completely indifferent to the condition "if then else", then executes all the branches of the condition. Same effect also using switch construct.

I hope support can solve fastly.

Regards, Smarty

User avatar
TJ
Posts: 6839
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 979 times
Been thanked: 1968 times

Re: Anomaly of interpreter

Postby TJ » 26 Feb 2018

You need to put PRINT statement at ALL the junctures.

You need to PRINT the variables values at every turn.
You need to PRINT the conditions values at every decision.

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 26 Feb 2018

Hi TJ ...

it's so clear what happened .... any calling at function is under a branch of "if-then-else", then must be called once .... do you want a switch version ?

Ciao

User avatar
TJ
Posts: 6839
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 979 times
Been thanked: 1968 times

Re: Anomaly of interpreter

Postby TJ » 26 Feb 2018

Smarty wrote:Hi TJ ...

it's so clear what happened .... any calling at function is under a branch of "if-then-else", then must be called once .... do you want a switch version ?

Ciao



This is a logical problem. The error is in the code.

GIGO

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 26 Feb 2018

where is logic error ? explain me ... and I said you "thanks" ...

or it's too much difficult to read ? do you want a more easy version ?

there are 10 instructions ...

User avatar
TJ
Posts: 6839
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 979 times
Been thanked: 1968 times

Re: Anomaly of interpreter

Postby TJ » 26 Feb 2018

Smarty wrote:where is logic error ? explain me ... and I said you "thanks" ...

or it's too much difficult to read ? do you want a more easy version ?

there are 10 instructions ...



I have already given you the instruction in post #2.

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 26 Feb 2018

This is a more easy version .. there are 2 instructions, in indicator there are "if then else" .... so it should print only 1 time following the logical condition ... no 2 times for both condition TRUE and FALSE...

Indicator:

Code: Select all

inputs: calcMode(0); 
vars: calc(0);

// or is calcMode = 1 or NOT ! .... no both !!
if calcMode = 1 then begin
   if LastBarOnChart then Print(ELDateToString(date),", calcMode = 1 --> TRUE");     
   calc = fun2(calcMode);   
end else begin
   if LastBarOnChart then Print(ELDateToString(date),", calcMode = 1 --> FALSE");              
   calc = fun2(calcMode);
end;
   
if LastBarOnChart then
Print(ELDateToString(date),", switch_fun ... calcMode: ",calcMode,", calc: ",calc); 

Plot1(calc,"calcolo");


and this is function ... 1 instruction (more simple)

Code: Select all

inputs: calcMode(numericsimple);
vars: double supVal(0.0), double infVal(0.0), value(0);

supVal = Close of data1;
infVal = Close of data2;

If infVal[1] <> 0 then value = supVal/infVal[1];

if LastBarOnChart = true then Print("Execution of fun: calcMode: ",calcMode);
fun2 = value;


Effects is this: 2 time fun2 is CALLED ! .... like or not like you ..... "if-then-else is NO working" try !!

Code: Select all

02/23/2018, calcMode = 1 --> FALSE
Execution of fun: calcMode:    0.00
02/23/2018, switch_fun ... calcMode:    0.00, calc:    4.80
Execution of fun: calcMode:    0.00


TJ, thanks for attention but I am waiting someone that understand the problem, I am interested like you to work ...
I am waiting support.

Hi at all.

i.e: for explain better ....
this happens ... the indicator correctly filters the if-then-else statement .... because there is print of indicator to prove it ...
the problem is .... fun2 is called twice ! ...

in the previous version I invoked the function several times ... with a more branched if-then-else structure ... the effect is that the function is always invoked ... no just 1 time !

User avatar
TJ
Posts: 6839
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 979 times
Been thanked: 1968 times

Re: Anomaly of interpreter

Postby TJ » 26 Feb 2018

You have to learn how to debug.

This is a coding error.

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 26 Feb 2018

Dear TJ .... then are 2 instructions !!!! .... no need debug ... debug have the same effects !

Thank you, but I don't answer more you.

I am waiting support

Ciao

User avatar
fbertram
Posts: 161
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 37 times
Been thanked: 66 times
Contact:

Re: Anomaly of interpreter

Postby fbertram » 26 Feb 2018

Smarty,

Is your function a simple function, or a series function? Keep in mind that for a series, the function has to be called on every bar. Therefore it might not be a good idea to call these functions conditionally.

Regardless of what your function is, is there a reason why you cannot call your function just once and use that result in the various branches of your if-then-else?


Cheers, Felix

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 26 Feb 2018

Hi Felix,
thanks for your contribute.

I put the same function to simplify the example ... but my goal is to perform a different functions according to the "calcMode" ... and those functions can also use previous values. Look at this version:

Code: Select all

inputs: calcMode(0); 
vars: calc(0);

// or is calcMode = 1 or NOT ! .... no both !!
if calcMode = 1 then begin
   if LastBarOnChart then Print(ELDateToString(date),", calcMode ... subtract");     
   calc = fun(calcMode);   
end else begin
   if LastBarOnChart then Print(ELDateToString(date),", calcMode ... ratio");              
   calc = fun2(calcMode);
end;
   
Plot1(calc,"calcolo");


functions 1: fun (do subctraction)

Code: Select all

inputs: calcMode(numericsimple);
vars: double supVal(0.0), double infVal(0.0), value(0);

supVal = Close of data1;
infVal = Close of data2;

value = supVal - infVal[1];

if LastBarOnChart = true then Print("Execution of fun: subtracting: ",calcMode);
fun = value;


functions 2: fun2 (do ratio)

Code: Select all

inputs: calcMode(numericsimple);
vars: double supVal(0.0), double infVal(0.0), value(0);

supVal = Close of data1;
infVal = Close of data2;

If infVal[1] <> 0 then value = supVal/infVal[1];

if LastBarOnChart = true then Print("Execution of fun: ratio: ",calcMode);
fun2 = value;


result is this:

Code: Select all

02/23/2018, calcMode ... ratio
Execution of fun: ratio:    0.00
Execution of fun: subtracting:    0.00


nothing to do: it is not an algorithm question, always flow go well ... but side effect is invoking both function !

the conclusions to you .....

Ciao Felix

User avatar
fbertram
Posts: 161
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 37 times
Been thanked: 66 times
Contact:

Re: Anomaly of interpreter

Postby fbertram » 26 Feb 2018

Hi Smarty,

this is pretty much what I said.

You said "and those functions can also use previous values" - this implies that these functions are not simple functions, but series functions. Now for a series function, you cannot just call them in a conditional statement, because you would run the risk that the time series ends up being invalid. So basically the interpreter has to execute them regardless of their position in the conditional statement.

However, I am pretty sure that in the calling code the conditional statement is honored. You can test this buy having fun and fun2 returning different values and see what is assigned to your calc variable.

Long story short: what you are seeing is not a bug, it is actual intentional behavior of series functions. I recommend to move calls to series functions out of the conditional statements and solve your calcMode switch in a different way, e.g by returning from the function right away when your calcMode doesn't match the function's intended purpose.

Hope this helps,
best regards,

Felix

Cheers, Felix

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 27 Feb 2018

Hi Felix, thank you, I read with attention ...

(1) "the calling code the conditional statement is honored" .... absolutelly true, I said already
(2) Now for a series function, you cannot just call them in a conditional statement .... if true, it's terrible,

I can understand that timeseries can have invalid values, then problem is just managed on first values of timeseries, .... but it's unacceptable to change in a destructured way the normal flow of a logic.

it's madness to invoke anyway a function in a strategy even if the flow does not pass, it is antiprogramming !!, it means that platform not being able to freely express a logic in an algorithm !

... and .. if I renounce the use of functions to organize the flow (that is, to programming in a professional way) it means that a program (strategy or function or indicator) must have a lot of instructions applied repeatedly in the code ... whose source code quickly becomes unmanageable.

Felix, this is unacceptable and you know it too.

I am waiting support still ...

Felix, with cordiality ...

bomberone1
Posts: 189
Joined: 02 Nov 2010
Has thanked: 14 times
Been thanked: 12 times

Re: Anomaly of interpreter

Postby bomberone1 » 27 Feb 2018

TJ wrote:You have to learn how to debug.

This is a coding error.


TJ do you understand the code and the problem??


Is there a professional programmer or an engineer who can make his contribution?
These users thanked the author bomberone1 for the post:
Smarty

User avatar
fbertram
Posts: 161
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 37 times
Been thanked: 66 times
Contact:

Re: Anomaly of interpreter

Postby fbertram » 27 Feb 2018

Hi Smarty,

before you jump to conclusions and get overly frustrated, take a deep breath, and dive a little deeper into the semantics of time series. PowerLanguage/ EasyLanguage makes these things look so simple and unassuming, but what's happening behind the scenes is actually surprisingly complicated. You are only scratching the surface with your problem, things get even more involved when your code is called multiple times for the same bar, which will happen in real-time.

(2) Now for a series function, you cannot just call them in a conditional statement .... if true, it's terrible,

Well, long story short: you can't. And even if you could, you shouldn't, because it will mess up your time series. Just think of a simple algorithm using two exponential moving averages. If could actually put the call to the moving average into conditional statements, how would your calling code know when the functions are 'warmed up' enough to trust the values?

it's madness to invoke anyway a function in a strategy even if the flow does not pass, it is antiprogramming !

If you need functions to only be invoked when the control flow passes them, use simple functions and voila - that's what you will get. Before you use time series functions, you need to understand a little more about what these really do.

Is there a professional programmer or an engineer who can make his contribution?

I am actually both. I am happy to send you an invoice :D

Cheers, Felix

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 27 Feb 2018

Hi Felix ..

thanks again ... :D :D :D ... we are colleagues may be we can send invoice at Multicharts (whose silence is deafening). We are serenely of opposite opinion ... for me, regardless of bizarre behavior of platform about timeseries, a logical flow is "sacred" and anything that happens out of flow is wrong.

For example this situation does NOT happen with Multicharts.NET ... that works serenely without these crazy side effects. on .NET platform exist the same of treatment of timeseries ...

... so ... which platform is wrong? :D :D

Ciao Felix.

User avatar
fbertram
Posts: 161
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 37 times
Been thanked: 66 times
Contact:

Re: Anomaly of interpreter

Postby fbertram » 27 Feb 2018

Hi Smarty,

I am actually spending 100% of my time on MultiCharts .NET. You are correct that this does not happen with the .NET version. However, when you call time series functions from conditional blocks, your time series will most likely be corrupted. Pick your poison...

Cheers, Felix

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 27 Feb 2018

Hi Felix ...
timeseries are not corrupted ...

Simply removing instructions that use previous values (lag parameter), anomaly disappears by magic :D Then I must write function without lag ... :(

We must look at reality, even if reality do not like us ... writing functions that refer to previous values of timeseries introduces the side effect of being always activated, without respecting the flow.

this it is unjustifiable.

Have a nice evening :wink:
Sabino.

bomberone1
Posts: 189
Joined: 02 Nov 2010
Has thanked: 14 times
Been thanked: 12 times

Re: Anomaly of interpreter

Postby bomberone1 » 01 Mar 2018

This multicharts error basically involves three fundamental elements at the level of standard functionality in live trading:

a) Unreliability in live trading between calculations made in the higher Data2 time frame (eg, 60 minutes) and operations performed and managed in the lower Data1 time frame (eg, at 1 minute).

b) Unreliability to make every type of spread (intramarket, intermarket, etc ..) trading between Data1 instrument A and Data2 instrument B, at the same time frame A and B.

3) Unreliability to operate on multiple calculations Data1, Data2 ..., Data..n with different instruments but at the same time frame or with identical instruments but different time frames.

We see that this is a BUG known to multicharts, so at the current state of things is correct to say that it is not advisable to trade with multicharts working on different date series?

What simple and effective solutions with examples of code have you adopted to solve these three problems for a while?

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter  [SOLVED]

Postby Smarty » 01 Mar 2018

Ciao Bomberone,
the problem exists if you refer to past values in a function,

this give error:

Code: Select all

If infVal[1] <> 0 then value = supVal/infVal[1];


this NO give error:

Code: Select all

If infVal <> 0 then value = supVal/infVal;


it is still possible to develop a system that works on an intermarket level but I'm thinking to switch to the dot.net version, or a competing platform.

Ciao.

bomberone1
Posts: 189
Joined: 02 Nov 2010
Has thanked: 14 times
Been thanked: 12 times

Re: Anomaly of interpreter

Postby bomberone1 » 01 Mar 2018

How to fix the problem?

Smarty
Posts: 29
Joined: 05 Nov 2013
Has thanked: 11 times
Been thanked: 1 time

Re: Anomaly of interpreter

Postby Smarty » 02 Mar 2018

Ciao Bomberone, ... no exist fix. This is evident.

You MUST avoid use previous values of variables inside functions. When a function use references to its array of values (previous), the interpreter badly handles the stack of function calls .. doing confusion.

Then fix is possible at interpreter source level, that is competence of Multicharts programmers (but they are silent).

Nothing to do for us.

Ciao


Return to “MultiCharts”