stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Nested if (SomeCondition) blocks
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=1367
Page 1 of 1
Author:  SteveHopwood [ Tue Jan 08, 2013 7:46 am ]
Post subject:  Nested if (SomeCondition) blocks

Lifesys has introduced me to the joys of nested if (SomeCondition) blocks. They are really cool. :lol:

From the gms' Double Big Mac V7 mod Steve EA:

Code: Select all

      if (curr == StringSubstr(pair[i],0,3))
         if (CheckIfOpen(pair[i]+postfix) == 0)
            if (CheckMACDH4(StringSubstr(pair[i],3,3)) == -1)
               if (!UseFilter_ADX || CheckADX(pair[i], buy))
                  if (SlopeTimeFrame == 0 || SlopeVal >= SlopeBuyOnlyLevel)
                     OpenBuy(pair[i]+postfix);
OpenBuy(pair+postfix); is only executed if all the previous conditionals pass. I have attached Paul's explanation.

:D
Author:  mobthehop [ Tue Jan 08, 2013 10:07 am ]
Post subject:  Re: Nested if (SomeCondition) blocks

Thanks Steve & lifesys - wondered for the longest time how to do 2 or more conditions ( a la "ducks in a row") as basis for a trade decision

Cheers
Author:  dietcoke [ Tue Jan 08, 2013 12:02 pm ]
Post subject:  Re: Nested if (SomeCondition) blocks

This is absolutely the way to code multiple conditions because if you put multiple conditions in a single if statement, Empty4 always tests ALL the conditions even if the first one fails.
Author:  garyfritz [ Wed Jan 09, 2013 5:55 pm ]
Post subject:  Re: Nested if (SomeCondition) blocks

You can do this as a single "if" with a complex condition, e.g.:

Code: Select all

if ((curr == StringSubstr(pair[i],0,3))
     && (CheckIfOpen(pair[i]+postfix) == 0)
     && (CheckMACDH4(StringSubstr(pair[i],3,3)) == -1)
     && (!UseFilter_ADX || CheckADX(pair[i], buy))
     && (SlopeTimeFrame == 0 || SlopeVal >= SlopeBuyOnlyLevel)
     )
         OpenBuy(pair[i]+postfix);
...but as DC said, Empty4 always tests all of those conditions and you might not want to do that. The multiple if statements should (assuming a competent compiler, but remember this is Empty4 we're talking about) compile into code that's just as efficient as the "&&" code.

But minor little things like efficiency of the compiled code really aren't worth worrying about. A much bigger deal is the "lazy evaluation" DC's talking about. Where most of those tests are calling functions, each of the tests might be an expensive operation. The single-if code using && evaluates ALL of those functions, even if the very first test fails. The multiple-if code quits as soon as one of them fails. That might save you quite a bit.

One thing to remember: the "then" part of an if is a single "statement," whether that's one actual statement or a compound statement in {}'s. Each if is considered a single "statement," no matter how complex the logic is inside the "then" part, so in this construct you don't need {}'s around the "then" parts. That looks cleaner in this nested-if test. But be careful about omitting {}'s in code that you might add to later, like this:

Code: Select all

if (test)
  thenPart;

// then you add more to the "then":

if (test)
  thenPart;
  thenPart2;
You'll find that thenPart2 gets executed whether the test is true or not. Guess how I know. :oops:
All times are UTC Page 1 of 1