getReturns (stockPortfolio package)

getReturns(ticker, freq=”month”, get=”overlapOnly”, start=”1970-01-01″, end=NULL)
Retrieve stock performance data from Yahoo! Finance. Use the returned object to fit a stock model (via stockModel) and then estimate the optimal portfolio based on that model (via optimalPort).
  • ticker – A character vector of stock tickers.
  • freq – Indicate what resolution should be retrieved: "month" (the default), "week", or "day".
  • get – Indicate whether the processed returns should include only overlapping dates for the stocks (the default) or whether all processed returns data should be provided ("all").
  • start – The earliest date to begin retrieving stock data, input as "YYYY-MM-DD".
  • end – The end date as "YYYY-MM-DD" (default is the most recent trading day).

Example. Below stock data is retrieved for Apple ("AAPL"), Microsoft ("MSFT"), and Red Hat ("RHT"). Many of the components of the returned object are examined and the stock data is plotted.
> 
> #install.packages("stockPortfolio")
> library(stockPortfolio)
> 
> ticker  <- c("AAPL", "MSFT", "RHT")
> returns <- getReturns(ticker, "day", start="2008-01-01")
> 
> # S3 class: stockReturns
> returns
Time Scale: day 

Average Return
        AAPL         MSFT          RHT 
1.066532e-03 1.625954e-05 8.891856e-04 
> 
> # This object is really a list
> names(returns)
[1] "R"      "ticker" "period" "start"  "end"    "full"  
> 
> # Returns data
> returns$R[1:5,]
                   AAPL         MSFT          RHT
2012-03-26  0.018337388  0.018119338  0.024291498
2012-03-23 -0.005489372  0.000312500  0.006402794
2012-03-22 -0.005244813  0.002820432 -0.009417644
2012-03-21 -0.005709948 -0.002500781  0.026435194
2012-03-20  0.008085177 -0.006521739 -0.006662747
> 
> # Start and End Dates
> returns$start
[1] "2008-01-03"
> returns$end
[1] "2012-03-26"
> 
> # Raw data
> returns$full$AAPL[1:5,]
        Date   Open   High    Low  Close   Volume Adj.Close
1 2012-03-26 599.79 607.15 595.26 606.98 21259900    606.98
2 2012-03-23 600.49 601.80 594.40 596.05 15359900    596.05
3 2012-03-22 597.78 604.50 595.53 599.34 22281100    599.34
4 2012-03-21 602.74 609.65 601.41 602.50 22958200    602.50
5 2012-03-20 599.51 606.90 591.48 605.96 29115000    605.96
> 
> # Plot data
> png("stockPortfolio-getReturns.png")
> plot(returns)
> dev.off()
null device 
          1 
> 

Leave a Reply