Portfolio Theory Dr. Andrea Rigamonti andrea.rigamonti@econ.muni.cz Seminar 2 Content: • Get the Alpha Vantage API • Download data • Assemble the dataset Get the Alpha Vantage API Go to https://www.alphavantage.co/ and click on “GET FREE API KEY” Get the Alpha Vantage API Next, fill the required fields and click again on “GET FREE API KEY”. The API will appear on the same page. Download data • Open the script “Code_2.R”. • Load the “dplyr” and “alphavantager” packages using the “library” function. If they are not available, install them from the menu or with the “install.packages” function. • Download data for American Express (Ticker symbol “AXP”) • We are interested in the closing prices. • Notice the extreme price drops in the unadjusted closing prices after some dividend payments. • The adjusted prices account for the dividends and correct for this effect. Download data • Store the “timestamp” column in a separate object and name it “date”. • Store the “adjusted_close” column in a separate object and name it “AXP”. • Remove “AXP_all”. • For the other stocks we directly keep only the column with the adjusted prices (column 6). Download data Now we download the risk-free rate. Go to https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ data_library.html and download the Fama-French 3 factors in CSV format. Extract the .csv file from the .zip archive and open it with the notepad. Download data • Delete the first 3 lines and name the first column as “date” Download data • Data from Alpha Vantage start in December 1999, but we will lose one period when computing the return, so delete the lines before “200001”. • Also delete the annual factors. Make sure to leave one empty line at the end. • Save the .csv file with the name “FF3.CSV” in the same folder in which “Code_2.R” is located. This allows R to open it without specifying its path. Download data • Load the “readr” package (install it first if not available). • Load data in R with the “read_csv” function. • Only keep the fifth column (which contains the risk-free rate) and call it “Rfree”. • We divide this rate by 100 because it is expressed in percentage. Assemble the dataset • First we compute the (adjusted) returns from the prices. • We lose the first observation when differencing to compute returns. Therefore, we also remove the first observation from the “date” object. • Data from Alpha Vantage span up to today, but the riskfree rate only spans up to December 2023. • The last price observations also do not coincide with the last trading day of the month. • Therefore, both in “date” and “DJIA_ret”, we only keep observations up to when “Rfree” spans to. Assemble the dataset • Finally we bind the date, the risk-free rate and the returns and call this new object “DJIA”. This is the dataset with the DJIA returns. • Usually it is useful to have a dataset of excess returns, i.e., the returns minus the risk-free rate. • To subtract the risk-free rate from the returns we use the “matrix” function to turn the vector of risk-free rate into a matrix. This gives us a matrix with 1 column. • We call the object with excess returns “DJIA_exret”. • We bind the date, the risk-free rate and the excess returns and call this new object “DJIA_ex”. Assemble the dataset We now have a dataset with returns and one with excess returns, plus the risk-free rates. We delete everything else and we save the environment as an “.RData” file. We will be able to load this file in the future and have our dataset ready to be used.