Financial Series Data Crisis Identification based on Increase in Prices rather then decrease in price. Please help i am anew MATLAB user

% Specify the selected data that you would like to run.
% 1) Date
% 2) S&P GSCI Gold / Precious Metals
clear
clc
data=xlsread('Metals');
t=datetime(data(:,1),'ConvertFrom','excel');
data=data(1:end,[1 2]);
u=60
b=0.41
%% Preliminary Coding:
% IT: Market state indicator with the first column indicating the date and
% the second been given a value of 1 if in a bull / non-crisis period state
% Xmax & Xmin: refer to local minimum and maximum from which the actual
% time series must deviate by a specified threshold.
% tau: specifies the last date since the local minimum or maximum.
% n: refers to how many different values the lambda 1 and 2 parameters are
% allowed to take. E.g. If lambda 1 and 2 are allowed to vary between 0.01
% and 0.40 with steps of 0.01, then n=40. This logically leads to 1600
% different possible combinations of lambda 1 and lambda 2 that can be tested
% in the loop below.
% bear: bear is a variable that specifies the beginning and end of a
% crisis / bear market respectively by the values 1 and 2.
IT=data(:,1);
IT(1,2)=1;
xmax=data(:,1);
xmin=data(:,1);
xmax(1,2)=data(1,2);
xmin(1,2)=data(1,2);
tau=data(:,1);
tau(1,2)=data(1,1);
tau(1,3)=data(1,1);
n=40
x=linspace(0.01,0.40,n)';
y=linspace(0.01,0.40,n)';
returns(:,1)=data(2:end,1);
returns(:,2)=price2ret(data(:,2));
bear(:,1)=IT(:,1);
bear(:,2)=0;
% Create a n^2x2 vector of all possible combinations of lambda 1 and lambda 2
% given n.
for i=1:n
for j=1:n
z(i,:,j)=[x(i,1) y(j,1)];
end
end
h=z(:,:,1);
for i=2:n
h=vertcat(h(:,:,1),z(:,:,i));
end
%% Optimization of the lambda parameters
% J-loop: loop over all different possible combinations of parameters lambda
% 1 and lambda 2
% i-loop: Loop over all values of the time series to determine the market
% state.
c=waitbar(0,'Please wait')
for j=1:size(h,1)
c=waitbar(j/size(h,1));
landa1=h(j,1);
landa2=h(j,2);
for i=2:size(IT,1)
if IT(i-1,2)==1
if data(i,2)>xmax(i-1,2)
IT(i,2)=1;
xmax(i,2)=data(i,2);
xmin(i,2)=xmin(i-1,2);
tau(i,2)=data(i,1);
tau(i,3)=tau(i-1,3);
elseif data(i,2)<(1-landa1)*xmax(i-1,2)
IT(find(data(:,1)==tau(i-1,2)):i,2)=0;
xmax(i,2)=data(i,2);
xmin(i,2)=data(i,2);
tau(i,2)=data(i,1);
tau(i,3)=data(i,1);
elseif data(i,2)>(1-landa1)*xmax(i-1,2)
IT(i,2)=1;
xmax(i,2)=xmax(i-1,2);
xmin(i,2)=xmin(i-1,2);
tau(i,2)=tau(i-1,2);
tau(i,3)=tau(i-1,3);
end
elseif IT(i-1,2)==0
if data(i,2)<xmin(i-1,2)
IT(i,2)=0;
xmax(i,2)=xmax(i-1,2);
xmin(i,2)=data(i,2);
tau(i,2)=tau(i-1,2);
tau(i,3)=data(i,1);
elseif data(i,2)>(1+landa2)*xmin(i-1,2)
IT(find(data(:,1)==tau(i-1,3)):i,2)=1;
xmax(i,2)=data(i,2);
xmin(i,2)=data(i,2);
tau(i,2)=data(i,1);
tau(i,3)=data(i,1);
elseif data(i,2)<(1+landa2)*xmin(i-1,2)
IT(i,2)=0;
xmin(i,2)=xmin(i-1,2);
xmax(i,2)=xmax(i-1,2);
tau(i,2)=tau(i-1,2);
tau(i,3)=tau(i-1,3);
end
end
end
% Determine the beginning and end of the bear/crisis state
bear(:,2)=0;
for i=2:size(bear,1)
if (IT(i,2)==0) && (IT(i-1,2)==1)
bear(i,2)=1;
elseif (IT(i,2)==1) && (IT(i-1,2)==0)
bear(i,2)=2;
end
end
% Enforce the proper alteration between beginning and end
% of a crisis period. In other words, if you end in a crisis state,
% then there is a 1, but not a 2 leading to a dimension mismatch in the
% summary matrix below. Thus, the bear state must be concluded, as well
% as the fact that a start in a bear state must have a beginning.
if IT(end,2)==0
bear(end,2)=2;
end
if IT(1,2)==0
bear(1,2)=1;
end
summary=zeros(size(find(bear(:,2)==1),1),2);
summary(:,1)=bear(find(bear(:,2)==1),1);
summary(:,2)=bear(find(bear(:,2)==2),1);
for i=2:size(summary,1)
summary(i,3)=summary(i,1)-summary(i-1,2);
end
for i=2:size(summary,1)
if summary(i,3)<u
IT(find(summary(i-1,1)==IT(:,1)):find(summary(i,2)==IT(:,1)),2)=0;
end
end
% redetermine the beginning and end of the bear/crisis state
bear(:,2)=0;
for i=2:size(bear,1)
if (IT(i,2)==0) && (IT(i-1,2)==1)
bear(i,2)=1;
elseif (IT(i,2)==1) && (IT(i-1,2)==0)
bear(i,2)=2;
end
end
if IT(end,2)==0
bear(end,2)=2;
end
if IT(1,2)==0
bear(1,2)=1;
end
% Summary matrix that shows the start of a crisis, the end, the fall in
% the market over a specific duration of the crisis period, the
% intensity factor of the crisis and the standard deviation of the
% return over the crisis period.
summary=zeros(size(find(bear(:,2)==1),1),2);
summary(:,1)=bear(find(bear(:,2)==1),1);
summary(:,2)=bear(find(bear(:,2)==2),1);
if size(summary,1)==0
IF(j,1)=0;
IF(j,2)=0;
IF(j,3)=0;
IF(j,4)=0;
else for i=1:size(summary,1)
summary(i,3)=log(data(find(data(:,1)==summary(i,2)),2)/data(find(data(:,1)==summary(i,1)),2));
summary(i,5)=summary(i,2)-summary(i,1);
summary(i,4)=std(returns(find(returns(:,1)==summary(i,1)):find(returns(:,1)==summary(i,2)),2))*sqrt(summary(i,5));
summary(i,6)=summary(i,3)/(summary(i,5)/365);
end
% Save the respective mean intensity factor over the crisis period
% for a given lambda 1 and 2 parameter combination, the standard
% deviation of these intensity factors, their intensity factor
% adjusted for IF-volatility and the amount of crises identified by
% the algorithm.
IF(j,1)=mean(summary(:,6));
IF(j,2)=std(summary(:,6));
IF(j,3)=IF(j,1)/IF(j,2);
IF(j,4)=size(summary,1);
end
end
% Remove lambda parameters that lead to only a single crisis being
% identified. This would lead to an infinite IF-adj.
for i=1:size(IF,1)
if IF(i,4)==1
IF(i,5)=0;
else IF(i,5)=1;
end
end
% Enforce a severe enough intensity factor upon the series. In other words,
% restrict the lambda parameters combinations only to those that provide an
% intensity factor that is more severe than the overall average intensity
% factor (in absolute value).
IFadj=IF(find(IF(:,5)==1),:);
z=mean(IFadj(:,1))
e=std(IFadj(:,1))
z=z
for i=1:size(IF,1)
if IF(i,1)<z
IF(i,6)=1;
else IF(i,6)=0;
end
end
% Enforce a the rebound to be smaller than the fall in the market.
for i=1:size(IF,1)
if h(i,1)>h(i,2)
IF(i,8)=1;
else IF(i,8)=0;
end
end
close(c)
IFadj=IF(find(IF(:,5)==1&IF(:,6)==1&IF(:,8)==1),:);
a=min(IFadj(:,3))
optimumparameters=h(find(IF(:,3)==a),:)
%% Employ the identified parameters to detect crisis periods.
landa1=optimumparameters(1,1);
landa2=optimumparameters(1,2);
c=waitbar(0,'Please Wait')
% Preliminary Coding:
IT=data(:,1);
IT(1,2)=1;
xmax=data(:,1);
xmin=data(:,2);
xmax(1,2)=data(1,2);
xmin(1,2)=data(1,2);
tau=data(:,1);
tau(1,2)=data(1,1);
tau(1,3)=data(1,1);
for i=2:size(IT,1)
c=waitbar(i/size(IT,1));
if IT(i-1,2)==1
if data(i,2)>xmax(i-1,2)
IT(i,2)=1;
xmax(i,2)=data(i,2);
xmin(i,2)=xmin(i-1,2);
tau(i,2)=data(i,1);
tau(i,3)=tau(i-1,3);
elseif data(i,2)<(1-landa1)*xmax(i-1,2)
IT(find(data(:,1)==tau(i-1,2)):i,2)=0;
xmax(i,2)=data(i,2);
xmin(i,2)=data(i,2);
tau(i,2)=data(i,1);
tau(i,3)=data(i,1);
elseif data(i,2)>(1-landa1)*xmax(i-1,2)
IT(i,2)=1;
xmax(i,2)=xmax(i-1,2);
xmin(i,2)=xmin(i-1,2);
tau(i,2)=tau(i-1,2);
tau(i,3)=tau(i-1,3);
end
elseif IT(i-1,2)==0
if data(i,2)<xmin(i-1,2)
IT(i,2)=0;
xmax(i,2)=xmax(i-1,2);
xmin(i,2)=data(i,2);
tau(i,2)=tau(i-1,2);
tau(i,3)=data(i,1);
elseif data(i,2)>(1+landa2)*xmin(i-1,2)
IT(find(data(:,1)==tau(i-1,3)):i,2)=1;
xmax(i,2)=data(i,2);
xmin(i,2)=data(i,2);
tau(i,2)=data(i,1);
tau(i,3)=data(i,1);
elseif data(i,2)<(1+landa2)*xmin(i-1,2)
IT(i,2)=0;
xmin(i,2)=xmin(i-1,2);
xmax(i,2)=xmax(i-1,2);
tau(i,2)=tau(i-1,2);
tau(i,3)=tau(i-1,3);
end
end
end
%% Analysis of features of a crisis state
% Define beginning and end of bear-crisis period
bear(:,1)=IT(:,1);
bear(:,2)=0;
for i=2:size(bear,1)
for j=2:size(IT,2)
if IT(i,j)==0&IT(i-1,j)==1
bear(i,j)=1;
elseif IT(i,j)==1&IT(i-1,j)==0
bear(i,j)=2;
else 0;
end
end
end
% Additional restriction to make sure dimensions match for the next summary
% matrix (specifically, if you find yourself in a bear-crisis period at the
% end of the sample period than this needs to be ended, or the matrix
% dimensions shall not match in the subsequent functions
if IT(end,2)==0
bear(end,2)=2;
end
returns(:,1)=data(2:end,1);
returns(:,2)=price2ret(data(:,2));
summary=zeros(size(find(bear(:,2)==1),1),2);
summary(:,1)=bear(find(bear(:,2)==1),1);
summary(:,2)=bear(find(bear(:,2)==2),1);
% Defining the return over the entire crisis (bear) period, the duration,
% the standard deviation of the returns over the period and the intensity
% factor.
for i=1:size(summary,1)
summary(i,3)=log(data(find(data(:,1)==summary(i,2)),2)/data(find(data(:,1)==summary(i,1)),2));
summary(i,5)=summary(i,2)-summary(i,1);
summary(i,4)=std(returns(find(returns(:,1)==summary(i,1)):find(returns(:,1)==summary(i,2)),2))*sqrt(summary(i,5));
summary(i,6)=summary(i,3)/(summary(i,5)/365);
end
%% add an additional restriction
% Define how long each crisis (bear) period has been since the last one
for i=2:size(summary,1)
summary(i,7)=summary(i,1)-summary(i-1,2);
end
% Identify when this is smaller than 60 days
for i=2:size(summary,1)
if summary(i,7)<u
IT(find(summary(i-1,1)==IT(:,1)):find(summary(i,2)==IT(:,1)),2)=0;
end
end
%% Rerun code and make the period one, if it is less than 30 days since the
%former one
bear2(:,1)=IT(:,1);
bear2(1,2)=0;
for i=2:size(bear2,1)
for j=2:size(IT,2)
if IT(i,j)==0&&IT(i-1,j)==1
bear2(i,j)=1;
elseif IT(i,j)==1&&IT(i-1,j)==0
bear2(i,j)=2;
else 0;
end
end
end
if IT(end,2)==0
bear2(end,2)=2;
end
summary2=zeros(size(find(bear2(:,2)==1),1),2);
summary2(:,1)=bear2(find(bear2(:,2)==1),1);
summary2(:,2)=bear2(find(bear2(:,2)==2),1);
for i=1:size(summary2,1)
summary2(i,3)=log(data(find(data(:,1)==summary2(i,2)),2)/data(find(data(:,1)== summary2(i,1)),2));
summary2(i,5)=summary2(i,2)-summary2(i,1);
summary2(i,4)=std(returns(find(returns(:,1)==summary2(i,1)):find(returns(:,1)==summary2(i,2)),2))*sqrt(summary2(i,5));
summary2(i,6)=summary2(i,3)/(summary2(i,5)/365);
end
for i=2:size(summary2,1)
summary2(i,7)=summary2(i,1)-summary2(i-1,2);
end
close(c)
IT(find(IT(:,2)==0),2)=max(data(:,2));
IT(find(IT(:,2)==1),2)=0;
t=datetime(datevec(data(:,1)));
plot(t,data(:,2))
hold on
area(IT(:,1),IT(:,2),'FaceColor',[0.7 0.7 0.7],'EdgeColor',[0.7 0.7 0.7])
plot(t,data(:,2), 'k')
hold off
% My Code
t=datetime(data(:,1),'ConvertFrom','excel');
hold on
area(t,IT(:,2),'FaceColor',[0.7 0.7 0.7],'EdgeColor',[0.7 0.7 0.7])
plot(t,data(:,2), 'k')
hold off
landa1
landa2
meanIF=mean(summary2(:,6))
meanRet=mean(summary2(:,3))
stdIF=std(summary2(:,6))
Ncrisis=size(summary2,1)
meanIFADJ= meanIF / stdIF
meanTminust=mean(summary2(:,5));
###############
Now i want to change this code for crisis identification based on the increase in the prices of financial assets like Gold. As for Gold and other precious metals price increase can be classified as Crisis rather then the price decline.
%%%% Anyone can help me please. i am badly struck as i am new to MATLAB

Answers (0)

Categories

Products

Release

R2019b

Asked:

on 24 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!