Embedded TRUE/FALSE options - move on to next function?
Hello,
I am in need of some help regarding an EA I have programmed that has indicators which can be set to true or false, currently I use an else statement to call another function that will run if the existing function is set to false.
The problem with this is that I end up with a huge list of functions, one for each indicator! This brings up errors in the journal when testing.
I need to have one function that will check if the technical analysis is set to true/false (properties) then run it if it is and go onto the next one or not run it if it is set to false and go onto the next one.
| Original Code: | void MACD2() { if (MACD2==1) { if ((iMACD(NULL, NULL,12,26,9,PRICE_CLOSE,MODE_MAIN,2) > iMACD(NULL, NULL,12,26,9,PRICE_CLOSE,MODE_MAIN,3))&& (iMACD(NULL, NULL,12,26,9,PRICE_CLOSE,MODE_MAIN,3) > iMACD(NULL, NULL,12,26,9,PRICE_CLOSE,MODE_MAIN,4)) ) { SMA(); } } else SMA(); } |
| Standardize your indenting. Factor common code. The second argument to iMACD is an int not a string. | double getMACD(int shift){ return( iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,shift) ); } void MACD2() { if (MACD2==1) { if ((getMACD(2) > getMACD(3))&& (getMACD(3) > getMACD(4)) ){ SMA(); } } else SMA(); } |
| Was does MACD2==1 mean? Write self-docu- menting code. | #define MACD2_DISABLE 0 // \ This form of enumeration #define MACD2_ENABLE 1 // > is when you have multiple extern int MACD2 = MACD2_ENABLE; // / options (more than two. void MACD2(){ if (MACD2==MACD2_ENABLE){ if (getMACD(2) > getMACD(3) && getMACD(3) > getMACD(4)){ SMA(); } } else SMA(); } |
| Use booleans when there are only two options. | extern bool useMACD2 = true; void MACD2(){ if (useMACD2){ if (getMACD(2) > getMACD(3) && getMACD(3) > getMACD(4)){ SMA(); } } else SMA(); } |
| Simplify code when possible. | void MACD2(){ if (!useMACD2 || (getMACD(2) > getMACD(3) && getMACD(3) > getMACD(4)) ) SMA(); } |
| Don't embed code. MACD2 shouldn't call SMA. It should return something to the caller. | bool MACD2(){ if (!useMACD2) return(true); if (getMACD(2) > getMACD(3) && getMACD(3) > getMACD(4) ) return(true); return(false) } // Caller should now be if (!MACD2()) return; SMA(); |
| Simplify booleans. If(con) return(true) else return(false) is simply return(con) | bool MACD2(){ return( !useMACD2 || (getMACD(2) > getMACD(3) && getMACD(3) > getMACD(4)) ); }The renaming of MACD2() to something better like isMacd2Ok() is left as an exercise. |
Hello,
I am in need of some help regarding an EA I have programmed that has indicators which can be set to true or false, currently I use an else statement to call another function that will run if the existing function is set to false.
The problem with this is that I end up with a huge list of functions, one for each indicator! This brings up errors in the journal when testing.
I need to have one function that will check if the technical analysis is set to true/false (properties) then run it if it is and go onto the next one or not run it if it is set to false and go onto the next one.
My apologies on how messy (bad) my code is, that is why I am posting here to look how I can improve it. Thank you for your time.