Escolar Documentos
Profissional Documentos
Cultura Documentos
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 :
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
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
Data Sources
Fixed Income
http://www.ivolatility.com/
http://www.optionmetrics.com/ (available from WRDS in library)
Bloomberg & Datastream
Futures
http://www.quandl.com
Bloomberg & Datastream
Yahoo Finance
www.mathworks.com/matlabcentral/fileexchange/.
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))
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.
% 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);
Write a for-loop that loops over the S&P closing prices and for each
year locates
For this reason (and others) empirical finance often focuses on returns.
pt
pt pt1
=
1
pt1
pt1
k1
Y
j=0
k1
Y
ptj
(1 + Rtj )
=
ptj1
j=0
k1
X
j=0
rtj
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
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).
, 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
% 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 !