4.2 Downloading data

Use getSymbols to get data from yahoo or Google (default is yahoo)

getSymbols("AAPL")

Let see what is inside the data.

head(AAPL,n=3)
##            AAPL.Open AAPL.High  AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2003-01-02 0.1270859 0.1320419 0.1269972  0.1309798   181428800      0.227260
## 2003-01-03 0.1309798 0.1321301 0.1291213  0.1318645   147453600      0.228795
## 2003-01-06 0.1330153 0.1361127 0.1316875  0.1318645   390532800      0.228795

This is callsed OHLCVA price data. We have opening, high, low, open, close, volume and adjusted closing prices. High and low are the highest and lowest prices of the trading day. Open and close are the opening and the closing prices of the trading day. Volume is the number of shares transacted on the trading day. Adjusted closing price is the closing price that adjusts for event after market closes such as stock splits and dividend.

To extract columns, we use Op, Hi, Lo, Cl, Vo and Ad

Open <- Op(AAPL)   #Open Price
High <- Hi(AAPL)    # High price
Low <- Lo(AAPL)  # Low price
Close<- Cl(AAPL)   #Close Price
Volume <- Vo(AAPL)   #Volume
AdjClose <- Ad(AAPL) # Adjusted close

If you wish to only import at a certain date e.g.,. 2000-01-01 to 2015-09-25, we can restrict the set the data to download.

getSymbols("AAPL", from='2000-01-01',to='2015-09-25')
## [1] "AAPL"

Alternatively, we can load the whole series and restrict using xts function last()

getSymbols("AAPL")
## [1] "AAPL"
AAPL <- xts::last(AAPL,'1 year')
head(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2021-01-04    133.52    133.61   126.76     129.41   143301900      128.9978
## 2021-01-05    128.89    131.74   128.43     131.01    97664900      130.5927
## 2021-01-06    127.72    131.05   126.38     126.60   155088000      126.1967
## 2021-01-07    128.36    131.63   127.86     130.92   109578200      130.5030
## 2021-01-08    132.43    132.63   130.23     132.05   105158200      131.6294
## 2021-01-11    129.19    130.17   128.50     128.98   100384500      128.5692

Alternatively, we can take the first 3 years by using first():

getSymbols("AAPL")
## [1] "AAPL"
AAPL <- first(AAPL,'3 years')
head(AAPL)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2007-01-03  3.081786  3.092143 2.925000   2.992857  1238319600      2.573566
## 2007-01-04  3.001786  3.069643 2.993571   3.059286   847260400      2.630688
## 2007-01-05  3.063214  3.078571 3.014286   3.037500   834741600      2.611954
## 2007-01-08  3.070000  3.090357 3.045714   3.052500   797106800      2.624853
## 2007-01-09  3.087500  3.320714 3.041071   3.306071  3349298400      2.842900
## 2007-01-10  3.383929  3.492857 3.337500   3.464286  2952880000      2.978950

We can also import three stocks at the same time by using a vector.

getSymbols(c("AAPL","GOOG"))
## [1] "AAPL" "GOOG"

Alternatively, we can assign a vector of stocks and import based on the vector.

stocklist <- c("AAPL","GOOG")
getSymbols(stocklist)

The package can also import non-US stocks.

getSymbols("0941.hk")
#If error, try instead the following:
#getSymbols("0941.HK",src="yahoo", auto.assign=FALSE)

If there is an error, try the following instead:

getSymbols("0941.HK",src="yahoo", auto.assign=FALSE)

Now let us see if the data is correct:

head(`0941.HK`)
##            0941.HK.Open 0941.HK.High 0941.HK.Low 0941.HK.Close 0941.HK.Volume
## 2007-01-02        67.25        70.00       67.10          69.6       35293388
## 2007-01-03        69.90        71.80       69.50          70.7       41163203
## 2007-01-04        70.60        70.95       67.30          68.2       37286533
## 2007-01-05        67.50        69.50       66.30          69.5       24502496
## 2007-01-08        67.50        68.40       67.45          67.7       15584163
## 2007-01-09        68.00        68.60       65.55          66.3       17861491
##            0941.HK.Adjusted
## 2007-01-02         40.64518
## 2007-01-03         41.28755
## 2007-01-04         39.82760
## 2007-01-05         40.58677
## 2007-01-08         39.53561
## 2007-01-09         38.71805

Besides prices, very often we are interested in the trading volume. Moreover, we would like to find volume over time: weekly, monthly, quarterly and yearly. We can use apply and sum to calculate the rolling sum of volume to each distinct period.

WeekVoYa<- apply.weekly(Vo(AAPL),sum)
# sum from Monday to Friday
MonthVoYa <- apply.monthly(Vo(AAPL),sum)
# sum to month
QuarterVoYa <- apply.quarterly(Vo(AAPL),sum)
# sum to quarter
YearVoYa <- apply.yearly(Vo(AAPL),sum)
# sum to year

In some case, we are interested in average than the sum. Then we can use apply and mean. Below we calculate the weekly average volume of AAPL.

WeekAveVoClYa<- apply.weekly(Vo(AAPL),mean)