|
|
|
|
|
|
|
|
|
We already have necessary knowledge to write the simplest program in MQL-4. I chose script rather than indicator or expert adviser, as there would be too much new information. Let’s write a script that operates as the terminal menu bar «File»- «Save as…» (Ctrl+S). For this we start a standard procedure in MetaEditor – «File»- «Create» (Ctrl+N), then a dialog window appears, put a tick in front of the field «User-defined script»:
|
|
|
|
|
Click «Next», a new dialog window opens. Fill in fields «Name», «Author» and «Reference». MetaEditor will remember fields «Author» and «Reference» and put their readings when creating a new executable file. Click «Done». The script is ready.
|
|
|
|
|
We have got the simplest code, generated by «Adviser Creation Wizard».
|
|
|
|
|
The code consists of three parts:
1.Head
2.Compiler directives
3.Start() function.
Head
|
|
|
|
|
is not a code, as it represents five lines of comments. Comment is any line that starts with a double slash. Compiler neglects comments and pays no attention to mistakes. Comments simplify understanding of the programs. There is no reason to spin out on comments, as later you will hardly remember all the details of the program. As we see, «Name», «Author», and «Reference» meanings are shown as comments. It will be useful when the code is published.
Compiler directives
|
|
|
|
|
don’t actually influence the script work, BUT exactly these particular directives! There are some other directives, which may considerably change the code algorithm. Help in «Compiler directive» suggests a «List of predetermined identifier».
|
|
|
|
|
Start() function
|
|
|
|
|
It is the most important part. Exactly here we set an algorithm that should be executed. Any program in MQL-4 should possess at least one function, this function is called start() by default. The first three lines are comments. For every function it’s necessary to write the minimal comment, which explains the function purpose. It’s better to get used to it at once. It requires just a couple of minutes to be written, but later it will save hours for you and those to whom you’ll give the code. We see that the function is highlighted visually as a block, which allows us to see where one function finishes and the other starts. The function itself may be divided into two parts:
|
|
|
|
|
contains name of the function and type of the returnable reading of this function,
|
|
|
|
|
the body of the function.
The body of the function is always put in the squiggle brackets and at the end of the function (before the closing squiggle bracket) almost always a return function stays. Help on the operator:
|
|
|
|
|
In this case we see that function start() returns the reading 0 (zero). In this case we won’t use this reading, so the line may be easily commented-out. Our task is to display on the chart readings of Open, Close, Low, High, Time and Volume for each bar (candlestick). We may get access to each bar through the index. Thus, we have to make a run on the bars from the last one to zero. For this we have a cycle for() operator. From the help:
|
|
|
|
|
In the open chart usually not more than Bars bars are located. We have to examine all of them, beginning with the first one in history with index Bars-1, and finishing with the last one with a zero index. Let’s announce a variable quantity of integer type, call it index and write a cycle operator:
for (int index=Bars-1; index>=0; index-) { // We'll output High, Low, Open, Close for the bar with index index }
Let’s analyze the cycle:
int index=Bars-1 we announced a variable quantity index and set for it the initial value Bars-1, i.e. we start with the first bar in the chart
index>=0 we established condition under which the body of the cycle is done. Thus, as long as the index is above or equal to zero, the cycle body is being done
index- is equal to index= index-1 . Decrease in a reading by 1 is called decrement, while increase in a reading is called increment. If the index is more or equal to zero, the cycle body is executed, afterwards the index decreases by 1. Thus, some day the index will be below zero and the cycle will break off.
{ // We’ll draw High, Low, Open, Close for a bar with the index index }The function body of the cycle, instead of comments we have to insert the real display function readings. For this we have the Print() function.
|
|
|
|
|
Let’s add Print(High[index],««,Low[index],» «,Open[index],» «,Close[index],» bar=»,index) to the function body of the cycle. It displays readings High, Low etc. in the Expert advisers and indicators register. The script is done. Click F5 (Compile) – if there are no mistakes, the script is ready for use.
|
|
|
|
|
Return to the terminal, open folder «Scripts» and sketch our script to the chart EURUSD D1. Open the terminal window «Terminal»(Ctrl+T), switch to the bookmark «Expert advisers» and see the result of the script work. Once the work is finished the script is removed from the chart: 2006.01.07 23:35:31 OutPrint EURUSD,Daily: removed. In the experts’ Register any information may be displayed with the help of operator Print, this information is shown in two columns. The first column contains the date and time of display, while the second one contains the script’s name, instrument’s name, time-frame and colon, after which the result of the script work is displayed. In this simplest script we used two most necessary functions of MQL-4, for() and Print(). For() is used to do the uniform operations (work in the cycle), while Print() is the most convenient function for the experts and indicators’ work logging and code debugging. If the program code doesn’t work or works wrong, we have to log variables of the program to analyze its work. Usually this procedure is enough to find mistakes in the code.
|
|
|
|
|
Ultimately, «Experts» tab logs contain work results of our script and any other adviser or indicator. If we click the right button of the mouse at any line, the context menu will appear.
Choose «Open», the dialog box will appear:
|
|
|
|
|
Logs of experts (and indicators) are kept in the folder Program FilesMetaTrader 4expertslogs. Every day a new file is created. Our log (where results of script work are kept) is the last by time and date. Let's open it:
|
|
|
|
|
Thus, we fixed our script work, so Print() is very convenient to use for code debugging on-line. The script is here
Go to article «Script as a bridge to Microsoft Excel».
|
|
|
|
|
|