R code snippet : Read Historical Prices of Stock Index

[This article was first published on SH Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post shows how to read prices of stock indices given symbols as a string.


Read historical prices of stock indices


S&P 500, Nikkei 225, Hang Seng Index, Historical Prices of Stock Index
I collected the symbols of major stock indices at

https://finance.yahoo.com/world-indices


R code


The following R code retrieves historical daily prices of selected stock indices given their symbols as of 2022-08-14.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#========================================================#
# Quantitative ALM, Financial Econometrics & Derivatives 
# ML/DL using R, Python, Tensorflow by Sang-Heon Lee 
#
# https://kiandlee.blogspot.com
#——————————————————–#
# read historical prices of stock indices
#========================================================#
 
graphics.off(); rm(list = ls())
 
library(quantmod)
library(stringr) # trim
 
#————————————————-
# Symbols of stock indices, as of 2022-08-14
#————————————————-
vstr_symbols < 
    Symbol    ,    Name
    ^GSPC     ,    S&P 500   
    ^DJI      ,    Dow 30
    ^IXIC     ,    Nasdaq
    ^NYA      ,    NYSE COMPOSITE (DJ)
    ^XAX      ,    NYSE AMEX COMPOSITE INDEX   
    ^BUK100P  ,    Cboe UK 100
    ^RUT      ,    Russell 2000
    ^VIX      ,    CBOE Volatility Index
    ^FTSE     ,    FTSE 100
    ^GDAXI    ,    DAX PERFORMANCE-INDEX
    ^FCHI     ,    CAC 40
    ^STOXX50E ,    ESTX 50 PR.EUR
    ^N100     ,    Euronext 100 Index
    ^BFX      ,    BEL 20
    ^N225     ,    Nikkei 225
    ^HSI      ,    HANG SENG INDEX
    000001.SS ,    SSE Composite Index
    399001.SZ ,    Shenzhen Index
    ^STI      ,    STI Index
    ^AXJO     ,    S&P/ASX 200
    ^AORD     ,    ALL ORDINARIES
    ^BSESN    ,    S&P BSE SENSEX
    ^JKSE     ,    Jakarta Composite Index
    ^KLSE     ,    FTSE Bursa Malaysia KLCI
    ^NZ50     ,    S&P/NZX 50 INDEX GROSS
    ^KS11     ,    KOSPI Composite Index
    ^TWII     ,    TSEC weighted index
    ^GSPTSE   ,    S&P/TSX Composite index
    ^BVSP     ,    IBOVESPA
    ^MXX      ,    IPC MEXICO   
    ^TA125.TA ,    TA-125   
    ^JN0U.JO  ,    Top 40 USD Net TRI Index
    “
 
#——————————————-
# split symbols and make vector
#——————————————-
df < read.table(text = str_trim(vstr_symbols), 
                 sep = “,”, header = TRUE)
df < as.data.frame(df); df
df$Symbol < gsub(” ““”, df$Symbol)
df$Name   < str_trim(gsub(“[\t\r\n,]”“”, df$Name))
df
nc < nrow(df) # number of index
 
#——————————————-
# read price information
#——————————————-
sdate < as.Date(“2001-01-01”)
edate < as.Date(“2022-08-12”)
getSymbols(df$Symbol, from=sdate, to=edate)
 
#——————————————-
# collect only adjusted prices
#——————————————-
price < NULL
for(i in 1:nc) {
    eval(parse(text=paste0(
        “price <- cbind(price,`",
        gsub(“\\^”,“”,df$Symbol[i]),“`[,6])”)))
}
 
# modify column Name as only symbol
colnames(price) < gsub(“.Adjusted”“”
                        colnames(price))
 
# convert to data.frame with the first column as Date
df.price < cbind(time=time(price), as.data.frame(price))
rownames(df.price) < NULL
 
# partial selection of complete cases 
# by S&P 500, Nikkei 225, HANG SENG INDEX
df.price < df.price[complete.cases(
                     df.price[,c(“GSPC”,“N225”,“HSI”)]),]
 
#——————————————-
# print time series of daily prices
#——————————————-
head(df.price,3)
tail(df.price,3)
 
cs


Running the above R code displays the status of data reading process as follows.

R code snippet : Read Historical Prices of Stock Index


Finally, we can get the collection of individual stock indices.

R code snippet : Read Historical Prices of Stock Index



To leave a comment for the author, please follow the link and comment on their blog: SH Fintech Modeling.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)