CODING AN EXPERT ADVISOR STEP BY STEP:
The first thing I am going to do is use input statements at the beginning of the code to set up the inputs for the indicators I will be using, which in this case, are going to be Bollinger bands. (Typically, I will usually want to input the default values that are going to be used by any indicators.)
Obviously, these input variables are in the global scope. Hence, they occur
outside any
other functions. In other words, variables defined in the global scope can be seen inside all other functions. This might seem like a convenient way to define variables, but this is not recommended, except that one has no choice when it comes to inputs, which
must be defined in the global scope.
I am using the first line to input the number of periods. Of course, each input statement begins with
input. Next comes the variable/data type (which in this case, is an integer)
int, which is followed by the name of the variable. Note that it makes sense (i.e., it seems rather logical or rational) to begin input names with the prefix:
inp as a standard naming convention so that you always know which variables are input variables. The last part of the input statement is the assignment, which as I just stated, is the number of periods in this instance.
When you fist run the expert advisor, the assignment is the default value that will appear in the input box. (Of course, you need a semicolon to finish the statement.)
Normally, inside code, there are comments, which always begin with a double slash if they are not very/too long. But, note that if you add inline comments here (comments on the same line as input statements) they
replace the name that appears in the input box.
So then, my default assignment is twenty (
20) periods, and by adding a comment, it will appear in the input box as "
Bands periods," which will be a better prompt than seeing the variable name (i.e., InpBandsPeriods).
The next input I have is the decimal number type
double, which is the number of deviations, which I am defaulting to
2 deviations.
And the third input I am using is an
ENUM (or enumerator) statement, which is an applied price type, and I am defaulting it to the close price. (Actually, there is a whole series of ENUMs that are already defined in MetaTrader, though you
can create your own. But here, I am using a predefined enumerator called
ENUM_APPLIED_PRICE.)
Other possible price values include open, high, low, and several others. Of course, it is normal to use the closing price, but by having this input statement here, it lets the user choose which price they are going to apply to the indicator inside the expert advisor.
Also, although ENUMs are just integers and I
could have therefore simply said
input int here, by nameing this as an
ENUM, when I run the expert advisor and get the input box, this will show as a drop down menu that lists the options to choose from, whereas if I simply defined it as an integer, the user would have been able to put in any number they liked, which might not have been a valid applied price.