Thanks a lot @Dominik Christian Egert.
"Instead of using conditional blocks like right now, you could make use of ternary operator " () ? :" and accumulate a "state-variable" which you then use in a final decision block to execute or skip. (Could be a switch or an if-statement)"
This is valuable suggestion though I have never tried it and just seen them on custom indicators. I will dig deeper into it and revise my code.
If statement look simpler in using :) so got used to it.
I would say A is faster, just because in B you have an extra set variable (the boolean), but it's not very significant. If you want to confirm that, run the profiler (but it will probably highlight other areas of your code)
As a suggestion, if you return in an if clause you don't need to put the "else", anything outside and below is only reached if it doesn't enter the first condition (those would be called "guard clauses").
You can also rearrange your code as Dominik said to avoid repeating calculations
if(STTH01[k+1].fastTrend == SHORT && STTH01[k].fastTrend == LONG) { mPriceSL = STTH01[k].fast; OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment); return; } if (HMAH01[k].trend != LONG) return; if (STTM15[k+1].fastTrend != SHORT) return; if (STTM15[k].fastTrend != LONG) return; if (STTH01[k].slowTrend == LONG && STTM15[k].fastTrend == LONG) { mPriceSL = STTM15[k].fast; OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment); return; } if(STTH01[k].fastTrend == LONG && STTM15[k].slowTrend == LONG) { mPriceSL = STTM15[k].fast; OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment); return; }
You can reverse the conditions and make them guard clauses too (especially those that repeat). Those fragments I highlighted do the same so probably they could also be extracted.
If you want to really improve the performance, make sure that the conditions that are more likely to fail (so you would exit the function) are first in order (that's something you would need to test for)
I would say A is faster, just because in B you have an extra set variable (the boolean), but it's not very significant. If you want to confirm that, run the profiler (but it will probably highlight other areas of your code)
As a suggestion, if you return in an if clause you don't need to put the "else", anything outside and below is only reached if it doesn't enter the first condition (those would be called "guard clauses").
You can also rearrange your code as Dominik said to avoid repeating calculations
You can reverse the conditions and make them guard clauses too (especially those that repeat). Those fragments I highlighted do the same so probably they could also be extracted.
If you want to really improve the performance, make sure that the conditions that are more likely to fail (so you would exit the function) are first in order (that's something you would need to test for)
Dear @Manuel Alejandro Cercos Perez thanks a lot for your valuable suggestions to improve my coding style on if..loop.
'The Guard Clauses" suggestion is something to make if..loop more clear.
I usually have if(!(condition)) return; to filter (guard clauses) for early exit of functions or methods. However still sometimes true or false conditions are so many that makes me use above style of code writing.
I made some other improvements as below:
Instead of using below condition, I have incorporated the the value in STTH01 Structure and call the GetArrayMethod gets it done for 'n' bars at PERIOD_H1 intervals. For position entry condition, I simply fetch value from structure at IsNewBar() of any given time frame or it can be every tick.
STTH01[k+1].fastTrend == SHORT && STTH01[k].fastTrend == LONG
Changed to STTH01[k].isCoT == LONG ...
I am sure suggestions by you and Dominik, will go long way in improving my code writing.
Regards.
Please don't post randomly in any section. Your topic has been moved to the section: Expert Advisors and Automated Trading
Please don't create topics randomly in any section.
Please don't post randomly in any section. Your topic has been moved to the section: Expert Advisors and Automated Trading
Well I am under the impression that once a post is moved to specific section, if I use 'reply' it will come from new section.
Please guide me how to make sure that replies are going from new moved section.
regards
Replies go to the thread, where ever the thread currently is.
My assumptions is confirmed by William. May be you have seen a post before it was transferred to another section or may you just had a bad day :) while writing your post.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Friends
I have a confusion which One of the following "if statements set" is better in terms of CPU performance?
If statement set [A]
If statement set [B]
In my opinion set [B] should be better, please advise.