An algorithm is a description of the actions and conditions performed in your program. Let’s analyze the compilation of the algorithm using the example of the construction of the ATR indicator (see Fig.246).

Figure 246. The ATR indicator.

In the TRANSAQ help, we are looking for information about this indicator.

ATR Indicator (Average True Range)

Formula:

ATR = EMA(TR, N)

EMA – Exponential Moving average

The formula looks like this:

EMA = Pc * K + EMAv * (1 — K), where K = 2/n — 1, n is the averaging period, Pc is the price today, EMAv is the value of EMA yesterday.

N is the period of the indicator

TR = max{H – L, H – C(-1), C(-1) – L}

C is the closing of the period,

L is the minimum price of the period

H – maximum price of the period

——————————————————————————–

ATR estimates the absolute volatility of the paper in the form of an average range of price movement per bar, taking into account the closing of the previous bar. Most often, ATR is used to determine the level at which a stop loss is placed. For example, the current value of ATR is 20 rubles, and in the last month ATR reached the value of 30 rubles. This means that, on average, the paper moves by 20 rubles, but there is a possibility of a sharp movement by 30 rubles. Therefore, 30 rubles can be used as the value of the maximum movement against the trader, after which the position should be closed.

Now let’s make an algorithm for calculating the indicator values for each candle, and then consider the listing code itself.

The algorithm of the ATR indicator.

1. In the programming language, a variable is a language construct that stores a numeric value that can change. Let’s introduce a variable of the averaging period. Let’s call it “period”. Let’s set it to period = 27. The type of this variable is “extern”, i.e. it will be reflected in the indicator settings.

2. Let’s introduce a new variable “tr” to calculate the parameter TR for our indicator. Calculate TR = max{H – L, H – C(-1), C(-1) – L}, i.e. equal to the maximum value of three parameters: the difference between the maximum and minimum of the next (right) candle, the difference between the maximum of the next candle and the closing price of the first (left) candle, the difference between the closing price of the first candle and the minimum of the next candle. In the variable tr, we write the maximum of these three values.

3. After calculating the TR parameter in the INIT function, we can draw the first line of our indicator. To do this, assign the desired value to the desired value of the array of line values: line[0][1] = tr. This entry means that we put the value of the variable TR at the first point of the first lnnii. The first point is under the first candle.

4. The basis of any indicator is the calc() function, which contains a formula for calculating the indicator. The result of the indicator is displayed on the chart by assigning values to the array line[]. The first candle is the leftmost one on the chart, the last one is the rightmost one. The CALC function runs through all the candles displayed in the window from left to right.

5. In the INIT function, we will have to specify the number of candles for which the CALC function is not called. This is done using the setInitCandles(2) function, i.e. we set 2 candles.

6. In practice, there are practically no indicators that use only the values of the current candle. Almost all indicators are calculated using at least some history, and often using their previous final and intermediate values. To refer to the value beyond the boundaries of the current candle to the used candle parameter (high, low, close, volume, etc.), it is necessary to add its shift relative to the current candle in square brackets.

7. In addition to calc(), we will define another function init(). This function, if defined by the user, is called once before TRANSAQ starts calling calc() for each candle. It turns out to be useful for performing preparatory actions before calculating the indicator, and in particular for determining candlesticks for which you do not need to call calc(). All indicator values that have not been explicitly set by the user are considered to be zero.

8. The meaning of the setInitCandles(2) function used is transparent — it indicates the initial number of candles for which it is not necessary to calculate the indicator using the calc() function. The reasons for using this function may be different — the need for the calc() function for a certain amount of history, the need to set an initial value using a formula other than calc(), and other possible reasons that will be discussed in the following sections. It should be noted that when referring to the candle parameters inside the init() function, it is assumed that we call it for the very first candle. The expression line[0] = close; will substitute close of the first candle for the graph. As with any candle parameters, the init() function can refer not only to the current candle, but also to candles with some shift relative to the current one. Here is the difference between the calc() and init() functions. Since the init() function is called relative to the very first candle, the shift of parameters relative to it cannot be negative – this will lead to access to inaccessible data.

9. The calc function will be called at the moment of drawing the current candle. So we need to record in this function the actions for drawing further pieces of our indicator lines.

10. In the CALC function, we calculate the value of the variable tr again as a maximum of three values. Now we take the maximum and minimum for the current calc candle for the cycle, and the closing price for the previous candle, for which we will set the offset index -1, i.e. close[-1].

11. Set a new variable alpha = 2 / (period + 1). We will specify its type var, i.e. a regular variable.

12. In the array of points of line 1, for the current line that the calc function is currently processing, we add a new value line[0] = alpha * tr + (1 – alpha) * line[0][-1]. line[0][-1] is the value of the previous point of our line under the previous candle. 13. Actually, next we create the code of our program.