Você está na página 1de 24

Application of Matlab for Finance

Imperial College London

Class 4: Financial Time Series Data

Paul Whelan
paul.whelan07@imperial.ac.uk

Today you will put your hands on real world financial data.
By the end of class you will learn how to :

Load stock price data from Yahoo Finance

work with MATLAB serial dates (datenum())

Save data to an excel file (xlswrite()+ m2xdate())

Load data from excel file (xlsread()+ x2mdate())

Visual financial data (plot()+ datetick())

compute simple returns and log returns

fit empirical distributions and test for normality (histfit())

print results to the screen (fprintf())

Test for market efficiency (weekday effects) using logical indexing,


for() loops, and (switch case)

Data Sources

Economic Data

World

United Kingdom

http://stats.oecd.org/index.aspx
http://www.imf.org/external/data.htm
Bloomberg & Datastream

http://www.statistics.gov.uk/hub/index.html
Bloomberg & Datastream

United States

Federal Reserve Economic Data (http://research.stlouisfed.org/fred2/)


http://www.census.gov/
http://www.bls.gov/
http://www.ssa.gov/
http://www.treasury.gov/
http://www.sec.gov/
http://www.economagic.com/
http://www.forecasts.org/
Bloomberg & Datastream

Data Sources

Foreign Exchange

http://www.oanda.com/currency/historical-rates/
http://www.dukascopy.com/swiss/english/marketwatch/historical/
http://www.dailyfx.com/forex forum/free-strategy-trader-historicaldata/220967-free-strategy-trader-historical-data.html
http://ratedata.gaincapital.com
Bloomberg & Datastream

Equity

Kenneth French Data Library


(http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/)
Robert Shiller (http://www.econ.yale.edu/shiller/data.htm)
http://finance.yahoo.com/
http://www.iasg.com/managed-futures/market-quotes
http://unicorn.us.com/advdec/
http://www.quandl.com
Bloomberg & Datastream

Data Sources

Fixed Income

Federal Reserve Economic Data (http://research.stlouisfed.org/fred2/)


http://www.bbalibor.com/rates/
http://federalreserve.gov/releases/h15/data.htm
http://www.quandl.com
Bloomberg & Datastream

Options and Implied Volatility

http://www.ivolatility.com/
http://www.optionmetrics.com/ (available from WRDS in library)
Bloomberg & Datastream

Futures

http://www.quandl.com
Bloomberg & Datastream

Yahoo Finance

In order to download data from Yahoo!finance, we make use of the


function hist stock data.m

This function can be found at the MATLAB File Exchange

www.mathworks.com/matlabcentral/fileexchange/.

I have provided it for you on the HUB.

Download this file into your current working directory.

Type help hist stock data and read the instructions

Download S&P 500 data


% specify ticker symbol as string variable
tickSym = 'GSPC'; % specify stock data of interest
% specify beginning and ending as string variables
dateBeg = '01011990'; % day, month, year: ddmmyyyy
dateEnd = '01122013';
% load data
SP = hist stock data(dateBeg,dateEnd,tickSym);

Note: GSPC is the yahoo ticker for the S&P 500.

A complete list of ticker symbols can be found here:


http://www.myinvestorshub.com/yahoo stock list.php

The function hist stock data returns a structure variable.

Taking a more detailed look into the data structure using


SP

Matlab has stored various pieces of information in a cell structure.

To access the cell structure we use dot syntax then parenthesis


indexing as usual.

For example to assess the first 4 dates


exampleDateEntries = SP.Date(1:4)

or the intra-day high for the first entry


exampleHIGH = SP.Open(1)

We need to
1. convert the date format from a string to a serial number
2. extract closing prices

% preallocation
caldt = zeros(length(SP.Date),1);
pt = zeros(length(SP.Date),1);
volume = zeros(length(SP.Date),1);
% transform and extract using a forloop
for i=1:length(SP.Date)
caldt(i) = datenum(SP.Date{i},'yyyymmdd');
pt(i) = SP.Close(i);
volume(i) = SP.Volume(i);
end

Flip dates, prices, and volumes upside down (as this is the convention)
% serial dates wrong way round
caldt(1:4)
datestr(caldt(1:4)) % displays to the screen
% flip most recent entries to the end
caldt = flipud(caldt);
pt = flipud(pt);
volume = flipud(volume);
% serial dates the right way round
caldt(1:4)
datestr(caldt(1:4))

Reading and Writing

% (1) Writing Example


% (1) Writing Example
excel dates = m2xdate(caldt,0);
%
%
%
%
%

Note:
Microsoft Excel supports two different date systems: the 1900
date system and the 1904 date system.
By default Excel on Windos uses the 1900 date system,
on MAC OS X the default is the 1904 date system.

excel data = [excel dates pt volume];


col header = {'excel date' , 'price' , 'volume'};
xlswrite('yahoo output.xls',excel data,'Sheet1','A2');
xlswrite('yahoo output.xls',col header,'Sheet1','A1');

% Row cell array (for column labels)


% Write data
% Write column header

% Note: it is possible to write to excel in one command using cell arrays.

Reading and Writing

% CLEAR EVERYTHING AND RELOAD THE DATA FROM THE EXCEL FILE :
clear all;
% (2) Reading Example
[data , text] = xlsread('yahoo output.xls','Sheet1');
caldt = x2mdate(data(:,1),0);
pt = data(:,2);
volume = data(:,3);

Plotting Financial Data

time span = 1990 : 0.5 : 2013;


x ticks = datenum(time span, 1, ones(1, length(time span)));
figure;
subplot(2,1,1)
plot(caldt,pt, 'k' , 'LineWidth' , 1.0);
set(gca,'XTick' , x ticks);
datetick('x',11,'keeplimits');
xlim([min(caldt) max(caldt)])
ylabel('Index Level' );
xlabel('date');
subplot(2,1,2)
plot(caldt,volume, 'g' , 'LineWidth' , 1.0);
set(gca,'XTick' , x ticks);
datetick('x',11,'keeplimits');
xlim([min(caldt) max(caldt)])
ylabel('Volume' );
xlabel('date');

Write a for-loop that loops over the S&P closing prices and for each
year locates

the date of the intra-year high

the date of the intra-year low

the yearly standard deviation of daily price changes (roughly volatility)

re-plot the S&P level with intra-year high and lows

plot bar chart of intra-year volatility

See Examples.m file for code

From Prices to Returns

Investors are not concerned with the level of prices per-se

But care about the returns to capital invested

For this reason (and others) empirical finance often focuses on returns.

From Prices to Returns

Rt is the 1-period return:


Rt =

pt
pt pt1
=
1
pt1
pt1

Rt+k is the k-period return:


1 + Rt+k =

k1
Y
j=0

k1
Y
ptj
(1 + Rtj )
=
ptj1
j=0

rt is the log return: rt = ln(pt /pt1 ) = ln(1 + Rt )

Similar in magnitude to Rt if Rt close to zero

Log returns convenient because of additivity


rt+k =

k1
X
j=0

rtj

Log Returns Versus Simple Returns

% transform prices to simple returns (note: ./ syntax !!! )


% daily returns
Rt = 100*(pt(2:end)pt(1:end1))./pt(1:end1);
lnPt = log(pt);
lnRt = 100*(lnPt(2:end) lnPt(1:end1));
% date vector for daily returns
caldt rets = caldt(2:end);

Monthly and Annual Returns

% First use the fints function from the financial time


% series toolbox in order to convert daily price series
% to monthly price series.
fts = fints(caldt, lnPt);
nfts = tomonthly(fts);
caldt fts = nfts.dates;
lnPt monthly = fts2mat(nfts);
% monthly returns
lnRt monthly = 100*(lnPt monthly(2:end) lnPt monthly(1:end1));
caldt monthly = caldt fts(2:end);
% annual returns
lnRt12 = 100*(lnPt monthly(13:end) lnPt monthly(1:end12));
caldt rets12 = caldt fts(13:end);

Plotting Returns

figure;
subplot(2,1,1)
plot(caldt rets , Rt, 'k' , 'LineWidth' , 1.0);
set(gca,'XTick' , x ticks);
datetick('x',11,'keeplimits');
xlim([min(caldt) max(caldt)])
ylabel('Simple Monthly Returns' );
xlabel('date');
subplot(2,1,2)
plot(caldt rets , lnRt, 'g' , 'LineWidth' , 1.0);
set(gca,'XTick' , x ticks);
datetick('x',11,'keeplimits');
xlim([min(caldt) max(caldt)])
ylabel('Ln Monthly Returns' );
xlabel('date');
% plot annual log returns
figure;
plot(caldt rets12 , lnRt12, 'g' , 'LineWidth' , 2.0);
set(gca,'XTick' , x ticks);
datetick('x',11,'keeplimits');
xlim([min(caldt) max(caldt)])
ylabel('Ln Annual Returns' );
xlabel('date');

Empirical Distributions

% unconditional empirical distribution of returns


figure;
subplot(2,1,1)
histfit(Rt,50)
xlim([5 +5])
subplot(2,1,2)
histfit(lnRt,50)
xlim([5 +5])
% > Difficult to see deviations from normality in monthly returns.
% How about annual returns ?
figure;
histfit(lnRt monthly,35)
xlim([25 +25])
% Now we see FAT left tails.

Weekday Effects

Early tests of the efficient market hypothesis tested whether returns are high or low
on particular days of the week.

If returns were higher on mondays than fridays one could trade on such information
to beat the market (which trades on all days).

% This can be done with logical indexing (quick) or a forloop (slow)


monday rets = Rt(weekday(caldt rets) == 2);
tuesday rets = Rt(weekday(caldt rets) == 3);
wednesday rets = Rt(weekday(caldt rets) == 4);
thursday rets = Rt(weekday(caldt rets) == 5);
friday rets = Rt(weekday(caldt rets) == 6);
output = [1 , mean(monday rets)
2 , mean(tuesday rets)
,
3 , mean(wednesday rets) ,
4 , mean(thursday rets)
,
5 , mean(friday rets)
,

, std(monday rets)
std(tuesday rets)
;
std(wednesday rets) ;
std(thursday rets)
;
std(friday rets) ]
;

fprintf('\n \n');
fprintf('\n Day of the week return statistics:
\n');
fprintf('\n \n');
fprintf('\n day
mean(Rt)
std(Rt)
\n');
for j=1:size(output,1)
fprintf(' %d %8.2f
%8.2f\n' , output(j,1) , output(j,2) , output(j,3) );
end
fprintf('\n \n');

Weekday Effects

% Returns are highest on a tuesday ! But is this statistically significant?


T tuesdays = length(tuesday rets);
se tuesday = std(tuesday rets)2/T tuesdays;
T fridays = length(friday rets);
se friday = std(friday rets)2/T fridays;
% t test for difference in means
t = (mean(tuesday rets) mean(friday rets))/sqrt(se tuesday + se friday)
DF = (T tuesdays 1) + (T fridays 1);
ci = 0.95;
alpha = (1 ci)/2;
t90 = tinv(1alpha/2, DF)
%
%
%
%

% 1sided test

We cannot reject the null hypothesis that returns on tuesdays are statistically
higher on tuesdays than on fridays. But statistics can be misleading.
How about a trading strategy that only invests on tuesdays (going long)
and fridays (going short)

Weekday Effects

trade = 0;
T = length(caldt rets);
return container = NaN(T,2);
for ii = 1:T
this day = weekday(caldt rets(ii));
switch this day
case 2
trade = 0;
case 3
trade = 1;
case 4
trade = 0;
case 5
trade = 0;
case 6
trade = 1;
otherwise
% do nothing
end
return container(ii,:) = [Rt(ii,1) , Rt(ii,1)*trade];
end
return container = (1+return container/100);
cumreturns = cumprod(return container);

Weekday Effects

figure;
plot(caldt rets , cumreturns, 'LineWidth' , 2.0);
set(gca,'XTick' , x ticks);
datetick('x',11,'keeplimits');
xlim([min(caldt) max(caldt)])
ylabel('Dollar Growth' );
xlabel('date');
legend('S&P','tuesdayfriday trade','Location','Northwest');
% Markets seem pretty efficient then !

Você também pode gostar