%% TSM with pooled regression (Figure 4) %%

clear;clc
addpath 'E:\RESEARCH\TSMOM\Codes_new';
addpath 'E:\Dropbox\My toolbox';
load Return.mat;

%% All asset classes %%
Ns = 1; Ne = 55;
Return = Return(Return(:,6)>=Ns & Return(:,6)<=Ne,:);
Return = Return(Return(:,1)>=198501,:);

% Generate xr/vol %
z = [nan; Return(2:end,2)./Return(1:end-1,3)];
z = [Return, z];
for s = Ns:Ne
    line1 = find(z(:,6)==s);
    z(line1(1):line1(12),:) = []; 
end
z = z(:, [1, 5, 6, 7]);                                         % [ym, NBER, ID, xr/vol]

res = []; res_sign = [];
for h = 1:60
    yy = [];
    for n = Ns:Ne 
        tt = find(z(:,3) == n);
        a = z(tt,:); 
        yy = [yy; a(h+1:end,4), a(1:end-h,4), a(h+1:end,[1, 3])];   % yy = [y, x,  ym, ID]; 
    end
  b = clusterreg(yy(:,1), [yy(:,2), ones(size(yy(:,2)))], yy(:,3));
  res = [res; b(1, [1, 3]), b(2, [1, 3])];
  
  b = clusterreg(yy(:,1), [sign(yy(:,2)), ones(size(yy(:,2)))], yy(:,3));
  res_sign = [res_sign; b(1, [1, 3]), b(2, [1, 3])];
  
end
save('E:\RESEARCH\TSMOM\Codes_new\Fig4_TstatAll.mat','res','res_sign')

clear;clc
addpath 'E:\RESEARCH\TSMOM\Codes_new';
addpath 'E:\Dropbox\My toolbox';
load Return.mat;
%% Commodity %%
Ns = 1; Ne = 24;
Return = Return(Return(:,6)>=Ns & Return(:,6)<=Ne,:);
Return = Return(Return(:,1)>=198501,:);

% Generate xr/vol %
z = [nan; Return(2:end,2)./Return(1:end-1,3)];
z = [Return, z];
for s = Ns:Ne
    line1 = find(z(:,6)==s);
    z(line1(1):line1(12),:) = []; 
end
z = z(:, [1, 5, 6, 7]);                                         % [ym, NBER, ID, xr/vol]

res_com = []; ressign_com = [];
for h = 1:60
    yy = [];
    for n = Ns:Ne 
        tt = find(z(:,3) == n);
        a = z(tt,:); 
        yy = [yy; a(h+1:end,4), a(1:end-h,4), a(h+1:end,[1, 3])];   % yy = [y, x,  ym, ID]; 
    end
  b = clusterreg(yy(:,1), [yy(:,2), ones(size(yy(:,2)))], yy(:,3));
  res_com = [res_com; b(1, [1, 3]), b(2, [1, 3])];
  
  b = clusterreg(yy(:,1), [sign(yy(:,2)), ones(size(yy(:,2)))], yy(:,3));
  ressign_com = [ressign_com; b(1, [1, 3]), b(2, [1, 3])];
  
end
save('E:\RESEARCH\TSMOM\Codes_new\Fig4_TstatCom.mat','res_com','ressign_com')

clear;clc
addpath 'E:\RESEARCH\TSMOM\Codes_new';
addpath 'E:\Dropbox\My toolbox';
load Return.mat;
%% Equity %%
Ns = 25; Ne = 33;
Return = Return(Return(:,6)>=Ns & Return(:,6)<=Ne,:);
Return = Return(Return(:,1)>=198501,:);

% generate xr/vol %
z = [nan; Return(2:end,2)./Return(1:end-1,3)];
z = [Return, z];
for s = Ns:Ne
    line1 = find(z(:,6)==s);
    z(line1(1):line1(12),:) = []; 
end
z = z(:, [1, 5, 6, 7]);                                         % [ym, NBER, ID, xr/vol]

res_eqt = []; ressign_eqt = [];
for h = 1:60
    yy = [];
    for n = Ns:Ne 
        tt = find(z(:,3) == n);
        a = z(tt,:); 
        yy = [yy; a(h+1:end,4), a(1:end-h,4), a(h+1:end,[1, 3])];   % yy = [y, x,  ym, ID]; 
    end
  b = clusterreg(yy(:,1), [yy(:,2), ones(size(yy(:,2)))], yy(:,3));
  res_eqt = [res_eqt; b(1, [1, 3]), b(2, [1, 3])];
  
  b = clusterreg(yy(:,1), [sign(yy(:,2)), ones(size(yy(:,2)))], yy(:,3));
  ressign_eqt = [ressign_eqt; b(1, [1, 3]), b(2, [1, 3])];
  
end
save('E:\RESEARCH\TSMOM\Codes_new\Fig4_TstatEqt.mat','res_eqt','ressign_eqt')

clear;clc
addpath 'E:\RESEARCH\TSMOM\Codes_new';
addpath 'E:\Dropbox\My toolbox';
load Return.mat;
%% Bond %%
Ns = 34; Ne = 46;
Return = Return(Return(:,6)>=Ns & Return(:,6)<=Ne,:);
Return = Return(Return(:,1)>=198501,:);

% Generate xr/vol %
z = [nan; Return(2:end,2)./Return(1:end-1,3)];
z = [Return, z];
for s = Ns:Ne
    line1 = find(z(:,6)==s);
    z(line1(1):line1(12),:) = []; 
end
z = z(:, [1, 5, 6, 7]);                                         % [ym, NBER, ID, xr/vol]

res_bnd = []; ressign_bnd = [];
for h = 1:60
    yy = [];
    for n = Ns:Ne 
        tt = find(z(:,3) == n);
        a = z(tt,:); 
        yy = [yy; a(h+1:end,4), a(1:end-h,4), a(h+1:end,[1, 3])];   % yy = [y, x,  ym, ID]; 
    end
  b = clusterreg(yy(:,1), [yy(:,2), ones(size(yy(:,2)))], yy(:,3));
  res_bnd = [res_bnd; b(1, [1, 3]), b(2, [1, 3])];
  
  b = clusterreg(yy(:,1), [sign(yy(:,2)), ones(size(yy(:,2)))], yy(:,3));
  ressign_bnd = [ressign_bnd; b(1, [1, 3]), b(2, [1, 3])];
  
end
save('E:\RESEARCH\TSMOM\Codes_new\Fig4_TstatBnd.mat','res_bnd','ressign_bnd')

clear;clc
addpath 'E:\RESEARCH\TSMOM\Codes_new';
addpath 'E:\Dropbox\My toolbox';
load Return.mat;
%% Currency %%
Ns = 47; Ne = 55;
Return = Return(Return(:,6)>=Ns & Return(:,6)<=Ne,:);
Return = Return(Return(:,1)>=198501,:);

% Generate xr/vol %
z = [nan; Return(2:end,2)./Return(1:end-1,3)];
z = [Return, z];
for s = Ns:Ne
    line1 = find(z(:,6)==s);
    z(line1(1):line1(12),:) = []; 
end
z = z(:, [1, 5, 6, 7]);                                         % [ym, NBER, ID, xr/vol]

res_cur = []; ressign_cur = [];
for h = 1:60
    yy = [];
    for n = Ns:Ne 
        tt = find(z(:,3) == n);
        a = z(tt,:); 
        yy = [yy; a(h+1:end,4), a(1:end-h,4), a(h+1:end,[1, 3])];   % yy = [y, x,  ym, ID]; 
    end
  b = clusterreg(yy(:,1), [yy(:,2), ones(size(yy(:,2)))], yy(:,3));
  res_cur = [res_cur; b(1, [1, 3]), b(2, [1, 3])];
  
  b = clusterreg(yy(:,1), [sign(yy(:,2)), ones(size(yy(:,2)))], yy(:,3));
  ressign_cur = [ressign_cur; b(1, [1, 3]), b(2, [1, 3])];
  
end
save('E:\RESEARCH\TSMOM\Codes_new\Fig4_TstatCur.mat','res_cur','ressign_cur')