%%%% Author: Daniel Nathan
%%%% Date: 19/09/2025
%%% Data is updated until the 31/12/2019. We exclude the Covid-19 yearly
%%% crisis.

clear all;
clc;

%global ini

%% initialize basic directories

cd ..\

ini.dirs.base=pwd;
ini.dirs.code=[ini.dirs.base '\_code\'];
addpath( genpath(ini.dirs.code)); %% add all subfolders. They will be used later
ini.dirs.raw=[ini.dirs.base '\_data\'];
ini.dirs.results.raw=[ini.dirs.base '\_results'];

cd(ini.dirs.code);


%%% Extract currency options data: Data is from 2008 up until 31/12/2020


%% RR -- Data is BGN -> NY 17:00
[num,~,raw]=xlsread([ini.dirs.raw 'option liquidity\options data\optionsdata - 03-01'],'RR');
dates=datenum(raw(8:end,1),'dd/mm/yyyy'); %% dates are aligned with option dates
usdilsrr25=num(2:end,1:6); %% we start from 2 because the first one has no value for the other dates.
usdilsrr10=num(2:end,7:12); %% we start from 2 because the first one has no value for the other dates.
optionsdates=dates;save('optionsdates','optionsdates');
save('usdilsrr10','usdilsrr10');save('usdilsrr25','usdilsrr25');
%% BF
[num,~,raw]=xlsread([ini.dirs.raw 'option liquidity\options data\optionsdata - 03-01'],'BF');
usdilsbf25=num(2:end,1:6); %% we start from 2 because the first one has no value for the other dates.
usdilsbf10=num(2:end,7:12); %% we start from 2 because the first one has no value for the other dates.
%% 9M BF doesn't exist for some reason. We interpolate it. 
save('usdilsbf10','usdilsbf10');save('usdilsbf25','usdilsbf25');

%% ATMV
[num,~,raw]=xlsread([ini.dirs.raw 'option liquidity\options data\optionsdata - 03-01'],'ATMV');
usdilsatmv(:,1:6)=num(2:end,1:6); %% we start from 2 because the first one has no value for the other dates.
save('usdilsatmv','usdilsatmv');

ccydates=dates; save('ccydates','ccydates');
T=size(ccydates,1);

%% load Israel basis
construct_basis; %% saved under newbasis

%% build NEER with NY time

ccysdata=readtable([ini.dirs.raw '/ccys/CMPN.xlsx'],'Sheet','clean'); %% 17:00 NY
ccys=table2array(ccysdata(:,2:end));
%% USD EUR CHINA BRITAN TURKEY JPY, RUSSIA MISSING, INDIA, SOUTH KOREA CHF, TAIWAN, CANADA, BRAZIL, HONG KONG, CZECH, POLAND, UKRAINE, MEXICO, SINGAPORE, VIETNAM,THAILAND, AUSTRALIA, SWEDEN, HUNGARY
weights=[0.287,0.244, 0.115, 0.064, 0.037, 0.031, 0.02, 0.019, 0.016, 0.013,0.013,0.013,0.013,0.011,0.011,0.011,0.009,0.008,0.008,0.007,0.007,0.007,0.007];
numpairs=size(weights,2);
ccysret=[repmat(NaN,1,numpairs);100*log(ccys(2:end,:)./ccys(1:end-1,:))];
ilsnyccy=ccys(1:end,1);
ilseur=ccys(1:end,2);
eurusd=ilseur./ilsnyccy;
tempNEERny=[];
for i=1:size(ccysret,1)
tempNEERny(i)=nansum(ccysret(i,1:23).*(weights(:,1:23)./(sum(weights(:,1:23)))) );
end
%tempNEERny(tempNEERny==0)=NaN;
save('eurusd','eurusd');
[~,idx]=ismember(datenum(ccysdata.date),ccydates);


NEERny=tempNEERny(idx(idx>0))';
save('NEERny','NEERny');

tempusdilsny=[];
ccysret=[repmat(NaN,1,numpairs);100*log(ccys(2:end,:)./ccys(1:end-1,:))];
for i=1:size(ccysret,1)
tempusdilsny(i)=nansum(ccysret(i,1:1).*(weights(:,1:1)./(sum(weights(:,1:1)))) );
end
%tempNEERny(tempNEERny==0)=NaN;

[~,idx]=ismember(datenum(ccysdata.date),ccydates);


dilsnyccy=tempusdilsny(idx(idx>0))';
ilsnyccy=ilsnyccy(idx(idx>0));
eurusd=eurusd(idx(idx>0));
dilsnyccy=tempusdilsny(idx(idx>0))';
ilsnyccy=ilsnyccy(idx(idx>0))';


save('ilsnyccy','ilsnyccy');
save('dilsnyccy','dilsnyccy');
save('eurusd','eurusd');


%% flow data: up until 31/12/2020
[num,~,raw]=xlsread([ini.dirs.raw '/flows/flowsraw.xlsx']);
dates=datenum(raw(3:end,1),'mm/dd/yyyy');
flowsdates=dates;

%% first we kill saturday

foreign_total=num(3:end,13);
inst=num(3:end,5);
real_sector=num(3:end,6);
finance_sector=num(3:end,7);
idx1=weekday(flowsdates)==7;
idx2=weekday(flowsdates)==1; %% kill sunday and saturday
idx=idx1+idx2;
flowsdates=flowsdates(~idx);
foreign_total=foreign_total(~idx);
foreign_total=fillmissing(foreign_total,'linear');
inst=inst(~idx);
inst=fillmissing(inst,'linear');
real_sector=real_sector(~idx);
real_sector=fillmissing(real_sector,'linear');
finance_sector=finance_sector(~idx);
finance_sector=fillmissing(finance_sector,'linear');
% foreign_total,inst,real_sector,finance_sector
flows=[foreign_total,inst,real_sector,finance_sector];
save('flows','flows');
save('flowsdates','flowsdates');

%% Interest rates
[num,~,raw]=xlsread([ini.dirs.raw '/interest_rates/interest - 03-01.xlsx'],'Sheet2');
dates=datenum(raw(2:end,1),'dd/mm/yyyy');
interestdates=dates;
save('interestdates','interestdates');
uslibor=NaN(size(dates,1),6); %% 1 on , 1M, 3M, 6M, 9M, 12M
telbor=NaN(size(dates,1),6); %% 1 on, 1M, 3M, 6M, 9M, 12M
uslibor(:,[1:4 6])=num(:,1:5); %% there is no 9M libor. We will interpolate it
uslibor(:,5)=uslibor(:,4)*0.5+uslibor(:,6).*0.5;
%% if zero put NaN and then interpolate
uslibor(uslibor(:,1)==0,1)=NaN;uslibor(uslibor(:,2)==0,2)=NaN;uslibor(uslibor(:,3)==0,3)=NaN;uslibor(uslibor(:,4)==0,4)=NaN;uslibor(uslibor(:,5)==0,5)=NaN;uslibor(uslibor(:,6)==0,6)=NaN;
%% count how many are missing
idx=find(interestdates==datenum('01/01/2013'));
missinglibor=sum(isnan(uslibor(idx:end,1)));
uslibor=fillmissing(uslibor,'linear');
telbor=num(:,6:11); %% there is no 9M libor. We will interpolate it

%% if zero put NaN and then interpolate
telbor(telbor(:,1)==0,1)=NaN;telbor(telbor(:,2)==0,2)=NaN;telbor(telbor(:,3)==0,3)=NaN;telbor(telbor(:,4)==0,4)=NaN;telbor(telbor(:,5)==0,5)=NaN;telbor(telbor(:,6)==0,6)=NaN;
idx=find(interestdates==datenum('01/01/2013'));
missingtelbor=sum(isnan(telbor(idx:end,1)));
telbor=fillmissing(telbor,'linear');
save('uslibor','uslibor');save('telbor','telbor');

%% CDS
[num,~,raw]=xlsread([ini.dirs.raw '/cds/cds5year.xlsx']);
dates=datenum(raw(2:end,1),'mm/dd/yyyy');
cdsdates=dates;
cds=cell2mat(raw(2:end,2));
save('cds','cds');save('cdsdates','cdsdates');

%% Volume: up to 31/12.
[num,~,raw]=xlsread([ini.dirs.raw '/daily volumes/daily_volume.xlsx']);
dates=datenum(raw(2:end,1),'mm/dd/yyyy');
fspotvol=num(:,1);
swapvol=num(:,2);
otcvol=num(:,3);
voldates=dates;
save('voldates','voldates'); save('fspotvol','fspotvol'); save('swapvol','swapvol');save('otcvol','otcvol');

%% Load zero coupon ten minus one

%% Global VIX
[num,~,raw]=xlsread([ini.dirs.raw '/vix/vix.xlsx']);
dates=datenum(raw(2:end,1),'mm/dd/yyyy');
vixdates=dates;
vix=num;
save('vix','vix');save('vixdates','vixdates');


%% s&p 500
[num,~,raw]=xlsread([ini.dirs.raw '/sp500/sp500.xlsx']);
dates=datenum(raw(2:end,1),'mm/dd/yyyy');
spdates=dates;
sp500=num;
save('sp500','sp500');save('spdates','spdates');


%% Daily interventions
[num,~,raw]=xlsread([ini.dirs.raw '/intervention/intervention_new.xlsx']);
dates=datenum(raw(2:end,1),'dd/mm/yyyy');
daily_intervention=cell2mat(raw(2:end,2));
daily_intervention_dates=dates;
T=size(ccydates,1);
intervention=NaN(T,1);
interventiondates=ccydates;
idx=ismember(interventiondates,daily_intervention_dates);
intervention(idx)=daily_intervention;
save('intervention','intervention');
save('interventiondates','interventiondates');

%% forwards: 1m, 3m, 6m, 12m
data=readtable([ini.dirs.raw '/forwards/ccysforwards.xlsx']);
forwarddates=datenum(data.Dates);
forwards=[data.x1MonthForward,data.x3MonthForward,data.x6MonthForward,data.x9MonthForward,data.x12MonthForward];
save('forwards','forwards');
save('forwarddates','forwarddates');
newforwards=NaN(T,5);
%newbasis=NaN(T,4);
[idx1]=ismember(ccydates,forwarddates);
[idx2]=ismember(forwarddates,ccydates);
newforwards(idx1,:)=forwards(idx2,:);
save('newforwards','newforwards');

%% Load the volume
load_volume;


%% Table 1 - Summary statistics

Figure1;

table1_2_3_appendix; %% summary statistics+includes results for table1+table2+table3+table B.2 in the appendix.

tableD2D3D4; %% correl. Goes to APPENDIX (Table D.2, D.3, D.4)

FigureE2E3E4; %% BAS of RR+BF+ATMV (goes to APPENDIX)

Figure2; %% Before after intervention ala distayat et al. (Figure 2 in the paper)

table_f2; %% intervention predictability table (appendix, also includes Table F.1)

table4; 

table5;

table6;

table7;

Figure3;

table8;

Figure4;

Appendix_G; %% creates the Figures in Appendix G

Appendix_H; %% creates the Figures in Appendix G

Appendix_I1; 

Figure_J;

Appendix_J; 

Figure_I1;
