Articles on MQL4
Moving Average (ending)

Moving Average (ending)


for example, forex

We have created the indicator of a simple moving average with the period PeriodMA, now we have to involve the parameter ShiftMA, which is responsible for the indicator’s shift. It is often necessary not only to calculate the indicator’s values in the technical analysis, but to shift them by some number of bars forward or backward. This property is called a shift. In MQL-4 a shift of an indicator is achieved very simply, we only have to set to any indicator index the attribute of shift with the help of the function SetIndexShift(), to do this we write this simple construction in the block init() and get the intended effect.

 
 


We apply our new indicator with a zero shift and the second indicator with a shift equal for example to three to a chart. We can see that the indicator that uses a shift is drawn three bars in advance; in case of a negative shift the indicator is drawn tardily. We may improve the indicator. When we look for the values of our indicator in the data window (Ctrl+D), we see that the name of indicator and its value for the given bar are shown. Often an indicator may contain more than one index, usually the zero index is called by the name of the indicator, other indexes are named as Value1 , Value2 etc. In order to give more meaningful names to indexes there is the function SetIndexLabel(), which sets the displayed name for the chosen indicator. We will use as a name SMA(PeriodMA), thus we showed that this is a simple moving average with the period PeriodMA. Usually this function is also used in the block init().

 
 


If we look at the technical indicator Moving Average, we’ll see that we haven’t used the price type to calculate our indicator. Let’s refer to the Help in ME; seven standard price constants are indicated there:

 
 


We will add a new parameter PriceType and we’ll change the blockstart() so that our indicator will be calculated not only on the basis of the opening prices. If we place the necessary type of price instead of prices Close[i+k] in the cycle for(), we won’t need anything else. We’ll introduce a new additional variable double price and we’ll set its values in accordance with the Help in МЕ. We’ll do it for unification of the user and technical indicator, in the block switch(PriceType) we’ll use as keys the predefined price constants.

 
 

Besides we’ll also make the procedure of forming index mark in the block init() more flexible, now we’ll show not only the period (PeriodMA) and the shift (ShiftMA), but also the price type, on which the indicator is based.

 
 


Now the creation of a simple moving average MA.mq4 is fully completed.

 
 


Now let’s consider one more variant of a moving average. There are often such indicators when the current value of the indicator depends on the previous value and the current price. This can be written as follows: F[i]=F[i+1]*a+Price[i]*b, where a and b - are coefficients, which are usually less than 1. Sometimes it is required that a+b=1. Then F[i]=F[i+1]*(1-b)+Price[i]*b . The less the coefficient b is, the less the indicator reacts to the price change and the more it depends on the previous value of the indicator. For an exponential moving average (EMA) the coefficient b=2/(1+PeriodMA). We see that the more the period of EMA is, the smoother the curve of the indicator will be. Let’s create a new indicatorEMA.mq4 based onMA.mq4. We’ll make small changes in the block init()

 
 


But there appears some halt in the block start(). At the very beginning when we calculate the indicator for the first time, we don’t have the previous value of the indicator, so we’ll have to make some assumption. The first variant is to agree that for a bar with the index Bars-1 the value of the indicator is equal to the value of the price on this bar (High, Open, Close or another price constant, for example {High+Low+Close}/3.0 ), and then make calculations following the algorithm. The second variant – for a bar with the index Bars-PeriodMA the value of the indicator is equal to a simple moving average (SMA), and then again make calculations following the algorithm. The first variant is realized in the user indicator Moving Averages.mq4 , which is included in the distribution package of MetaTrader4 and it is situated in the folder MetaTrader 4expertssamplesindicators . That’s why we will follow the second variant in order to compare them.

 
 

We will take the block of calculating a simple moving average (blue color) from the indicator MA.mq4. After a small modification the indicator EMA.mq4 is ready.

 
 

We can see that there are two similar sections/blocks that are very alike in the code. MQL-4 allows to declare user functions for the similar calculations. We can see the Help on functions in the « MQL4 manual» in the section «Functions». We’ll create the function GetPrice(int PriceMode, int index), we’ll transfer all the calculations in it, and set the type of function double.

 
 

Now we will substitute these blocks by a line of calling the functionGetPrice() and the indicator EMA2.mq4.mq4 is ready. We’ll make the same changes with the indicator MA.mq4 and name it MA2.mq4. You can download them here .

Go to article «Conversion of the Indicator from MQL-2 to MQL-4».

+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?