Articles on MQL4
It catches your eye, but the code can't catch it

It catches your eye, but the code can't catch it


for example, forex
A completed MTS in the form of a program in MQL-4 – is the result of laborious work. At first an idea appears from observations, and then this idea is checked with the help of manual testing on historical data. The entry date and price, and the reason of exit are written down, profits and losses are counted. If manual testing confirmed potential profitability of the trading approach, it’s time to formalize the algorithm of the trading system.

But not always one manages to formalize entry signals; often it is a very difficult task. A man can estimate visually the current situation in whole, but he finds difficulty in writing the rules in the algorithmic language. Is MT4 helpless here? There is a partial way out of this situation. If we could prepare signals for the expert advisor (pending orders placing, market entry etc.) manually, while with the help of the tester we would find optimal values for the protective stop, the level of Take Profit, sort out different variants of trailing…

This variant is not bad, but the prospect of typing manually the date, type and direction of a signal in a file may absolutely discourage you. We will try to resolve the first part of the task (creation of the file of signals) with the help of plotting arrows of different shape and color on the chart. Then we will process all these arrows and will write their coordinates in a file. We open the chart and choose the arrow type.

 
 


We plot the arrow in the needed place and set its color, and then we write the description of the signal (sell limit).

 
 


The next arrow must have the «buy limit» signal and be blue. In all there must be at least 50-60 such arrows… Optimism has withered away. Each arrow requires 8-10 mouse clicks (count yourself). But there is a way to make this process easier a bit, for this we will use a script with an infinite loop. We will call it SmartArrow.mq4 (the idea of MetaQuotes). We create with the help of wizard:

 
 


Until we give the instruction to stop the work the script will do something in the infinite loop (see the help on IsStopped()). This script gives the processor no peace. That’s why first of all we will insert the Sleep(1000) function in the loop. This function frees the processor at least for a second.

 
 


The next step is to study how to process some event. We can’t choose by means of the script the arrow type in the menu and stick it to the cursor, but we may track movement of graphical objects on the chart. We insert the ArrowPosChanged() function.

 
 


One may check the change of location of any graphical object with the help of the ObjectGet(ObjectName, PropertyType) function. Object arrow has only two coordinates – first coordinates of time and price. Besides we will change the arrow’s color and type.

 
 


The fact of changing the coordinate may be find out by comparing the value of the coordinate at the current moment with its value at some previous moment. That’s why we will introduce global variables curPrice, prevPrice, сurrTime and prevTime. We’ve got the following code:

 
 


We added checking, if the arrow with the name «SmartArrow» isn’t on the chart the script ends its work. Checking of the first call of the function doesn’t influence the work of the script, makes the algorithm more correct – when the ArrowPosChanged() function is called for the first time there is no false return of the value true. We launch the script for checking:

 
 


Now we will give to the script the arrow with the name «SmartArrow» to work with it. For this we will simply rename any arrow on the chart.

 
 


We launch the script and receive a message (I added output Comment()).

 
 


We save this variant of the script and continue to work on it. First of all at launching the script we will create our arrow «SmartArrow» automatically and will set all attributes for it. For this we will add the init() function.

 
 


We write signals for the expert advisor, the expert advisor’s testing starts no sooner than on the hundredth bar from the end, that’s why we have created our arrow almost at the very end of the chart. We delete our script (it is still looping on the chart, do you remember?), we compile SmartArrow2.mq4 and launch it on the chart

 
 


The launch went off successfully, the tab «Experts» confirms, the block of checking the first call of the function works.

 
 


We scroll the chart to the very end, looking for the green circle, but fail to find it … What is wrong, why doesn’t it work? With the help of the hot keys Ctrl+B we look through the list of objects and find our arrow – it is at the same place where it was!

 
 


Here we remember that almost any function in MT4 returns the error’s code, if we add processing of this error perhaps we will understand what is wrong.

 
 


We compile, launch and look in the tab «Experts»: there is no error. The error is in our algorithm, the situation when there is no arrow on the chart at launching the script is processed, while presence of this arrow glitches. We will modify the code once again init():

 
 


Now the code works and we find the green arrow where it should be. We only have to write processing of such event as «Arrow moving». Let’s agree that if the arrow appears to be below the Low bar , we turn it into the arrow «Up»(code 241) of the blue color, if it is above the High bar, then we turn it into the arrow «Down» (code 242) of the red color (we mark the entry of signals limit). You may choose any other code for arrows; they are given in the help menu of ME:

 
 


If the arrow is between High and Low – we simply swear because of the awkward manipulation of the arrow. At the same time we don’t forget to output in comments the coordinates of the arrow in terms of time/price.

 
 


The script works, only sometimes it doesn’t manage to change the parameters of the removed arrow. It occurs in case if at the moment of changing the user holds the arrow. We will add processing of this error (i.e. a situation when parameters of a graphical object must be changed, but they aren’t). You may see the code of this error processing in SmartArrow2.mq4. We perform a compilation and launch the script, we move but don’t release the arrow and watch the output in the tab «Experts».

 
 


We look into the “Error codes” and find out that “Object doesn’t exist”. It means that the object isn’t available for the program as it is used by the user and that’s why such a mistake is returned. The reason is clear, now we have to think out how to resolve such an exception case. We could launch the loop of setting the needed parameter in case the error 4202 is returned, but it is always possible to find a simpler decision. You may see it in SmartArrow3.mq4.

At last we have overcome visual errors, now we have to get the file of signals. The most natural decision is to use arrays. Each time when a change of the arrow’s coordinates is determined the parameters of the arrow are changed. At this moment we could write the following parameters in an array: time, price and type/code of the arrow (up down). We declare an array Arrows[1000][3]. A thousand of arrows must be sufficient for us. What type of array do we need? The time and code of the arrow are of the int type, while the price is of the double type. We will consider both variants.

 
 


The variant with the double type is simple:

 
 


The variant with the integer array is slightly more complicated. We have to reduce the price with Digits decimal places to an integer. There are two possible variants:

 
 


At last we went through all the points/signals. Now we have to finish the work of script and plot all the stored arrows. The most suitable way is to do it in deinit().

 
 


After the script is deleted all our arrows will be plotted on the chart. The first part – plotting of arrows on the chart – is done. The final variant is saved as SmartArrow5.mq4. All the variants of the scripts are here
Go to article «Reading from the file».

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?