3 APIS and R graphs

3.1 API´s (Application Programming Interface)

3.1.1 Quantmod API

Quantitative Financial Modelling Framework

The (Wickham and Francois 2016) package for R was designed to assist the quantitative trader in developing, testing, and deploying statistically based trading models.

The function getSymbols is a wrapper to load data from various sources, local or remote. One of the more popular and the default is yahoo fiance,
getSymbols(“Symbol”):

library(quantmod)
getSymbols("AAPL")
#> [1] "AAPL"
class(AAPL)
#> [1] "xts" "zoo"

As you can see, the object class is xts.

To eliminate warnings, we could add the argument warnings = F

Getting the data from an specific date:

getSymbols(“symbol”, from=“YY/m/d”,to=“YY/m/d”)

Where: YY= is 4 digit year, m= 2 digit month, d= 2 digit day.

getSymbols("AMZN", from="2020/04/01",to="2022/04/04")
#> [1] "AMZN"

The previous way to download the data store the information in an object on the environment, and by default assign a name to that object, in the previous example would be “AMZN”.

Another way to do it or to store with a different name, form example with the name “data”, is the following:

apple2 <- new.env()
getSymbols("AAPL", env=apple2)
#> [1] "AAPL"
class(apple2)
#> [1] "environment"

The object class is a list. To get the information we have to apply the following code.

apple3<-apple2[["AAPL"]]

The previous method is useful when we have tickers like Bitcoin:

getSymbols("BTC-USD")
#> [1] "BTC-USD"

Because apparently the name of that object in the environment is “BTC-USD”, but if we want to modify it with that name, it would show a debug like: “object ‘BTC’ not found”

bit<-BTC-USD[,4]

This is because even when looks “BTC-USD” the name has these kind of brackets:

# `BTC-USD`

Another alternative, which is helpful when we apply loops to get the information from the environment is:

bit<-get("BTC-USD")
head(bit)
#>            BTC-USD.Open BTC-USD.High BTC-USD.Low BTC-USD.Close BTC-USD.Volume
#> 2014-09-17      465.864      468.174     452.422       457.334       21056800
#> 2014-09-18      456.860      456.860     413.104       424.440       34483200
#> 2014-09-19      424.103      427.835     384.532       394.796       37919700
#> 2014-09-20      394.673      423.296     389.883       408.904       36863600
#> 2014-09-21      408.085      412.426     393.181       398.821       26580100
#> 2014-09-22      399.100      406.916     397.130       402.152       24127600
#>            BTC-USD.Adjusted
#> 2014-09-17          457.334
#> 2014-09-18          424.440
#> 2014-09-19          394.796
#> 2014-09-20          408.904
#> 2014-09-21          398.821
#> 2014-09-22          402.152

If we would like to download intra-day data, we could get the data from Alphavantage, applying the same function getSymbols.

getSymbols("AAPL", src="av", api.key="yourKey", output.size="full",
periodicity="intraday",interval="5min")
#> [1] "AAPL"

To get dividends, the xts object with the ticker name, needs to be in the R environment, in this case “AAPL”:

ap_div<-getDividends("AAPL")

3.1.3 In-house Api

Harvesting the web with rvest (to get tickers).

This code is to read characters from web pages. In this case, to get the tickers from yahoo finance.

# the page of of the criptocurrencies
#yf <- "https://finance.yahoo.com/cryptocurrencies/"

# for the IPC components
yf <- "https://finance.yahoo.com/quote/%5EMXX/components?p=%5EMXX"
html <- read_html(yf)
# To get the node a, wich contains characters 
node <- html_nodes(html,"a")

# To read the text in the node
node<-html_text(node, trim=TRUE)

# To get the elements that have USD (the tickers). For the IPC tickers, replace "USD" with ".MX". For other tickers, print the node object and look for patterns or select by rows.
#tickers<-grep(pattern = "USD", x = node, value = TRUE)

tickers<-grep(pattern = ".MX", x = node, value = TRUE)

# to get only the first 5 tickers

tickers1<-tickers
tickers1
#>  [1] "IENOVA.MX"     "FEMSAUBD.MX"   "ALPEKA.MX"     "GAPB.MX"      
#>  [5] "KIMBERA.MX"    "MEGACPO.MX"    "GFNORTEO.MX"   "AMXL.MX"      
#>  [9] "CUERVO.MX"     "TLEVISACPO.MX" "ASURB.MX"      "GCARSOA1.MX"  
#> [13] "ALSEA.MX"      "BIMBOA.MX"     "PINFRA.MX"     "OMAB.MX"      
#> [17] "CEMEXCPO.MX"   "GRUMAB.MX"     "GCC.MX"        "GMEXICOB.MX"  
#> [21] "AC.MX"         "BBAJIOO.MX"    "LABB.MX"       "BOLSAA.MX"    
#> [25] "GENTERA.MX"    "LIVEPOLC1.MX"  "MEXCHEM.MX"    "KOFL.MX"      
#> [29] "PEOLES.MX"     "SITESB1.MX"
getSymbols(tickers1[1:2]) 
#> [1] "IENOVA.MX"   "FEMSAUBD.MX"

3.2 Basic R-Graphs

The basic function for creating a graph is: plot(x, type = “h”, col = “red”, lwd = 10, “xlab”,“ylab”).

For this example we use 100 rows of the “AAPL” ticker.


getSymbols("AAPL")
#> [1] "AAPL"
app<-AAPL[1:100,4]

In the help of tue function “plot.xy” and “plot.default” we can see the arguments. Plot the HSI close price

plot(app,type="l",col="green", main = "APPL")

To add anhother time series, for example: apple*1.1

plot(app,type="l",col="green", main = "APPL")

lines(app[,1]*1.1,col="red")

Note: After applying the function plot, to add another line, it must be in the same data frame, otherwise it may not appear in the plot.

To adding leggends, the function legends only work with data frames, not xts., then we transform it in dataframe as.data.frame(object).

To add a legend, but it only works for data frames objects, not in xts. Then we need to transform it into data frame.

appdf<-as.data.frame(app)
app_1df<-as.data.frame(app[,1]*1.1)

plot(appdf[,1],type="l",col="green", main = "APPL") 
lines(app_1df[,1],col="red") 
legend(x= "topleft", legend = c("app","app*1.1"),lty = 1,lwd=2,col=c("green","red"))