MATLAB Answers

0

Financial contract value

CapaB 님이 질문을 제출함. 10 Jun 2012
My bank sells this contract:
If Apple, Coca-Cola, Volkswagen or H&M dont fall more than 50% over the next 3 years i get a return of 35%. 32.4% including commissions.
(Its between the start on 25 June 2012 and end on 25 June 2015. The fall between these two dates should have to be no more than 50%)
If any one of them fall by more than 50% i get my investment reduced by how much the worst stock has fallen.
I tried to calculate the value of such a contract based on historical prices from 1 Jan 1994.
*****************************'
clear
Tot=0;
n=4655-755; % Number of 3-year periods
T1=0; % Number of these which yields a positive return
T2=0; % Number of these which yields a negative return
fid = fopen( 'AppHist2.csv', 'r' ); % Read Apple data
str = fread( fid, '*char' );
sts = fclose( fid );
str( str == '"' ) = [];
cac = textscan( str, '%f' );
AppH = cell2mat(cac);
for i=1:4655
AppHO(i)=AppH(4656-i);
end
fid2 = fopen( 'CoHist.csv', 'r' ); % Read Coca-Cola data
str2 = fread( fid2, '*char' );
sts2 = fclose( fid2 );
str2( str2 == '"' ) = [];
cac2 = textscan( str2, '%f' );
CoH = cell2mat(cac2);
for i=1:4655
CoHO(i)=CoH(4656-i);
end
fid3 = fopen( 'VwHist4.csv', 'r' ); % Read Volkswagen data
str3 = fread( fid3, '*char' );
sts3 = fclose( fid3 );
str3( str3 == '"' ) = [];
str3( str3 == ',' ) = '.';
cac3 = textscan( str3, '%f' );
VwH = cell2mat(cac3);
for i=1:3416
VwHO(i)=VwH(3417-i);
end
fid4 = fopen( 'HMhist.csv', 'r' ); % Read HM data
str4 = fread( fid4, '*char' );
sts4 = fclose( fid4 );
str4( str4 == '"' ) = [];
str4( str4 == ',' ) = '.';
cac4 = textscan( str4, '%f' );
HmH = cell2mat(cac4);
for i=1:4628
HmHO(i)=HmH(4629-i);
end
P3=1; P4=1;
for i=1:n
% run through the data from 1 jan 1994
K=AppHO(i+755)-AppHO(i);
P=K/AppHO(i);
% Apple. Change in % between two points separated by 3 years.
K2=CoHO(i+755)-CoHO(i);
P2=K2/CoHO(i);
% Coca-Cola. Change in % between two points separated by 3 years.
if i>1239 % less data for VW.
j=i-1239;
K3=VwHO(j+755)-VwHO(j);
P3=K3/VwHO(j);
% VW. Change in % between two points separated by 3 years.
end
if i>27
% 27 data points less for HM.
k=i-27;
K4=HmHO(k+755)-HmHO(k);
P4=K4/HmHO(k);
% HM. Change in % between two points separated by 3 years.
end
if (P>=-0.5)&(P2>=-0.5)&(P3>=-0.5)&(P4>=-0.5)
T=9800*1.35;
% 35% return if none of the four stocks have fallen more than 50%
% Contract size is 10000 - 200 in commision.
T1 = T1+1;
else
S=[P P2 P3 P4]
Smin=min(S)
T=9800+9800*Smin
% If any stock have fallen by more than 50% my investment is reduced by how much the worst stock has fallen.
T2 = T2+1;
end
end
T;
Tot = Tot + T;
end
OutcomePerContract=Tot/n
Return=(OutcomePerContract-10000)/10000
NumberPosDays=T1
NumberNegDays=T2
*************************************
OutcomePerContract =
1.1801e+04
Return =
0.1801
NumberPosDays =
3311
NumberNegDays =
589
***********************************************
If anyone has any ideas on how this can be improved (mathematically or the code. Ofc the data is a problem but that was the best i could do. ) or if there is any errors, I am happy to hear about it.

  댓글 수: 0

로그인 to comment.

답변 수: 1

Geoff 님의 답변 10 Jun 2012

Just a snippet of coding advice from me...
If you need to use a number more than once, especially if it relates to how many values you have, etc, DON'T put it in as a literal constant. This includes numbers that are derived mathematically from others... Like 3416, 4628, 755, 1239. What are they? What if you need to change 4665 to something else? How does that change your other numbers? How can you make all the changes without missing something and breaking your code? Put these values into variables with meaningful names and use them consistently.
Here's another small MatLab thing that can help make your code clearer.... You have:
if (P>=-0.5)&(P2>=-0.5)&(P3>=-0.5)&(P4>=-0.5)
I have to read the whole thing carefully before realising that the meaning is:
if all( [P,P2,P3,P4] >= -0.5 )
Consider that syntax if you're not concerned about exploiting logic shortcutting.

  댓글 수: 0

로그인 to comment.



Translated by