Technical Analysis in R: There is no doubt about the facilities that computing has brought us in solving complex problems and the way in which it helps us make predictions in both theoretical and practical estimates.

In the financial field, it is no exception, since the different mathematical and statistical methods that can be calculated using a computational language allow future estimates of the prices of various financial instruments or stock market indices to be made.

In this article, a brief introduction will be given on how to perform technical analysis on financial market instruments using R Studio as an R language interface [at the end of this article there is a link to download Annex 1, which shows the instructions for installation instructions and a brief description of the software.

## Japanese candlestick charts.

The information to work with will be the daily price of Grupo México series B shares, which are listed on the Mexican Stock Exchange [BMV: GMEXICOB] (see Annex 1.5).

After we have successfully created our variable df (read Annex 1.4), which will store the open, high, low, and close prices, it is possible to start by viewing the historical behavior of prices through a candlestick chart. The first function of the quantmod library (read Annex 1.3) that we will use will be candleChart().

– Run the following line from the Console:

CandleChart(df,name=”GMEXICOB”,theme = “white”,subset = “2018-01-01/”,TA = “addVo()”)

From the Plots tab, in the third window, you can see a graph like the following:

From the graph above it is possible to notice several things. The first is that, although the entire series begins with a date of 2012, we can cut the series with the subset argument, always putting the date in quotes and with the slash ( / ) at the end, in the previous example, the period is cut to show only the values of 2018. The value in green at the top represents the last closing value and the value in green at the bottom represents the total number of shares traded (volume) on the last day of the period (in this case both are green because the last candlestick is bullish).

## Simple moving average.

In the previous example, it is already possible to identify linear trends or through patterns, however, because this type of technical analysis is usually too subjective, we will choose to identify how prices can move with respect to their historical behavior, for which it is It is possible to use moving averages (moving averages), under the premise of the effect of reversion to the mean 1, to identify when a trend has just ended and another begins.

Note: It is important to mention that the analyzes derived from the use of daily time series may be taken for a long-term investment horizon and, to a lesser extent, for the medium term. It is possible to improve any analysis and reduce the short-term investment horizon as long as the information is available more frequently, for example, monthly, every fifteen minutes, etc.

### Calculation

To calculate a simple moving average, SMA ( Simple Moving Average ), you must first establish an estimation period according to the frequency of the data.

Although there are no standard measures for calculating moving averages, technical analysis usually use multiples of 25 days, referring to the average of the closing prices of the last 25 days that would correspond to a month (not counting Saturdays or Sundays).

In the following example, the moving averages of 25, 50, and 100 days will be used.

As mentioned, each value of the series that makes up each simple moving average is obtained by calculating the average of previous days established and so on until the last value is completed. The quantmod library allows you to include moving averages from the TA argument of the CandleChart() function as follows:

candleChart(dfw,name=”GMEXICOB”,theme = “white”,subset = “2017-03-01/”,TA=”addVo());addSMA(n=25,col=2);addSMA(n=50 ,col=3);addSMA(n=100,col=4)”)

Executing the above line of code from the Console window will add the moving averages to the chart as follows:

- A. addSMA(n=25,col=2) refers to the 25-day moving average in red.
- B. addSMA(n=50,col=3) refers to the 50-day moving average in green.
- C. addSMA(n=100,col=4) refers to the 100-day moving average in blue.

In Graph 2, the observation period has been extended to identify when a trend is able to form in the long term:

The first thing to distinguish from Graph 2 is that the moving average with the longest delay (blue line), that is, the one that occupies a period of 100 days to obtain each value, is further away from the real behavior of prices. . In contrast, the moving average with the least lag (red line) is the one that most closely resembles the actual price movement.

## Selection of moving averages.

In general, in terms of prices, moving averages delay the behavior of a financial instrument because previous prices are taken into account in their measurement. In this sense, the longer the period of estimation of the moving average, the greater the delay and the longer it will take for the real movement of prices to reach the trend, for this matter it is essential to have complete databases so that an estimate It is optimal in case it is required to design a strategy for long-term investments.

Due to the above, a medium or long-term analysis will be closer to reality depending on the moving averages used.

The first technical analysis that we can derive from Graph 2 has to do with the 100-day moving average: it is observed that in the second half of 2017 and at the beginning of 2018 it took a few months for the real movement of prices to be below the trend. to then rebound in the price for at least a couple of times. The simplest idea, based on this trend line, is to buy below it and sell when prices rise above the moving average, always bearing in mind that the best-performing buying or selling opportunities are where actual prices meet. further away from the trend.

The latter also applies to the trend lines marked in green and red, however, how can you know when a trend change begins to take shape and it is time to buy or sell?

## Combination of moving averages.

To answer the above question, analysts often use a combination of moving averages as a strategy to detect when a trend change can be sustained in the long, medium, or short term.

If we go to the beginning of the graph, we can see that the first crossing of the 25 and 50-day moving averages precedes the drop in the price of Grupo México shares. Subsequently, the second crossing of these lines precedes the rebound in the price. The same happens based on the combination of the 25 and 100-day SMAs or the 50 and 100-day SMAs, but in these cases, the signal that precedes a change in the real price trend happens later due to the long delay.

In general, the crossing of two moving averages with a negative direction can precede a contraction in prices and, on the contrary, the crossing of both with a positive direction can precede a rebound in the price. The length of time that the trend is likely to sustain depends on the selection of moving averages that are made.

### Illustration

To illustrate the above, a possible long-term strategy could have been to buy immediately after spotting the trend change at the first positive crossover of the 25 and 50-day moving averages (left arrow) and hold the reversal as long as these trend lines remain above the 100-day moving average. The signal indicating the change in trend would have been at the first crossover of the 25-day SMA with the 100-SMA and would have been confirmed with the crossover between the 50-day SMA and the 100-SMA (right arrow).

The interpretation of the signals in the previous example, and in a general way, falls on the correct use of the combined moving averages and is oriented to the term that we have foreseen for an investment.

In the next article that will be published on the Analysis Network, we will talk about how to get the price data of financial instruments from an Internet source and how to create a signal system so that it appears automatically on the charts.

Want to know about Raising funds or backing when you are a startup