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
## 2003-01-02 0.5083427 0.5281666 0.5079888  0.5239188    45357200
## 2003-01-03 0.5239188 0.5285210 0.5164849  0.5274589    36863400
## 2003-01-06 0.5320605 0.5444505 0.5267507  0.5274589    97633200
##            AAPL.Adjusted
## 2003-01-02      0.919821
## 2003-01-03      0.926036
## 2003-01-06      0.926036

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
## 2019-01-02    154.89    158.85   154.23     157.92    37039700
## 2019-01-03    143.98    145.72   142.00     142.19    91312200
## 2019-01-04    144.53    148.55   143.80     148.26    58607100
## 2019-01-07    148.70    148.83   145.90     147.93    54777800
## 2019-01-08    149.56    151.82   148.52     150.75    41025300
## 2019-01-09    151.29    154.53   149.63     153.31    45099100
##            AAPL.Adjusted
## 2019-01-02      155.5824
## 2019-01-03      140.0852
## 2019-01-04      146.0654
## 2019-01-07      145.7403
## 2019-01-08      148.5185
## 2019-01-09      151.0406

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
## 2007-01-03  12.32714  12.36857 11.70000   11.97143   309579900
## 2007-01-04  12.00714  12.27857 11.97429   12.23714   211815100
## 2007-01-05  12.25286  12.31428 12.05714   12.15000   208685400
## 2007-01-08  12.28000  12.36143 12.18286   12.21000   199276700
## 2007-01-09  12.35000  13.28286 12.16429   13.22429   837324600
## 2007-01-10  13.53571  13.97143 13.35000   13.85714   738220000
##            AAPL.Adjusted
## 2007-01-03      10.41635
## 2007-01-04      10.64755
## 2007-01-05      10.57173
## 2007-01-08      10.62393
## 2007-01-09      11.50647
## 2007-01-10      12.05711

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
## 2007-01-02        67.25        70.00       67.10          69.6
## 2007-01-03        69.90        71.80       69.50          70.7
## 2007-01-04        70.60        70.95       67.30          68.2
## 2007-01-05        67.50        69.50       66.30          69.5
## 2007-01-08        67.50        68.40       67.45          67.7
## 2007-01-09        68.00        68.60       65.55          66.3
##            0941.HK.Volume 0941.HK.Adjusted
## 2007-01-02       35293388         44.25044
## 2007-01-03       41163203         44.94978
## 2007-01-04       37286533         43.36033
## 2007-01-05       24502496         44.18684
## 2007-01-08       15584163         43.04243
## 2007-01-09       17861491         42.15236

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)