Nested If Statement

Questions about MultiCharts and user contributed studies.
joshweaver05
Posts: 12
Joined: 09 Jan 2014

Nested If Statement

Postby joshweaver05 » 09 Jan 2014

is the following accepted as a nested if statement in MC Easylangue?

Code: Select all

if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then begin
condition30= True;
end
Else begin
if SymbolName = "@US#C" and ( time >=1700 And Time < 1800 ) then begin
condition30= True;
end
Else begin
if SymbolName = "QGC#" and ( time >=1715 And Time < 1800 ) then begin
condition30= True;
end
Else begin
if SymbolName = "QSI##C" and ( time >=1715 And Time < 1800 ) then begin
condition30= True;
end
Else begin
if SymbolName = "@BOH14" and ( time >=14141505 And Time < 2000 ) then begin
condition30= True;
end
Else begin
if SymbolName = "@SB#C" and ( time >=1400 And Time < 230 ) then begin
condition30= True;
end
Else begin
condition30= False;
end;end;end;end;end;end;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Nested If Statement

Postby TJ » 09 Jan 2014

I would format the matching BEGIN and END at the same tab for debugging.
eg.

Code: Select all

if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then
BEGIN //1
condition30= True;
END //1
Else
begin //2
if SymbolName = "@US#C" and ( time >=1700 And Time < 1800 ) then
begin //3
condition30= True;
end //3
Else
begin //4
if SymbolName = "QGC#" and ( time >=1715 And Time < 1800 ) then
begin //5
condition30= True;
end //5
Else
begin //6
if SymbolName = "QSI##C" and ( time >=1715 And Time < 1800 ) then
begin //7
condition30= True;
end //7
Else
begin //8
if SymbolName = "@BOH14" and ( time >=14141505 And Time < 2000 ) then
begin //9
condition30= True;
end //9
Else
begin //10
if SymbolName = "@SB#C" and ( time >=1400 And Time < 230 ) then
begin //11
condition30= True;
end //11
Else
begin //12
condition30= False;
end; //12
end; //10
end; //8
end; //6
end; //4
end; //2

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Nested If Statement

Postby TJ » 09 Jan 2014

If I read you correctly, you do not need the extra BEGIN.
This is not a nest, it is a cascade.

Code: Select all

if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then
begin
condition30= True;
end
Else // <--no begin here
if SymbolName = "@US#C" and ( time >=1700 And Time < 1800 ) then
begin
condition30= True;
end
Else // <--no begin here
if SymbolName = "QGC#" and ( time >=1715 And Time < 1800 ) then
begin
condition30= True;
end
Else // <--no begin here
if SymbolName = "QSI##C" and ( time >=1715 And Time < 1800 ) then
begin
condition30= True;
end
Else // <--no begin here
if SymbolName = "@BOH14" and ( time >=14141505 And Time < 2000 ) then
begin
condition30= True;
end
Else // <--no begin here
if SymbolName = "@SB#C" and ( time >=1400 And Time < 230 ) then
begin
condition30= True;
end
Else // <--no begin here
condition30= False;
// <--no END here

joshweaver05
Posts: 12
Joined: 09 Jan 2014

Re: Nested If Statement

Postby joshweaver05 » 09 Jan 2014

I was not aware of the cascade syntax so basically in this format the statement says:

if none of the IF statements are true then condition30 = false.

which is exactly what i want.

is my above logic correct?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Nested If Statement

Postby TJ » 10 Jan 2014

I was not aware of the cascade syntax so basically in this format the statement says:
if none of the IF statements are true then condition30 = false.
which is exactly what i want.
is my above logic correct?
Your code does the job, but it does not have to have the extra BEGIN/END in the logic.

If all you wanted is as described above, then all you needed is this:

Code: Select all

if (SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ))
or (SymbolName = "@US#C" and ( time >=1700 And Time < 1800 ))
or (SymbolName = "QGC#" and ( time >=1715 And Time < 1800 ))
or (SymbolName = "QSI##C" and ( time >=1715 And Time < 1800 ))
or (SymbolName = "@BOH14" and ( time >=14141505 And Time < 2000 ))
or (SymbolName = "@SB#C" and ( time >=1400 And Time < 230 ))
then condition30= True
Else
condition30= False;

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Nested If Statement

Postby TJ » 10 Jan 2014

Here are more examples of loops

viewtopic.php?f=16&t=7351&p=32845#p32845

joshweaver05
Posts: 12
Joined: 09 Jan 2014

Re: Nested If Statement

Postby joshweaver05 » 10 Jan 2014

thanks for your help, that page is a lot of help. quick question that is different than nested statements but directly related to the above code. I have inserted the above code because MC will send out a stream of limit orders after market hours and my broker does not like this. if I have the following statement:

Code: Select all

if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then begin
condition30= True;
end;


it still seems to send the orders out after the close of 1615, for this to work properly do i need it to say 1610 (its 5 minute bars) so its not sending order out at the end of 1615?

thanks

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Nested If Statement

Postby TJ » 10 Jan 2014

thanks for your help, that page is a lot of help. quick question that is different than nested statements but directly related to the above code. I have inserted the above code because MC will send out a stream of limit orders after market hours and my broker does not like this. if I have the following statement:

Code: Select all

if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then begin
condition30= True;
end;

it still seems to send the orders out after the close of 1615, for this to work properly do i need it to say 1610 (its 5 minute bars) so its not sending order out at the end of 1615?
thanks
I can't tell with just the tiny snippet...

What is your chart resolution?

joshweaver05
Posts: 12
Joined: 09 Jan 2014

Re: Nested If Statement

Postby joshweaver05 » 11 Jan 2014

the chart resolution is 5 min ES chart, which is open from 1800 to 1615 EST. the issue being with limit order strategies it send the order at the end of the bar and when MC does this and the market is closed it will send unlimited orders until the order is no longer rejected. so i devised the below code to not send any orders after market hours.

Code: Select all

//////to stop limits from going out after market hours
if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then begin
condition30= True;
end
else begin
condition30= false;
end;
//////


//////limit exit
if condition30 = false then begin
if marketposition = 1 then begin
Sell ("LT") next bar at ZZY limit;
end;
the problem being even with the above code it will send a limit order out at 1615 so i was thinking because orders are sent sent bar then it would have to be at 1610?

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Nested If Statement

Postby TJ » 11 Jan 2014

the chart resolution is 5 min ES chart, which is open from 1800 to 1615 EST. the issue being with limit order strategies it send the order at the end of the bar and when MC does this and the market is closed it will send unlimited orders until the order is no longer rejected. so i devised the below code to not send any orders after market hours.

Code: Select all

//////to stop limits from going out after market hours
if SymbolName = "@ES#C" and ( time >=1615 And Time < 1800 ) then begin
condition30= True;
end
else begin
condition30= false;
end;
//////
//////limit exit
if condition30 = false then begin
if marketposition = 1 then begin
Sell ("LT") next bar at ZZY limit;
end;
the problem being even with the above code it will send a limit order out at 1615 so i was thinking because orders are sent sent bar then it would have to be at 1610?
Yes, you have to stop the autotrade before the last bar.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Nested If Statement

Postby bowlesj3 » 11 Jan 2014

You might want to reverse that last bit of code like this. It makes it easier to read (faster to understand). I took a guess at the 0930 of course.

Code: Select all

if SymbolName = "@ES#C" and ( time >=0930 And Time < 1615 ) then
begin
if marketposition = 1 then
Sell ("LT") next bar at ZZY limit;
end;
Or even simpler.

Code: Select all

if SymbolName = "@ES#C" and time >=0930 And Time < 1615 and marketposition = 1 then
Sell ("LT") next bar at ZZY limit;


Return to “MultiCharts”