|
|
|
|
|
|
|
|
Some traders use indicator figures, others believe that price is the best indicator. Though it is believed that it is very important to use different levels in trading. Search for the highest and lowest prices within some definite period of time is one of the most popular methods. In MQL IV there are functions Highest() and Lowest() for this purpose.
Let's suppose that on October 20, 2006 we must find the highest price within the last 26 hours. With this purpose I have added pattern Rectangle to the chart to see the required range of candles.
|
|
|
|
|
To draw object «Rectangle» in the chart the following should be done: choose Menu «Paste» - «Pattern» - «Rectangle».
|
|
|
|
|
Mark the upper left corner of the Rectangle with the mouse left button and drag it to the right lower corner of the desirable Rectangle. Moreover, in the object attributes I have set the color of the Rectangle.
|
|
|
|
|
To get a precise copy of my Rectangle you can set its coordinates:
|
|
|
|
|
There is no H1 candle with opening time 20.00 of October 19, 2006 in the Rectangle. It was done on purpose to understand the syntax of function Highest(). Help system MetaEditor informs
|
|
|
|
|
The function corresponds to the requirements of almost every function in MQL IV: at first there is a symbol, to which the function is applied, then there is a period (time frame). Then there is a time series pointer, as we can search not only for the highest High[] but also for the highest Close[] and Low[]. The next parameter, count, specifies the selection where the value is looked for. The last parameter, start, specifies the first bar of the required selection.
Let's try to write a script which will draw the same object «Rectangle» on the H1 chart of EURUSD. The rectangle covers 26 bars, its high will pass through the maximum price within 26 bars, its bottom will be equal to the minimum price within 26 bars, the right edge will be on the zero bar (bar index is equal to zero), the left edge is on the 26th bar with the index equal to 25 (note it!). With the help of function Highest() we will find the index of the bar with the maximum price High[], for it we write Highest(Symbol(),Period(),MODE_HIGH, 26,0). We have pointed out a search for a bar with the highest High[] within 26 bars from the zero one. Or should I have written 25?
Let's create script TestHighestLowest.mq4 and have a look at it. At first we will add the required external parameters manually.
|
|
|
|
|
I have introduced a new variable of type color, with the help of which we will indicate the color of the rectangle, and variable backGround of zero type, we will use it when setting the desirable type of depiction, whether there will be a fill or not. Value firstBar is presented as parameter start for function Highest() and Lowest().
Let's complete function start():
|
|
|
|
|
We should find out why the time of the first coordinate time1 is calculated this way and why the opening time firstBar is the time of the second coordinate when a rectangle is created (OBJ_RECTANGLE).
Moreover, we will also try to analyse creation of a rectangle. In this example we display a massage with an error code in the log.
The code is ready, let's run it in the chart and change the input parameter a bit:
|
|
|
|
|
We get the following result:
|
|
|
|
|
As we see our script shows the highest and the lowest price within the last 26 bars beginning with the zero one. Press Ctrl+B, select our rectangle and see its characteristics:
|
|
|
|
|
Specially for this example I have chosen a borderline case. Now let's try to execute the script with value countBars=27. Nothing happens when we start it:
|
|
|
|
|
Error handling is of use, the error code value is given in the Help menu of MetaEditor. Let's delete the rectangle created when the script was run for the first time and run the script again:
|
|
|
|
|
As we see, price high within 27 bars has increased, which means that we have understood parameters function correctly. Note that if in the required selection there will be two bars with the same extremums, functions Highest() and Lowest() will return the hint to the bar with a larger index, that is will point at the older bar. It may be important for some algorithms.
There is a disadvantage in our script as we should point out the initial bar (firstBar) as a numeric value. There is a class of tasks for a script to show the index of some necessary bar programmatically. There are special functions for it in MQL-IV PriceOnDropped() and TimeOnDropped():
|
|
|
|
|
Enter function init() and add variables from the example and also add display of multi-line comment:
|
|
|
|
|
Combination “slash and n” means line advance due to which multi-line comments are possible. Let's compile the new variant of the script and add it to the chart (the other objects have been deleted):
|
|
|
|
|
If we execute the script double clicking the mouse, we will get the following:
|
|
|
|
|
We can specify how a script is run and coordinates of the point in the chart where it was drawn. Let's use these data and name a new variant of the script as follows TestHighestLowest2.mq4.
|
|
|
|
|
Let's run a new variant of the script and get the desired result:
|
|
|
|
|
I have described the four new functions. Scripts are available here
Go to article «Some Ways to Build Channels».
|
|