Abstract
In this workshop you will write a program that automates data collection, stock selection and portfolio optimization. We will use an Excel file as input with detailed parameters, so our program can run with any change in the Excel file.You will work in RStudio. Create an R Notebook document to write whatever is asked in this workshop.
You have to solve CHALLENGE exercises.
It is STRONGLY RECOMMENDED that you write your OWN NOTES as if this were your notebook. Your own workshop/notebook will be very helpful for your further study.
Keep saving your .Rmd file, and ONLY SUBMIT the .html version of your .Rmd file.
In this workshop you will write a program to do the following:
Bring data according to input specifications of an Excel file
Select stocks based on the CAPM alpha and/or beta,
Apply portfolio optimization, and
Evaluate the portfolio strategy calculating the holding period return (portfolio backtesting)
The input parameters are a list of tickers, frequency, start and end dates, market ticker, risk-free ticker, and backtest dates. These parameters are specified in the templateW9.xlsx Excel file.
Open the templateW9.xlsx in Excel. This file has the specifications of the inputs for your program.
The “parameters” Sheet has the specification of 3 different datasets. For each dataset specification, you have the following inputs:
Start and end data to collect historical prices of stocks and the market
The frequency of the data to be brought
The Excel sheet of the ticker list to be brought from the web, and also the source.
The market ticker to be used to run the CAPM models for the stocks. Also, the risk-free rate ticker (from the FED) to be used to generate the premium returns for the CAPM.
The minimum and maximum weight to allow for each stock for the portfolio optimization
The number of stocks to be selected for the portfolio
The initial and end date to do the backtest of the portfolio. The backtest of the portfolio is the holding return of the optimized portfolio in the periods specified.
Define a variable called dataset and assign 1. You have to bring the corresponding data for dataset 1 secified in the input. You have to bring the stock continuously compounded returns, the market index returns, and the risk-free returns. Do any data processing you need to generate this data. Hint: start testing your program with the dataset 1, which has a short list of tickers (like 10 tickers). Make sure your program works if you change the dataset to 2 or 3.
You have to do the same you did in previous workshop in terms of selecting stocks according to specific beta coefficients, and the estimate an optimal portfolio. However, you have to use the input parameters you read to process the right information. Then, make sure that you have the correct program for the previous workshop. Here are more details for this part:
Keep only the stocks with significant and positive alpha (b0) and sort the remaining stocks from the highest to the lowest alpha. Select the top N stocks. The number of stocks to be selected is indicated in the “parameter” sheet.
With these stocks estimate the optimal portfolio (use a risk-free rate=0).
(Optional) Save the portfolio HPR in the “parameter” sheet (yellow cell)
(Optional) Save the market return in the “parameter” sheet (yellow cell)
Did your portfolio strategy performed better than the market index? Report your results in program as comments.