ccylist={'USDEUR Curncy', 'USDJPY Curncy', 'USDCAD Curncy', 'USDCHF Curncy', 'USDGBP Curncy'};
datelist=<datenum list>.
clmat=<contain all close of the above currencies>.
plot(datelist, clmat); % row count of datelist and clmat are equal
legend(ccylist);
datetick('x','yyyy-mm-dd','keepticks');
%% logical vector "and"/"or"
x=[1 0 1];
y=~x;
x&y;
x|y;
%% Get strings begin with "USD" from a cell string list
ccylist(strncmpi(ccylist,'USD',3));
%% convert datenum to readable in matlab
[y,m,d]=datevec(data(:,1));
dlist=(y*100+m)*100+d;
newdata=[dlist, data(:,2)];
%% located by datenum in matrix
data(data(:,1)==datenum(2011,5,3),:);
%% function pointer
f1=@(x)x/2;
f1([2 4 6 8])
%% to store un-equal size array in a matrix, use cell array
range = {1:300,1:300,55};
%% string to number table usage
weight={
'USD',0.3
'EUR',0.2
'JPY',0.2};
item=weight(strncmpi(weight(:,1),'EUR',1)==1, :);
if(~isempty(item))
item{2}
end;
%% output matrix data with header names
data=[180 160];
header={'height', 'wieght'};
rpt(1,:)=header;
rpt(2,:)=num2cell(data);
rpt
%% mldivide(\), solve linear equations
A =
8 1 6
3 5 7
4 9 2
y = [1;2;3];
to solve this: x=A\b
x =
0.0500
0.3000
0.0500
to VERIFY:
A*x = y;
%% mldivide(\) require same row counts, but mrdivide require same column counts.
so to solve equations, use (\)
%%% manual OLS
function cointres=isCoint(X,Y)
% first perform an OLS and get residuals
beta = [ones(length(X),1),X]\Y;
res = Y - [ones(length(X),1),X]*beta;
% do a simple unit root test on the residuals
[H,PValue,TestStat] = dfARTest(res);
if H
cointres=TestStat;
else
cointres=0;
end
%%%% rolling sum
r=filter(ones(1,N),1,x);
%% idx1 store the logical index of entry, calc exit pnl with hold days=3
%calc exit logical index
N=3;
ex=logical([zeros(N,1);idx1(1:size(idx1,1)-N)]);
expx=data(ex);
enpx=data(idx1); % rows(enpx) might > expx
enpx=enpx(1:size(expx,1),:);
d2=[enpx,expx];
%% draw line on (2,4) and (3,5)
line([2 3],[4,5]); xlim([0 10]); ylim([0 10]);
%% output matrix data with header names, more easier way
data=[180 160];
[{'height', 'weight'}
num2cell(data)]
%% assign cell string in a column
strs(:,1)={'Msg'};
%% auto set colors in plot(): hold all
figure;
hold all;
for k=1:size1
plot(..);
end;
%% auto set colors in plot(): generate HSV
hc=hsv(size1);
figure;
hold on;
for i=1:size1
plot(...'color',hc(i,:));
end
%% parfor example
matlabpool close force local;
matlabpool open 7;
parfor k=1:N
...
end;
matlabpool close force local;
%% calc monthly sharpe
v1=datevec(dayeqty(:,1));
idx=(v1(:,3)==1);
idx(1)=1;
idx(end)=1;
mthly=dayeqty(idx,2);
mthret=mthly(2:end)./mthly(1:end-1)-1;
annvol_mth=std(mthret)*sqrt(12.0);
nyear=length(mthret)/12;
endret_mth = mthly(end)/mthly(1);
annrtn_mth = endret_mth ^ (1/nyear) - 1;
sh_mth=annrtn_mth/annvol_mth;
%% mesh
plot a 2 dimension matrix into 3D chart,
x,y is the row and column of matrix, z is the value