## File Exchange Pick of the WeekOur best user submissions

### Background

This submission contains a suite of tools for getting daily stock prices from Yahoo! and Google. There are utility functions for converting between Yahoo! and Google's formatting and to calculate adjusted closing prices, (which Yahoo! does automatically but which Google does not).

As a side note, Michael Weidman was one of the first few people I met when I started working at MathWorks. He taught me everything I know about the Kolmogorov-Smirnov test ( kstest() ) and was a frequent resource for all of us when it came to math questions (Thanks!). He left MathWorks a little over a year ago to pursue new ventures across the pond.

### Acquiring Financial Data

For this simple example, we will use Michael's tools to pull financial data from Yahoo! and calculate an efficient frontier.

% Selected symbols
symbols = {'TSLA','MSFT','GOOG','MAR','SAM','PG','YHOO'};

% Retrieve data using Michael's function
data = getYahooDailyData(symbols,...
'01/01/2011', '10/31/2013', 'dd/mm/yyyy'); %start, end, format

Data is a struct array of tables!

disp(data)
    TSLA: [730x7 table]
MSFT: [730x7 table]
GOOG: [730x7 table]
MAR: [730x7 table]
SAM: [730x7 table]
PG: [730x7 table]
YHOO: [730x7 table]


The table is a new MATLAB data container in R2013b. Tables make it easy to store heterogenous column data in one container. Michael has included a check to see if the instance of MATLAB is new enough to have tables, if it isn't, the code uses datasets, the table's predecessor from the Statistics Toolbox.

Let's take a look at a table by displaying the first 10 rows of Tesla's data:

disp(data.TSLA(1:10,:));
       Date       Open     High      Low     Close      Volume      AdjClose
__________    _____    _____    _____    _____    __________    ________
7.3451e+05    26.84       27     25.9    26.62     1.283e+06    26.62
7.3451e+05    26.66    26.95    26.02    26.67    1.1874e+06    26.67
7.3451e+05    26.48     26.9    26.19    26.83    1.4467e+06    26.83
7.3451e+05    26.83       28    26.81    27.88    2.0612e+06    27.88
7.3451e+05       28    28.58     27.9    28.24    2.2479e+06    28.24
7.3451e+05    28.17    28.68    28.05    28.45    1.3427e+06    28.45
7.3451e+05    28.59    28.71    26.92    26.96    1.7102e+06    26.96
7.3452e+05    27.01     27.4    26.52    26.96     9.644e+05    26.96
7.3452e+05    26.96    26.97    26.16    26.22     7.236e+05    26.22
7.3452e+05    26.15    26.58    25.61    25.75     1.192e+06    25.75


We can see that the table is displaying the separate variables and their names. The best part is that we can still index it like a matrix using {}.

h = plot(data.TSLA{:,1},...   Date
data.TSLA{:,2:5},... 2nd:5th variables are valuations
'DisplayName',...    Variable names for the legend
data.TSLA.Properties.VariableNames(2:5));

axis tight; % tight axes
datetick('x','mmmyy','keepticks','keeplimits'); %date tick
ylabel('Stock Price (US\$)');

Now that we have the data we can use it to figure out how to get rich quickly by calculating the efficient frontier.

% Extract the closing valuations into an array:
assetsStruct = structfun(@(x)x.Close,data.','UniformOutput',false);
% Uses x.Close to extract the "Close" variable from each table.
assets = struct2array(assetsStruct); % Convert to array

% Convert to returns
assetReturns = tick2ret(assets);

% Create a portfolio object with the asset names. (Financial Toolbox req'd)
p = Portfolio('AssetList',symbols);

% Estimate the asset moments: Mean and Covariance, for the asset returns.
p = p.estimateAssetMoments(assetReturns);

% Set default constraints such that the weights sum up to one and to allow
% long only position (no shorting or shortselling: weights can't be negative)
p = p.setDefaultConstraints;

% Calculate frontier, risk and return
frontierWeights = p.estimateFrontier(20);
frontierRisk    = p.estimatePortRisk(frontierWeights);
frontierReturns = p.estimatePortReturn(frontierWeights);

% Visualize efficient frontier
figure;
plot(frontierRisk,frontierReturns, '*-r','LineWidth',2)
legend('Efficient Frontier','Location','SouthEast')
title('Efficient Frontier');
xlabel('Risk');
ylabel('Return');

### The Rest of It

In addition to giving us easy to use tools to get data from Yahoo! and Google, Michael has also managed to use quite a few of the other features in the MATLAB language. These include: regular expressions, tables, the new functiontests capabilities for running function-based unit tests in the MATLAB unit testing framework, version checking, and safe-guarding against exceptions or errors. For example, when I first ran the above this morning before my laptop had found an internet connection:

### Datafeed Toolbox

For additional options or access to many other popular Financial Data Servers such as Bloomberg® or Thomson Reuters®, MathWorks has the Datafeed Toolbox. This toolbox provides direct access to the servers through their APIs and allows for realtime as well as intraday tick information if requested.