- how to retain variable if mt4 needs to restart?
- put TimeFrame into variable?
- How to calculate the number of Bars since the start of the trade session?
A #define is not a variable it is a constant declaration.
"Using the #define construction, one can define the symbolic name or symbolic constant at the program start to be the specific symbol string. Later on, the compiler will replace all appearances of this name without quotation marks with the corresponding string. In fact, this name can be replaced by any absolutely arbitrary text, not necessary with digits:"
You use a #define for two reasons that I can see:
- enhanced readability of your code, e.g. using OP_SELL instead of 0
- ease of changing constants, e.g. #define EA_NAME "TEST"
A #define is not a variable it is a constant declaration.
"Using the #define construction, one can define the symbolic name or symbolic constant at the program start to be the specific symbol string. Later on, the compiler will replace all appearances of this name without quotation marks with the corresponding string. In fact, this name can be replaced by any absolutely arbitrary text, not necessary with digits:"
You use a #define for two reasons that I can see:
- enhanced readability of your code, e.g. using OP_SELL instead of 0
- ease of changing constants, e.g. #define EA_NAME "TEST"
Both of those reasons apply to global variables also. 'string EA_NAME = "TEST"' does the same thing. What's the advantage of using #define instead of a global variable?
I think that using #define will cause the script to run just a little faster because the compiler actually replaces the variable with it's #defined value and at run time it doesn't have to look up the value from memory. But a global variable can have it's values changed if desired.
Right now that's what I'm deciding. If the value will never change I use #define, but if there is the possibility of the value changing then I use a global variable.
But I'm still writing my first EA, so I'm still learning and experimenting to see what's possible and/or best way to do things. And asking a lot of questions that the dictionary didn't answer for me.
Both of those reasons apply to global variables also. 'string EA_NAME = "TEST"' does the same thing. What's the advantage of using #define instead of a global variable?
I think that using #define will cause the script to run just a little faster because the compiler actually replaces the variable with it's #defined value and at run time it doesn't have to look up the value from memory. But a global variable can have it's values changed if desired.
IMO that is correct. It's a better use or resources too . . .
But I'm still writing my first EA, so I'm still learning and experimenting to see what's possible and/or best way to do things. And asking a lot of questions that the dictionary didn't answer for me.
Yep, me too . . .
Is there any advantage to using #define instead of a global variable? My gut tells me that a global variable can be changes but a #defined variable can't. Is there any other thing to consider?
When you come back to that portion of the code months from now and see a variable name, you're going to assume its value can change and will have to find out where and why. If you see a #define, you know it's always constant. If you use the standard ALL_CAPITALS, you also know it's a constant where used.
Write self documenting code where possible.
extern int Magic.Number.Base = 20110202; extern int MM.Fix0.Mod1.Geo2 = 2; // Money Management Mode #define MMMODE_FIXED 0 // $ #define MMMODE_MODERATE 1 // SQRT(MMM*AB) #define MMMODE_GEOMETRICAL 2 // MMM*AB #define MMMODE.MAX 2 ... switch(MM.Fix0.Mod1.Geo2){ case MMMODE_FIXED: perChrt = MM.PerChart, ...
#define NO_SL 0 #define NO_TP 0 #define NO_EXPIRATION 0 ... oo.ticket = OrderSend( Symbol(), op.code, size, now.open, Slippage.Pips*pips2points, NO_SL, NO_TP, order.comment, magic.number, NO_EXPIRATION, op.color); ... if(OrderStopLoss() == NO_SL){ ...
When you come back to that portion of the code months from now and see a variable name, you're going to assume its value can change and will have to find out where and why. If you see a #define, you know it's always constant. If you use the standard ALL_CAPITALS, you also know it's a constant where used.
I think you're agreeing with me, that the only real difference between a global and a #defined variable is that the global can be changed.
I didn't realize that it was a standard to use ALL_CAPITALS for #defined variables. It seems fairly obvious now. It makes sense, so when I see it in a function somewhere in 5 months I know that it's #defined.
Is there somewhere that explains all the standards that MT4 programmers use?
Is there somewhere that explains all the standards that MT4 programmers use?
Google C++ coding standards
This is going to get confusing in my head. It is so different than the coding conventions that I use in my other programming.
I notice that almost all script I find violates standard #63 from http://geosoft.no/development/cppstyle.html#Naming%20Conventions
63. The conditional should be put on a separate line. if (isDone) doCleanup(); // NOT: if (isDone) doCleanup();
and #84
84. - Conventional operators should be surrounded by a space character. - C++ reserved words should be followed by a white space. - Commas should be followed by a white space. - Colons should be surrounded by white space. - Semicolons in for statments should be followed by a space character. a = (b + c) * d; // NOT: a=(b+c)*d while (true) // NOT: while(true) { ... doSomething(a, b, c, d); // NOT: doSomething(a,b,c,d); case 100 : // NOT: case 100: for (i = 0; i < 10; i++) { // NOT: for(i=0;i<10;i++){ ...
Are the conventions for MT4 specifically different than for C? Or is it just that the programmers are ignoring then conventions?
This is going to get confusing in my head. It is so different than the coding conventions that I use in my other programming.
I notice that almost all script I find violates standard #63 from http://geosoft.no/development/cppstyle.html#Naming%20Conventions
and #84
Are the conventions for MT4 specifically different than for C? Or is it just that the programmers are ignoring then conventions?
Are there any conventions for MT4 ? if there were I think a link to them would have been provided rather than pointing you to standards for C++, anything is better than nothing, in other words being consistent throughout your code is better than being inconsistent.
If you are the only person seeing your coding then you can set your own standards . . . use some common sense, use meaningful variable names . . .
Are there any conventions for MT4 ? if there were I think a link to them would have been provided rather than pointing you to standards for C++, anything is better than nothing, in other words being consistent throughout your code is better than being inconsistent.
If you are the only person seeing your coding then you can set your own standards . . . use some common sense, use meaningful variable names . . .
I hope to start programming for other people in the Jobs section, so I feel that I should try and make my code look like others.
It's going to take me a while. I'm accustomed to using lower case letters on the front of variables to show scope and type (gsType would be global string, ldt would be local date) and capitalizing just the first letter of the "words" in the variable name.
This is going to get confusing in my head. It is so different than the coding conventions that I use in my other programming.
I notice that almost all script I find violates standard #63 from http://geosoft.no/development/cppstyle.html#Naming%20Conventions
and #84
Are the conventions for MT4 specifically different than for C? Or is it just that the programmers are ignoring then conventions?
- The purpose of coding standards is to make things clearer. They are not gospel. All are broken at will if the coder thinks it makes things clearer.
- #63: I come form the glass teletype era where I only had 24 lines available, so my choice is the right column, (more on the same line not exceeding 80 characters wide.)
if (isDone) doCleanup();
if (isDone) doCleanup();
if (isDone) doCleanup(); else doMore();
if (isDone) doCleanup(); else doMore();
if (isDone) { doCleanupA(); doCleanupB(); } else { doMoreA(); doMoreB(); }
if (isDone){ doCleanupA(); doCleanupB(); } else{ doMoreA(); doMoreB(); }
Also conventions change with available tools. I use Notepad-2 with code folding. So the last example above, folded and partially unfolded would look like:
Again I prefer the right column.if (isDone) x {
X if (isDone){
if (isDone) x { doCleanupA(); doCleanupB(); } else X {
x if (isDone){ doCleanupA(); doCleanupB(); } X else{
- Are the conventions for MT4 specifically different than for C?There are NO real conventions for MT4 except for the 3 space indenting (which I don't use.)
![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