Issue With Conditional Statement Execution

Questions about MultiCharts and user contributed studies.
pschriber2
Posts: 36
Joined: 24 Apr 2012
Has thanked: 6 times
Been thanked: 2 times

Issue With Conditional Statement Execution

Postby pschriber2 » 08 Oct 2012

Hi,
I wrote some code that is conditional on whether the calculation is for real time data or historical data, and some conditional values.


The issue I'm having is that when the values DH=0 and DL = 0 are true, the code executes the second statement (correct). But once it has done that, and changed DH and DL to real values, for some reason it executes the first conditional statement's function, but doesn't execute any of the print statements. And this happens on the same bar number as the second conditional statement is executed for.

The way I have it programmed (see below), it should not loop around and execute Conditional Statement A, on the same bar that Conditional Statement B is executed on. I'm thinking this may be a problem with MC, but I wanted to see if anyone else has had a similar problem.


My code structure is as follows:

Code: Select all

if ((GetAppInfo(aiRealTimeCalc)=0 and DH<> 0 and DL<>0)
or(GetAppInfo(aiRealTimeCalc)=0
AND (((PS =1 and PS2 = 1) or (PS=2 and PS2 = 2)) and (H[1]<H) and (L[1]>L))))
then 
begin
   //There is a function here
        Print ("In Conditional Statement A");
end;

//**********************************************************
if ((GetAppInfo(aiRealTimeCalc)=0 and DH = 0 and DL = 0)) or (GetAppInfo(aiRealTimeCalc)=1) then   
begin

          // Function here that calculates the values for DH and DL
          Print ("In Conditional Statement B");
end;


//--------------------------------------------------------------------------
// PS values are calculated by a function and done for all conditions (i.e. outside the conditional functions.
   

User avatar
TJ
Posts: 6585
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Re: Issue With Conditional Statement Execution

Postby TJ » 08 Oct 2012

pschriber2 wrote:Hi,
I wrote some code that is conditional on whether the calculation is for real time data or historical data, and some conditional values.


The issue I'm having is that when the values DH=0 and DL = 0 are true, the code executes the second statement (correct). But once it has done that, and changed DH and DL to real values, for some reason it executes the first conditional statement's function, but doesn't execute any of the print statements. And this happens on the same bar number as the second conditional statement is executed for.

The way I have it programmed (see below), it should not loop around and execute Conditional Statement A, on the same bar that Conditional Statement B is executed on. I'm thinking this may be a problem with MC, but I wanted to see if anyone else has had a similar problem.


My code structure is as follows:

if ((GetAppInfo(aiRealTimeCalc)=0 and DH<> 0 and DL<>0) or(GetAppInfo(aiRealTimeCalc)=0 AND(((PS =1 and PS2 = 1) or (PS=2 and PS2 = 2)) and (H[1]<H) and (L[1]>L)))) then
begin
//There is a function here
Print ("In Conditional Statement A");
end;

//**********************************************************
if ((GetAppInfo(aiRealTimeCalc)=0 and DH = 0 and DL = 0)) or (GetAppInfo(aiRealTimeCalc)=1) then
begin

// Function here that calculates the values for DH and DL
Print ("In Conditional Statement B");
end;


//--------------------------------------------------------------------------
// PS values are calculated by a function and done for all conditions (i.e. outside the conditional functions.


Can't tell you much with hypothetical scenarios.
Computers are dumb, they will only do what you tell them to do.

Have you walked through the conditional statement on all the permutations?

ps. please use code tag when posting codes.
I have tagged them for you in your post.
Also, I have changed the formatting for you,hope you can see your logic better. It should help your debugging.
These users thanked the author TJ for the post:
pschriber2

pschriber2
Posts: 36
Joined: 24 Apr 2012
Has thanked: 6 times
Been thanked: 2 times

Re: Issue With Conditional Statement Execution

Postby pschriber2 » 09 Oct 2012

Hey TJ,

Yes i have walked through all of the permutations but no luck. If I understand properly, Power Language executes the code from Top to Bottom. So it shouldn't be doing this looping thing.

User avatar
TJ
Posts: 6585
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Re: Issue With Conditional Statement Execution

Postby TJ » 09 Oct 2012

pschriber2 wrote:Hey TJ,

Yes i have walked through all of the permutations but no luck. If I understand properly, Power Language executes the code from Top to Bottom. So it shouldn't be doing this looping thing.


Not necessary from top to bottom,
You have brackets, the inner brackets execute first.


My suggestion:

Convert your conditions to easily understandable names:

eg:

Code: Select all

var:
higher.high(false),
lower.low(false),
outside.bar(false);

higher.high = H[1]<H;
lower.low = L[1]>L;
outside.bar = higher.high AND lower.low;

if outside.bar then
begin
     ....

User avatar
TJ
Posts: 6585
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Re: Issue With Conditional Statement Execution

Postby TJ » 09 Oct 2012

you have to create a table to debug this condition:

Code: Select all

if ((GetAppInfo(aiRealTimeCalc)=0 and DH<> 0 and DL<>0) or(GetAppInfo(aiRealTimeCalc)=0 AND(((PS =1 and PS2 = 1) or (PS=2 and PS2 = 2)) and (H[1]<H) and (L[1]>L)))) then
begin


put each sub-conditions in an excel worksheet and go through them one by one:

GetAppInfo(aiRealTimeCalc)=0
DH<> 0
DL<>0
GetAppInfo(aiRealTimeCalc)=0
PS =1
PS2 = 1
PS=2
PS2 = 2
H[1]<H
L[1]>L

then do the same for secondary conditions:

(
(GetAppInfo(aiRealTimeCalc)=0 and DH<> 0 and DL<>0)
or
(GetAppInfo(aiRealTimeCalc)=0
AND
(
( (PS =1 and PS2 = 1) or (PS=2 and PS2 = 2) ) and (H[1]<H) and (L[1]>L))
)
)
These users thanked the author TJ for the post:
pschriber2

pschriber2
Posts: 36
Joined: 24 Apr 2012
Has thanked: 6 times
Been thanked: 2 times

Re: Issue With Conditional Statement Execution

Postby pschriber2 » 09 Oct 2012

Thanks TJ. I simplified and seperated my statements and it seems to work.

User avatar
TJ
Posts: 6585
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Re: Issue With Conditional Statement Execution

Postby TJ » 09 Oct 2012

pschriber2 wrote:Thanks TJ. I simplified and seperated my statements and it seems to work.

That's good to hear!
These users thanked the author TJ for the post:
pschriber2

pschriber2
Posts: 36
Joined: 24 Apr 2012
Has thanked: 6 times
Been thanked: 2 times

Re: Issue With Conditional Statement Execution

Postby pschriber2 » 09 Oct 2012

Never mind, it still doesn't work. I thought it did but I still get the same issue

pschriber2
Posts: 36
Joined: 24 Apr 2012
Has thanked: 6 times
Been thanked: 2 times

Re: Issue With Conditional Statement Execution

Postby pschriber2 » 09 Oct 2012

So I put a True/False flag within the function that is set to true when conditional statement A is executed. Since my algo was only executing the function in conditional statement A, this flag doesn't get triggered and the function does not execute. It's odd but works.


Return to “MultiCharts”