Calling a function with a loop
3 views (last 30 days)
Show older comments
I am calling a function that is one .m file from another .m file. Anytime I want to call the "Single" function I get error messages. What am I doing wrong here? Can someone debug my code please?
Thank you in advance!
File 1: Main program
data_set= importdata('/Users/.../Desktop/Fall2018/6214_AssetPotfolio/Assignment1/Return_data_import_Assignment1.xlsx');
%importing data from the root directory%
indexdata = data_set.data(:,1:2);
%grabbing and renaming the date column and the SPXT data column
%(:, 1:2)- all rows and columns 1 and 2
indexdata(:,1) = x2mdate(indexdata(:,1));
%converting the data format into a Matlab Data format%
%(:, 1)- all rows and first column "#x2mdate" command
return_data = indexdata;
%rename the spxtdata as return_data
annual_factor=12;
rolling_window=12;
%these are used to annualize the data
%rolling window to compute the rolling returns
Single (return_data, annual_factor,rolling_window)
%Calling the function which inputs the return data vector annulization
%factor and the rolling window values
File 2: Function
function results= Single (return_data, annual_factor, rolling_window)
%funtion name is defined as follows:
%function name (variables inputs which are locally defined-see below)
%This function calculates the statistics for a single vector
%The input variables in the function are:
%return data: nx2 vector with date in col1, monthly returns in col2
%annual factor: 12 which is the number of periods used to annualize the values
%rolling window: the number of periods used to compute the rolling returns,
%i.e 12 in this case
%Outputs:
%results.Stats: Column vector of the 5 basic stats: (1) Annulized Returns, (2)
%Annulaized Volatility, (3) Return to Risk Ratio, (4) Maximum Drawdown
%Value, (5) Max 1 Year Loss
%results.labels: A matrix of dates and rolling "rolling_window"period returns
%Main program for the function:
gross_return=cumprod(return_data(:,2)+1);
%gross cumulative returns calculated
obs=size(return_data,1);
%number of observations in the data
annual_return=gross_return(end)^(annual_factor/obs)-1;
%annualized formula of the gross return
annual_volatility = sqrt(annual_factor)*std(return_data(:,2));
%annualizing the value of the volatility
risk_return=annual_return/annual_volatility;
%the risk to return ratio is calculated
MaxDD=maxdrawdown(gross_return);
%maximum drawdown is calculated using built in function
no_periods=obs-rolling_window+1;
%the number of periods
rolling_year=zeros(no_periods, 2);
%the rolling year zero matrix is used to store the rolling year values
for i=1:no_periods
total_cumulprod= cumprod(return_data(1:1+annual_factor-1,2)+1);
rolling_year (i,1)=return_data(1+annual_factor-1,1);
rolling_year(i,2)=total_cumulprod(end)-1;
end
max1yrloss=min(rolling_year(:, 2));
results.Stats=[annual_return; annual_volatility; risk_return, MaxDD; max1yrloss];
results.Rollyr=rolling_year;
results.labels={"Annual Return"; "Annual Volatility"; "Risk to Return";
"Maximum Drawdown"; "Max 1year Loss"};
end
2 Comments
Walter Roberson
on 14 Sep 2018
What is the complete error message?
We do not have your data so we cannot test the code ourselves.
Stephen23
on 15 Sep 2018
I get various error messages which I suspect are all interrelated. I have attached the data file here. The data file is a xls file of the various stock indeax returns. I am taking a single return vector as an input (column 2 of data) and outputing the following indices as a matrix:
(1) annual_return=gross_return(end)^(annual_factor/obs)-1;
(2) annual_volatility = sqrt(annual_factor)*std(return_data(:,2));
(3) risk_return=annual_return/annual_volatility;
(4) MaxDD=maxdrawdown(gross_return);
(5) max1yrloss=min(rolling_year(:, 2))
Answers (1)
Walter Roberson
on 15 Sep 2018
You have
results.Stats=[annual_return; annual_volatility; risk_return, MaxDD; max1yrloss];
Notice the comma: it needs to be semi-colon.
0 Comments
See Also
Categories
Find more on Web Services in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!