|
|
|
|
|
|
|
|
MetaTrader4 offers great opportunities for analysis of the financial instruments. Many traders suppose this platform to be the best for the technical analysis. Other opportunities should not be refused from. Data recording to the log file of MT4 is not always convenient, while MQL-4 has functions of work with the file system. Save script OutPrint with a new name WriteFile. It’s necessary to change the algorithm slightly. Insert the opening file function FileOpen() before cycle operator for(), insert the closing file function FileClose() after the cycle, substitute the function Print() with the filing function FileWrite(). It’s necessary to create a handle object-file beforehand for manipulations with the file. The file handle has an int type. FileOpen() has to be given a parameter, file name that has a string type. Files, which are opened in MQL-4 are always kept only in folder C:Program FilesMetaTrader 4expertsfiles (except files used in the tester) for the purpose of safety. Logging to the other directories is forbidden. We’ll log in format CSV, which is understood well by Microsoft Excel. The opened file should be closed at the end of the script work, otherwise other programs won’t get the full access to it.
Let’s try to check a simple idea, prices behavior at different weekdays is not coincidental. For this we’ll record the difference between the closing and opening prices in points (Close[i]-Open[i])/Point.
Point is a predefined variable that contains the point value (minimum permissible price change) for the current instrument. Moreover, we have to write down a weekday of the current daily bar TimeDayofWeek(Time[i]) and year TimeYear(Time[i]). Let us suppose that we buy on the day opening and sell on the day close. As prices are built according to Bid in MT4 (selling price), while buying is made according to Ask (buying price), we have to take into consideration the spread of the instrument. Spread of the symbol may be got through function MarketInfo(Symbol(),MODE_SPREAD).
I remind that execution in operator for() is done in the following way:
|
|
|
|
|
At first, the cycle initialization is done. For this initial level of counter index is assumed. I marked the block by S mark. This block is executed once at the beginning.
Further on the conditional test is done – block 1. If condition is fulfilled, the cycle body starts fulfilling – block 2. Once the cycle body is fulfilled the counter is changed in block 3 – index--. 3 blocks passing makes one timing period of cycle for(). Further on circle-wise – 1, 2, 3 while conditions of block 1 are fulfilling. Once the condition is violated (in our case counter index is below zero), the cycle will end and control will be given to the functions following function for().
Our task is to write into the file all the values since 2001 through 2005 (as an example). Cycle for() passes all the bars in our script from the beginning of the history until now. Thus, we do not have to write bars, which refer to years before 2001 and after 2005. For this purpose there are continue and break functions. Let’s modify the script in the following way.
|
|
|
|
|
Lets add to the functions a conditional function if(), where we’ll check the terms of the year for each bar. Information about the conditional function is given in MetaEditor, our script just helps understand the logic of the functions use.
Now the cycle body is fulfilling in another way. Before the function Print() we have two checks. The first one finishes work with the current index and returns work to block 1 of the cycle (in case the bar year is before 2001), the next functions of the cycle body are not fulfilled. The second one fully completes the cycle work (in case the bar year with the current index is after 2005).
Now we have to add the blocks of opening and closing files and modify output to log for writing into the file. Now our script consists of three operations/blocks.
|
|
|
|
|
The output levels will be divided by semicolon in the FileWrite() function, because we indicated «;» as a separator for the file opening. It helps open the file in Microsoft Excel, where each level divided by semicolon will be put into its own column. We should also name the columns to understand figures better. For this we'll add a writing of the heading of the output table. Don’t forget to fill in the year, weekday for each bar, spread and difference between the opening and closing prices in the items.
|
|
|
|
|
Work in MetaEditor is finished here. We should compile and execute the script in chart EURUSD D1. Now start Excel and open the file.
|
|
|
|
|
Rearrange limits of the columns.
|
|
|
|
|
Now we may use the simplest facilities of Excel. Initiate auto filter.
|
|
|
|
|
Column titles will be able to filter the data on these columns.
|
|
|
|
|
As an example I will take only Fridays. That is, the table will display only the lines which fall on Fridays within 5 years from 2001 till 2005. Let's do the following:
|
|
|
|
|
That is in column «Weekday» we have put a condition, equal 5 (5 means Friday in MT4).
Now we can count all Fridays, sum up Close-Open figures of Fridays, for it the lowest box in column «Close-Open» should be activated and sign «Sum» should be pressed on the toolbar.
|
|
|
|
|
Readings for auto-summing are filled in automatically. Click Enter and get a sum in items. The same should be done with column «Spread».
|
|
|
|
|
We’ve got the following result: if we bought EURUSD on the day opening every Friday for five years and closed a deal on the day closing, we would take profit of 652 points (1435-783) for those days after spread of 783 points subtracting. The data are nominal, as we didn’t took slippage into consideration. We may check other weekdays in the same way. Filter on the year may be initiated additionally.
|
|
|
|
|
The last thing we may build Open, High, Low, Close diagram on the back of our data. For this select the data for the needed period (I've selected a small part of 2003), click «Diagram wizard» and choose type «Stock».
|
|
|
|
|
We have the following diagram. Charts are better in MT4.
|
|
|
|
The script may be taken here
Go to article «Orders in MetaTrader4».
|
|