This can't be right, can it?

 

Do to lack of programming skill my buying function looks like this:

isBuying = (DMAsteepness > DDS && H4MAsteepness > H4S && triggerema > NormalizeDouble(Ask,5) && adxblack > 35 && adxblack < 55 && adxblackp < 55 && atrcurrent < 0.0070 && (Hour()>9 && Hour()<24)

Should it look something more like this? It works the way it is (above), but i'm looking for it to be as efficient as possible while still executing when it should. Thanks for the help.

If(DMAsteepness > DDS)

{

If(H4MAsteepness > H4S)

{

If(triggerma > NormalizeDouble(Ask,5)

{

If(adxblack > 35 && adxblack <55)

{

If(adxblackp < 55&& atrcurrent < 0.0070 )

{

If((Hour()>9 && Hour()<24)

{

isBuying == True

}

}

}

}

}

}

FXPC

 
FXpipclash:

Do to lack of programming skill my buying function looks like this:

isBuying = [...]

Should it look something more like this? It works the way it is (above), but i'm looking for it to be as efficient as possible while still executing when it should.

Have a look at blogzr3's post in 'which is faster nested if or &&'. In the example you're providing, the difference in speed between the two versions is absolutely trivial, and my opinion would be that you should choose between the versions based on readability of the code, not based on the fact that one version is less than a microsecond faster per call. (The clearer version can be the same as the faster version, and it's a subjective decision.) But blogzr3 is absolutely right that the position can change if you are comparing the results of heavyweight functions.


You can try out a crude check of the comparative speeds of your two versions using the attached script.

 

MQL4 specifies that it does not exit a condition early.

.

The condition: if(true || false) could afford to leave early since the first part was satisfied (and it does so in other languages) and will obviously end up true but it doesn't in MQL4. By nesting everything, you gain performance but you have to decide when it is worth sacrificing cleanliness for optimization. In the above case, you should put it all inside the same condition. But in cases where you have dozens of indicator calls inside the same function, it might be better to nest the conditions inside each other so as to avoid heavily using computer resources uselessly when you KNOW the end result will be false anyway.

.

Jon

 

Jon:

Thank you very much for your response. Am I also correct in my assumption that I should place the sub condition that is met the LEAST amount of time (as long as it isn't dependent upon a prior condition) at the beginning of the condition so if it registers as FALSE, the rest of the condition doesn't need to be determined?

Thanks,

FXPC