Portfolio construction
Investors are interested in reducing risk and maximizing return of their investment and creating a portfolio does this job provided we have constructed it by keeping in mind the investor risk-return profile. I will guide you through creating an efficient frontier that can help you to measure risk with respect to your return expectation. For that, I will start extracting data for four securities. The first line of code creates a new environment to store data; the next few lines are for symbols list, data starting date, and extracting data using getSymbols()
:
>stockData<- new.env() > symbols <- c("MSFT","FB","GOOG","AAPL") >start_date<- as.Date("2014-01-01") >getSymbols(symbols, src="yahoo", env=stockData, from=start_date) > x <- list()
The next for loop stores individual stock data in a list, and calculates the day's gain and a data frame consisting of closing prices of all stocks in portfolio:
>for (i in 1:length(symbols)) { x[[i]]...