programming exclusively in MQ4 for a year. During that time I have
established my own set of coding standards to help simplify the
understanding of my MQL source code. I submit these standards so that:
1) others can understand my conventions when they see my code and
2) adopt these conventions for their own code if they so desire.
I have revised this post based on the suggestions of Member 7bit.
I am also willing to work with others in establishing coding standards.
MQL Coding Standards v03 by Ron Mauldin
File Naming Conventions
Here is my standard file name:
“AA_ProgramName_Function_TVV.mqX” where...
AA = Authors Initials
ProgramName is a brief description of the program function.
T = Type of program, where:
- I = Indicator
- E = Expert advisor
- N = iNclude
- L = function Library
Function = (Used only with iNcludes) The function of the iNclude where:
- Calc = Calculations
- Anlz = Analyze
VV = Version number
X = standard Metatrader Extensions… h/4/5/etc.
So for instance RM_TimeSuite_L01.mq4 would indicate the program was
written by “RM” (me), the program function is a Time Suite, it is a function
Library with the version number “01”.
MQ4/MT4 Variable Naming Conventions - Data Type Prefix
The first two or three characters of a variable name are lower case and indicate:
- type of variable {f=function, b=buffer array, g=global, l=local, p=parameter, c=constant, e=external},
- data format {i=int, d=double, b=bool, d=datetime, v=void} and
- if the variable is an array… add an “a”.
Here are some examples:
- fvFunction() – a function that returns nothing (void).
- fiHourGMT(int piServer_GMT_Offset) – a function that returns and integer and has a single integer input parameter.
- lsMonth – a local string.
- gdItemValue – a global double.
- ebUseThis – an external global boolean.
- ctPresetDateTime – a constant datetime value.
- bdIndicatorName – One of the eight possible indicator or data buffers and is a double.
- ldaArrayName – An array that is a local double.
MQ5/MT5 Variable Naming Conventions - Data Type Prefix
The first three or four character prefix of a variable name are lower case and indicate:
- kind of variable {f=function, b=buffer array, g=global, l=local, p=parameter, c=constant, e=external},
- data types:
cs = char signed
cu = char unsigned
ss = short signed
su = short unsigned
is = int signed
iu = int unsigned
ls = long signed
lu = long unsigned
bo = bool
st = string
db = double
fl = float
co = color
dt = datetime
en = enumerations
sc = structures
cl = classes
vo = void - if the variable is an array… add an “a”.
Here are some examples:
- fvoFunction() – a function that returns nothing (void).
- fisHourGMT(int piServer_GMT_Offset) – a function that returns and integer and has a single integer input parameter.
- lstMonth – a local string.
- gdoItemValue – a global double.
- eboUseThis – an external global boolean.
- cdtPresetDateTime – a constant datetime value.
- bdoIndicatorName – One of the eight possible indicator or data buffers and is a double.
- ldoaArrayName – An array that is a local double.
Control Structures And Braces
In general, indent each level three spaces (the MetaEditor defaults of 3 spaces when you use the <Tab> key )
and do not include unnecessary spaces and lines… except to line up for clarity.
Conform to the One True Brace Style, the most widely used formatting style for C-Like languages. Never use
the short forms for if/else constructs, always fully write them out with braces, even if it is only a one liner.
This greatly helps readability!
When a parenthesis is "syntactically needed" for either functions or control statements, do not include a space
before or after the parenthesis... i.e. "if(y == x){".
Here is a standard example with comments:
int start(){ for(i=10, i>=0, i--){ if(condition1 || condition2){ shortsingleaction1; }else if(condition3 || condition4){ verylongsingleaction2; } if(condition5 || condition6){ multiaction3; multiaction4; }else if((condition10 || condition11) && //split long statements and (condition12 || condition13)) { //try to align similar conditions switch(condition){ case 1: multiaction5; break; default: multiaction6; break; } }else{ singleaction2; } } return(); }
Insert a space before and after operators:
// right foo = a * (b + c); // wrong foo=a*(b+c); // exception from the rule: for loops, function default arguments // have no space around operators, only after the comma or semicolon // function calls have no space before the opening brace // but while, if, for have one. /** * comment all function declarations this way */ int bar(int a, int b=1) { int i; int x; for (i=0; i<a; i++) { x += i * b; } return(x); }
- Indicator layout template that shows how to separate code so as to be used/reused by both Indicators and Expert Advisors.
- Storing global variables that are only updated once at the beginning of each bar.
- Standard Indicator Loop Setup
- I would be very interested in working with others to establish EA standards and template(s).