Importing and Visualizing Stock Market Data in R

In this tutorial, we’ll analyze the French CAC 40 stocks. Listed CAC 40 firms can be found at the Yahoo Finance website.

Getting the Symbols

First, we need to obtain the associated Yahoo symbols for each CAC 40 stock. Example: for Danone S.A, we have the correspnding symbol BN.PA. From Yahoo Finance we use the DataPasta package to copy and paste the entire data table (We have modified the the column’s name % Change to deal with R naming requirement). Then we select the Symbol column.

library(tidyverse)


base_data <- data.frame(stringsAsFactors=FALSE,
         Symbol = c("VIE.PA", "AI.PA", "BN.PA", "ACA.PA", "CA.PA", "ML.PA",
                    "BNP.PA", "RI.PA", "SU.PA", "VIV.PA", "ENGI.PA", "CAP.PA",
                    "LR.PA", "ORA.PA", "AC.PA", "HO.PA", "KER.PA", "GLE.PA",
                    "EN.PA", "SGO.PA", "SAN.PA", "DG.PA", "ATO.PA", "OR.PA", "SW.PA",
                    "UG.PA", "AIR.PA", "MC.PA", "FP.PA", "FTI.PA"),
   Company.Name = c("Veolia Environnement S.A.", "L'Air Liquide S.A.",
                    "Danone S.A.", "Crédit Agricole S.A.", "Carrefour SA",
                    "Compagnie Générale des Établissements Michelin",
                    "BNP Paribas SA", "Pernod Ricard SA", "Schneider Electric S.E.",
                    "Vivendi SA", "ENGIE SA", "Capgemini SE", "Legrand SA", "Orange S.A.",
                    "Accor SA", "Thales S.A.", "Kering SA",
                    "Société Générale Société anonyme", "Bouygues SA",
                    "Compagnie de Saint-Gobain S.A.", "Sanofi", "VINCI SA", "Atos SE", "L'Oréal S.A.",
                    "Sodexo S.A.", "Peugeot S.A.", "Airbus SE",
                    "LVMH Moët Hennessy - Louis Vuitton, Société Européenne", "TOTAL S.A.",
                    "TechnipFMC plc"),
     Last.Price = c(21.93, 123.35, 80.24, 10.26, 15.49, 93.32, 40.86, 158.05,
                    73.12, 25.64, 13.57, 106.35, 61.98, 13.6, 38.3, 102.9,
                    437.6, 22.35, 32.4, 32.33, 77.04, 96.82, 66.92, 236.7, 101.6,
                    19.34, 125.32, 355.2, 44.08, 21.73),
         Change = c(0, 0.15, 0.14, 0.03, 0.06, 0.48, 0.25, -1.05, 0.48, 0.17,
                    0.11, 1.05, 0.62, 0.14, 0.42, 1.2, 5.3, 0.28, 0.42, 0.43,
                    1.05, 1.34, 0.94, 3.4, 1.62, 0.34, 2.3, 6.75, 0.85, 0.75),
        Change_prc = c("0.00%", "+0.12%", "+0.17%", "+0.24%", "+0.39%", "+0.52%",
                    "+0.62%", "-0.66%", "+0.66%", "+0.67%", "+0.82%", "+1.00%",
                    "+1.01%", "+1.08%", "+1.11%", "+1.18%", "+1.23%", "+1.29%",
                    "+1.31%", "+1.35%", "+1.38%", "+1.40%", "+1.42%", "+1.46%",
                    "+1.62%", "+1.79%", "+1.87%", "+1.94%", "+1.97%", "+3.57%"),
         Volume = c(818027, 264260, 461968, 3363436, 772440, 275617, 3304556,
                    167013, 408078, 788907, 2077728, 221310, 258356, 3729428,
                    202482, 178755, 128660, 2969772, 264297, 640992, 1164010,
                    708090, 202814, 322746, 88158, 814983, 432166, 306987, 2264031,
                    777895)
)


Symbols <- base_data %>% select(Symbol)

Loading Stock Prices Data

In order to import the stock prices, we use the getSymbols() function from the quantmod package. Let’s say we are interested in the evolution of CAC 40 Adjusted Prices from the 15/07/2019 to 15/08/2019. The map(~Ad(get(.))) is used to extract the Adjusted Prices from the imported prices.

library(quantmod)

cac_prices <- getSymbols(Symbols$Symbol, 
                     src = "yahoo",
                     from = "2019-07-15",
                     to = "2019-08-15",
                     auto.assign = TRUE,
                     warnings = FALSE) %>% 
  map(~Ad(get(.))) %>% 
  reduce(merge) %>% 
  "colnames<-"(Symbols$Symbol)


head(cac_prices, n = 2) 
##              VIE.PA    AI.PA    BN.PA ACA.PA CA.PA  ML.PA   BNP.PA    RI.PA
## 2019-07-15 21.99244 109.4339 73.37318  11.01  17.9 111.55 38.15968 155.4672
## 2019-07-16 22.00218 109.3450 73.19907  11.12  18.0 110.00 38.48481 155.2201
##               SU.PA   VIV.PA  ENGI.PA CAP.PA LR.PA  ORA.PA AC.PA    HO.PA
## 2019-07-15 74.58144 24.19410 12.68655 113.85 63.28 13.0389 39.94 100.6184
## 2019-07-16 75.51105 24.21354 12.59051 114.60 63.68 13.0634 39.85 101.3894
##              KER.PA GLE.PA    EN.PA SGO.PA   SAN.PA    DG.PA ATO.PA    OR.PA
## 2019-07-15 507.6668 22.815 30.90874 33.885 71.18390 88.37585  78.02 246.1758
## 2019-07-16 518.1149 23.100 31.76812 34.670 71.69518 90.78260  79.24 246.9620
##               SW.PA    UG.PA   AIR.PA    MC.PA    FP.PA   FTI.PA
## 2019-07-15 99.65470 19.87590 125.1056 373.1165 47.71738 21.78312
## 2019-07-16 99.46063 19.95637 126.9842 378.2707 47.59831 21.87972

Using the getSymbols() function, we’ve obtained an xts object. xts or Extensible Time Series are dataframes indexed with dates.

Stock Prices Visualization with highcharter

highcharter offers highly reactive time series visualizations. Suppose we are interested in plotting French banks adjusted prices.

library(highcharter)


plot1 <- highchart(type = "stock") %>% 
  hc_title(text = "Stocks' Evolution of French CAC 40 listed banks") %>% 
  hc_add_series(cac_prices$ACA.PA, name="Crédit Agricole") %>% 
  hc_add_series(cac_prices$BNP.PA, name="BNP Paribas") %>% 
  hc_add_series(cac_prices$GLE.PA, name="Société Générale")

From the above chart, we observe a very small variation. This is not suprising as the the considered period is short (1 month). Let us consider a longer period (1 year). s

Symbol_banks <- c("ACA.PA", "BNP.PA", "GLE.PA")

cac_banks <- getSymbols(Symbol_banks, 
                     src = "yahoo",
                     from = "2018-08-18",
                     to = "2019-08-18",
                     auto.assign = TRUE,
                     warnings = FALSE) %>% 
  map(~Ad(get(.))) %>% 
  reduce(merge) %>% 
  "colnames<-"(Symbol_banks)


plot2 <- highchart(type = "stock") %>% 
  hc_title(text = "Stocks' Evolution of French CAC 40 Listed Banks") %>% 
  hc_add_series(cac_banks$ACA.PA, name="Crédit Agricole") %>% 
  hc_add_series(cac_banks$BNP.PA, name="BNP Paribas") %>% 
  hc_add_series(cac_banks$GLE.PA, name="Société Générale")
Avatar
Mohamed El Fodil Ihaddaden
Ph.D candidate in Economics.

My research interests include Performance Management, Efficiency Analysis and Experimental Economics.

Related