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.
