# R program to read prices of stocks from Yahoo Finance and estimate the vector # of expected returns and the covariance matrix # Use the daily closing prices. # The list of stocks is "tickerList". You have to update this by hand. baseURL = "http://ichart.finance.yahoo.com/table.csv?s=" # The base URL is yahoo finance tickerList = c("T", # Tickers of the stocks you want to analyse "BA", "CSCO" ) n = length( tickerList) cat( n, " tickers\n") #----------------------------------------------------------------------------------- # Read price time series from Yahoo! #----------------------------------------------------------------------------------- PriceTimeSeries = list() # An R "list" of time series, one series for each ticker # Create an empty list, add the series info as they come. SeriesLengths = c() # A vector to hold the lengths of the series for ( ticker in tickerList){ # Make the string to send to Yahoo! that asks for the prices for the given ticker. # Do this by adding the ticker symbol to complete the generic price request. # The second line, with "sep" and "collapse" keeps R from putting a space between the two strings fullURL = paste( baseURL , ticker, sep = "", collapse = NULL) # Send the request to Yahoo! and put the reply into the file PRices.csv download.file( fullURL, "Prices.csv", "auto", quiet = TRUE, mode = "w") # The R function "read.csv" reads a csv file (simple spreadsheet format) and # puts the result into an R "data frame". This is called "Prices" Prices = read.csv("Prices.csv") # The fifth column of the Prices data frame, which is the fifth column of # the csv file from Yahoo! has the daily closing prices for that ticker ClosingPrices = Prices[[5]] L = length( ClosingPrices) cat("ticker ", ticker, " has L = ", L, "\n") cat(" most recent prices are ", ClosingPrices[1:5], "\n") # The R command "append( list, item)" returns the list with the item appended to the end # The "list( ClosingPrices)" says to append the list as a list. Don't add the numbers # to the end of the previous list. PriceTimeSeries = append( PriceTimeSeries, list( ClosingPrices)) SeriesLengths = append( SeriesLengths, L) } #----------------------------------------------------------------------------------- # Compute the returns, expected returns, and covariance matrix #----------------------------------------------------------------------------------- ReturnSeries = list() # An R list that will contain the return sequences mu = numeric(n) # An R vector that will have the means of the return sequences for (i in 1:n){ L = SeriesLengths[i] # the length of this series R = numeric( L - 1) # allocate the vector for the returns of this ticker S = PriceTimeSeries[[i]] # the time series of daily close prices for this stock for ( t in 1:(L-1)) { R[t] = (S[t] - S[t+1])/S[t+1] # S[t] is the price t days ago. # This is the return from the previous day, which is S[t+1] } ReturnSeries = append( ReturnSeries, list(R)) mu[i] = mean(R) } for (i in 1:n){ cat("Ticker ", tickerList[i], " has mean daily return ", mu[i], "\n") } C = matrix( numeric(n^2), nrow=n, ncol=n) R1 = ReturnSeries[[1]] # Use double square brackets to retrieve a time series from the list R2 = ReturnSeries[[2]] C[1,1] = var(R1) # var() computes the variance of a list of numbers C[2,2] = var(R2) L1 = length(R1) L2 = length(R2) L = min( L1, L2) # Find the length of the shorter time series cov( R1[1:L], R2[1:L]) # cov() calculates the covariance of two lists of the same length C[1,2] = cov( R1[1:L], R2[1:L]) C[2,1] = C[1,2] # cov( R2[1:L], R1[1:L]) does unnecessary extra calculation cat("C_11 = var(", tickerList[1], ") is ", C[1,1], "\n") cat("C_12 = cov(", tickerList[1], ",", tickerList[2], ") is ", C[1,2], "\n") rho_12 = C[1,2]/sqrt(C[1,1]*C[2,2]) cat("The correlation coefficient is ",rho_12, "\n") #----------------------------------------------------------------------------------- # Compute some efficient portfolios #-----------------------------------------------------------------------------------