|
|
|
|
|
|
|
|
|
Any trading system consists of signals for opening and closing positions, for trailing protective stop orders. Most expert advisors are written on the basis of standard indicators, included in МТ4, which are called technical indicators in the Help. Let’s consider the simplest system, based on the crossing of two Moving Averages. This indicator has two parameters — the period of averaging (the number of bars used for calculation) and the method of averaging (the type of price). There is the third parameter — shift, but we won’t use it in this example. The situation when a short-term moving average crosses a long-term moving average is usually used for selling, for buying everything is vise versa. We’ll make a script (CrossMASignals. mq4), that will plot buy signals on the chart in the form of blue upward arrows, sell signals in the form of downward red arrows.
|
|
|
|
|
In the picture you can see that on the 21-st of July 2005 the moving average with the period 13 (value 1.2052) crossed from the bottom upwards the moving average with the period 21 (value 1.2050). We will plot the blue arrow at the opening price of the daily bar of 22.07.2005, as we will receive signals on closed bars, this will exclude false signals, when price may several times go up and down during the day. Let’s make the following flowchart :
|
|
|
|
|
Though it is not quite correct to check the intersection of moving averages at the very beginning of the chart (from the bar with index Bars-1), but in this case we won’t have a mistake and we will not analyze so far why the values of averages in the beginning of the chart are equal to zero.
|
|
|
|
|
In our code we need to receive the values of these averages on every bar. We will set two variables maLong and maShort and will assign them corresponding values. From the Help we can see that it is necessary to indicate symbol (NULL), time-frame (0), the values of periods (21 and 13), shift (it will be zero in our case), the type of averaging (simple), the price type (we will apply closing prices) and the index of the bar.
|
|
|
|
|
The fact of intersection of two moving averages can be checked in the following way — on the current bar the short-term moving average is higher than the long-term one, while on the previous bar on the contrary the long-term moving average is higher than the short-term one. That’s why we will set four, not two, variables maLongCur, maLongPrev, maShortCur, maShortPrev.
|
|
|
|
|
I’ve made a picture that shows the connection between the parameters of the indicator on the chart and the parameters of the technical indicator called in the code. For any built-up indicator in MT4 there exists its call in the language MQL-4 in the form of a technical indicator.
|
|
|
|
|
The script is almost ready, we only have to put somehow arrows where it is necessary. We’ll do it with the help of functions for working with graphic objects. In order to create an arrow it is necessary to: 1. create an Object of the type symbol 2. set coordinates of time (X) and price (Y) , to which the object is bound 3. indicate the shape of the symbol and the color with which it will be displayed on the chart.
All graphic objects in MT4 are created with the help of the function ObjectCreate() . Each object at once is assigned a unique name, there can’t be two objects with the same names. If You try to create the second object with the name that already exists it won’t be created, the mistake won’t be output in the terminal (but it is possible to check the fact of a mistake ) in order not to disturb the user. This is the most common mistake in the work with graphic objects, users got an impression that the program doesn’t work, fruitless attempts are made to find a mistake in the algorithm code, while it doesn’t exist - there is the mistake of creation arrays of graphic objects with the same name. We create an arrow with the function ObjectCreate(«unique object name», OBJ_ARROW, handle_window, time, price). The parameter OBJ_ARROW — points that an object of the type arrow (symbol), handle_window — points to the window in which the object will be created (usually it is equal to zero, that points to the «native» window), time and price — coordinates of the arrow. After creating the arrow it is necessary to indicate the code of the symbol — ObjectSet(«unique_object_name», OBJPROP_ARROWCODE, symbol_code) . The function ObjectSet() serves for setting properties of a graphic object, for each type of object its own second type of parameter is required, in our case for the object OBJ_ARROW we can set the symbol code with the help of the parameter OBJPROP_ARROWCODE, You can look for symbols codes in the Help menu of MetaEditor. Besides You can use symbols from the font Wingdings, for which Help is also available.
|
|
|
|
|
Symbols with the codes 241 and 242 will do for us. Then we only have to set the last property — the color of the symbol. Again we will use the function ObjectSet() and will set the property OBJPROP_COLOR either Red or Blue according to the situation.
|
|
|
|
|
The script is ready, but there is the last question — uniqueness of the name for each arrow. There is declaration of a string variable arrowName in the code, this name is used every time when a new arrow is created and when attributes of objects-arrows are changed. The simplest way to solve this problem is to use a variable of integer type, for example int arrowCounter, and to increase the counter of arrows by one every time when a new arrow is created with the help of arrowCounter++. The unique name we will get by addition of expression «arrow» and arrowCounter, we will get names of the type arrow1, arrow2 etc.
|
|
|
|
|
The script is almost ready, it works, we launch it on the chart of EURUSD D1 and arrows appear instantly. We press the keys Ctrl+B — the window with the list of objects attached to the chart appears.
|
|
|
|
|
We have got a «graphic» indicator, its advantage is the fact that arrows remain at their places when time-frame is changed, for example when we change it for Weekly. If we press the button «Edit», we will get into the window of properties of the selected object. I chose the arrow (object Arrow) with the name arrow1. On the tabs «General» and «Parameters» we can see the values that were assigned by the script — object name, color, coordinates time and price, and also the symbol code — 242.
|
|
|
|
|
There is only one unaccounted point — if we try to launch the script for the second time — it either won’t work or will work not entirely correctly. Every time when we launch the script objects with the names arrow1, arrow2 etc. are created, but these objects exist already after the first launch of the script. And that’s why if we have to launch it on the same instrument but on another time-frame, we will have to delete previously all the arrows by hand, we can do it with the help of the menu «Charts» — «Objects» — «Delete all icons». This is not the best way, we can delete objects using the function ObjectsDeleteAll (window_number, object_type). It returns the number of objects, that was deleted. Let’s output this values onto the chart with the help of the function Comment(entry_line). This function is convenient to use in indicators and advisors for reflecting the necessary current information. Now we have a script in which everything is included for a proper work.
The script CrossMASignals may be taken here
Go to article «Functions init() and deinit()».
|
|
|
|
|
|