📄 dfdb_port_opt.m
字号:
function dfdb_port_opt(varargin)
% DFDB_PORT_OPT Database/Datafeed Portfolio Optimizer - Graphical User Interface.
% DFDB_PORT_OPT imports security time series data from Yahoo or a database,
% plots it, and then computes the efficient frontier of a portfolio made up of
% the securities.
%
% Toolboxes Needed: Financial, Optimization, Statistics, Database, Datafeed
%
% Setup:
% Set up a datasource though ODBC on Windows from the assets2000.mdb database
% called DFDBsample. There is an explanation of how to do this in the Database
% Toolbox User抯 Guide.
%
% Selecting Data Inport Method:
% Pull down the Filee menu, select Preferences, then Data Source.
% Once inside Data Source, select either Datafeed or Database.
%
% Importing Time Series Data:
% If using the Datafeed option:
% Type the Yahoo recognized symbols of the securities you wish to use in the
% editable text box, labeled "Enter Symbols Separated by Commas." Enter as many
% securities as you wish. Symbols need to be separated by commas. Use the "Help ->
% Symbol Lookup" menu for assistance.
%
% Example:
% ibm,msft,csco
%
% Select a starting date for the time series to be imported. If on a PC, an
% ActiveX calendar may be brought up to view and select a date. The time series
% ends with the yesterday's closing price.
%
% If using the Database option:
% Use the "Select Data" window to select the Data Source and fields from the
% database. Clicking the "Apply" button will send the Date and Asset choices for
% importing. The database option does not allow setting the startdate. Also,
% the database set up to work with this tool is DFDBsample. The time series date
% range stored in DFDBsample is from January 2, 1997 to December 8, 2000.
%
% To import the time series data and plot it, press the button labeled,
% "Get Time Series." To Scale the time series data to one, select the
% checkbox labelled "Scale Time Series."
%
% Plotting Efficient Frontier:
% Press the button, "Generate Frontier." Make sure that the data has been loaded
% before attempting to generate the frontier.
%
% Find the Security Distribution Given a Desired Rate of Return:
% Enter the desired rate of return or desired risk in the appropriate editable
% text box. The number entered needs to be within the upper and lower bounds
% shown in red. The fraction of each security in your portfolio needed to reach
% the desired rate of return or risk will then be shown underneath. This can also
% be done graphically by dragging the green lines along the efficient frontier.
% Author: Brian Kiernan
% Date: May 21, 2001
if nargin == 0
call = 'Setup';
shh = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on')
F1 = findobj(0,'tag','dfdbportopt');
set(0,'ShowHiddenHandles',shh);
if ~isempty(F1)
figure(F1)
return
end
else
call = varargin{1};
end
if ~ispref('DFDBpref','datainput')
setpref('DFDBpref','datainput','df')
end
if ~ispref('DFDBpref','scaling')
setpref('DFDBpref','scaling','on')
end
persistent cal
switch call
case 'Setup'
% Create GUI
screenRect = get(0,'screensize');
F1=hgload('mymenufile.fig');
fpos = get(F1,'pos');
set(F1,'pos',[20 screenRect(4)-fpos(4)-50 fpos(3) fpos(4)],'defaultaxesunits','norm', ...
'defaultuicontrolunits','norm','defaultuicontrolfontsize',9,'defaulttextfontsize',9, ...
'defaulttextcolor','b','menubar','none','numbertitle','off','name', ...
'Data Input Portfolio Optimization','tag','dfdbportopt','closerequestfcn', ...
'dfdb_port_opt(''Closecall'')');
prefmenu = uimenu(F1,'label','&Preferences','separator','on');
datasourcemenu = uimenu(prefmenu,'label','&Data Source');
dataf=uimenu(datasourcemenu,'label','datafeed','tag','dfmen','callback', ...
'setpref(''DFDBpref'',''datainput'',''df''); dfdb_port_opt(''Datasource'');');
datab=uimenu(datasourcemenu,'label','database','tag','dbmen','callback', ...
'setpref(''DFDBpref'',''datainput'',''db''); dfdb_port_opt(''Datasource'')');
pref = getpref('DFDBpref','datainput');
if strcmp(pref,'df')
set(findobj(gcf,'tag','dfmen'),'checked','on')
set(findobj(gcf,'tag','dbmen'),'checked','off')
elseif strcmp(pref,'db')
set(findobj(gcf,'tag','dbmen'),'checked','on')
set(findobj(gcf,'tag','dfmen'),'checked','off')
end
helpmenu = uimenu(F1,'label','Help');
toolhelp = uimenu(helpmenu,'label','&DFDB_Port_Opt Help','callback', ...
'dfdb_port_opt(''Helppage'')');
symlookup = uimenu(helpmenu,'label','Symbol Lookup','callback','dfdb_port_opt(''Yahoosym'')');
% C = yahoo;
% if ~isconnection(C)
% set(symlookup,'enable','off')
% end
% clear C
closemenu = uimenu(findobj(F1,'label','&File'),'label','Close', ...
'callback','dfdb_port_opt(''Closecall'')');
Tseries = axes('pos',[.4 .57 .52 .39],'tag','tsax');
set(Tseries,'xtick',0:.2:1,'ytick',0:.2:1);
set(gca,'fontsize',8,'xticklabel','','yticklabel','')
xlabel('Time')
Ylabel('Price')
title('Time Series','color','k')
grid on
Effront = axes('units','norm','pos',[.4 .07 .52 .39],'tag','efax');
set(Effront,'xtick',0:.2:1,'ytick',0:.2:1);
set(gca,'fontsize',8,'xticklabel','','yticklabel','')
xlabel('Volatility (\sigma)')
ylabel('Rate of Return')
title('Efficient Frontier','color','k')
grid on
axes(Tseries)
Symbolprompt = uicontrol('style','text','pos',[.01 .95 .33 .03],'fontsize',9, ...
'string','Enter Symbols Separated by Commas','backgroundcolor',[.8 .8 .8], ...
'foregroundcolor','b','tag','symp');
Symbols = uicontrol('style','edit','string','','pos',[.02 .9 .18 .04], ...
'horizontalalignment','left','backgroundcolor','w','tag','symstr');
Dscall = uicontrol('pos',[.21 .9 .12 .04],'string','Select Data','tag','dsbut', ...
'callback','DFDB_port_opt(''SelectData'')');
% Starting date of time series
axes(Tseries)
Startdateprompt = uicontrol('style','text','pos',[.05 .8 .25 .03],'fontsize',9, ...
'string','Enter Starting Date (m/d/yyyy)','backgroundcolor',[.8 .8 .8], ...
'foregroundcolor','b','tag','sdp');
if ispc
Startdatecal = uicontrol('string','View Calendar','pos',[.045 .73 .13 .05], ...
'callback','dfdb_port_opt(''Calendar'')','tag','calbut');
Startdatebox = uicontrol('style','edit','string','','pos',[.185 .735 .12 .04], ...
'backgroundcolor','w','tag','sdstr','callback','dfdb_port_opt(''Datecheck'')');
if isequal(getpref('DFDBpref','datainput'),'db')
set(Startdatecal,'enable','off')
end
else
Startdatebox = uicontrol('style','edit','string','','pos',[.09 .753 .12 .04], ...
'backgroundcolor','w','tag','sdstr');
end
if isequal(getpref('DFDBpref','datainput'),'db')
set(Startdateprompt,'enable','off')
set(Startdatebox,'string','1/2/1997','enable','off')
else
set(Dscall,'enable','off')
end
% scale time series checkbox
TSscale = uicontrol('style','check','string','Scale Time Series','pos',[.09 .66 .18 .02], ...
'backgroundcolor',[.8 .8 .8],'callback','dfdb_port_opt(''ScaleTS'')','tag','scalechk', ...
'foregroundcolor','b');
if strcmp(getpref('DFDBpref','scaling'),'on') == 1
set(TSscale,'value',1)
else
set(TSscale,'value',0)
end
% time series generator button
TScall = uicontrol('string','Get Time Series','pos',[.07 .57 .2 .05], ...
'callback','dfdb_port_opt(''GetData'')','tag','tsbut');
% efficient frontier generator button
Frontiercall = uicontrol('string','Generate Frontier','pos',[.07 .41 .2 .05], ...
'callback','dfdb_port_opt(''EffFront'')','tag','efbut','enable','off');
% Rate of Return
axes(Effront);
Rateofreturnprompt = uicontrol('style','text','pos',[.005 .37 .13 .03],'string', ...
'Rate of Return','foregroundcolor','b','backgroundcolor',[.8 .8 .8],'tag', ...
'rorp','enable','off');
Rateofreturn = uicontrol('style','edit','string','','pos',[.03 .33 .08 .04], ...
'callback','dfdb_port_opt(''WeightCalcRoR'')','backgroundcolor','w','tag','rorstr', ...
'enable','off');
Riskprompt = uicontrol('style','text','pos',[.235 .37 .05 .03],'string', ...
'Risk','foregroundcolor','b','backgroundcolor',[.8 .8 .8],'tag', ...
'riskp','enable','off');
Risk = uicontrol('style','edit','string','','pos',[.22 .33 .08 .04], ...
'callback','dfdb_port_opt(''WeightCalcRisk'')','backgroundcolor','w','tag','riskstr', ...
'enable','off');
% Securites list with corresponding weights
Securtext = uicontrol('style','text','backgroundcolor',[.8 .8 .8],'foregroundcolor',[.66 0 .11], ...
'pos',[.06 .295 .1 .028],'string','Securites','fontsize',9,'tag','securtxt','enable','off');
Securweights = uicontrol('style','text','backgroundcolor',[.8 .8 .8],'foregroundcolor',[.66 0 .11], ...
'pos',[.17 .295 .1 .028],'string','Weights','fontsize',9,'tag','securwts','enable','off');
set(F1,'handlevis','callback');
pref = getpref('DFDBpref','datainput');
if strcmp(pref,'db')
DFDB_port_opt('SelectData','start')
end
case 'Datecheck'
% check for correct date format
Startdatebox = findobj(gcf,'tag','sdstr');
startdate = get(Startdatebox,'string');
slash = find(startdate == '/');
if length(startdate(slash(end)+1:end)) ~= 4
err = errordlg('Invalid Date','Enter Starting Date');
set(err,'windowstyle','modal')
return
end
try
datenum(startdate);
catch
err = errordlg('Invalid Date','Enter Starting Date');
set(err,'windowstyle','modal')
return
end
case 'GetData'
% Retrieve time series from the Yahoo server or from local database.
% clear plots and data from of earlier runs
%%%%%%%%%%
Tseries = findobj(gcf,'tag','tsax');
Effront = findobj(gcf,'tag','efax');
axes(Effront)
cla(Effront)
axes(Tseries)
cla(Tseries)
drawnow
Rateofreturn = findobj(gcf,'tag','rorstr');
Rateofreturnprompt = findobj(gcf,'tag','rorp');
Risk = findobj(gcf,'tag','riskstr');
Riskprompt = findobj(gcf,'tag','riskp');
Frontiercall = findobj(gcf,'tag','efbut');
Securtext = findobj(gcf,'tag','securtxt');
Securweights = findobj(gcf,'tag','securwts');
WtText = findobj(gcf,'tag','wttxt');
WtWeight = findobj(gcf,'tag','wtwt');
leg = findobj(gcf,'tag','legend');
Frontleg = findobj(gcf,'tag','frleg');
set(Rateofreturn,'string','')
set(Risk,'string','')
delete(WtText)
delete(WtWeight)
delete(leg)
delete(Frontleg)
clear WtText WtWeight leg Frontleg
drawnow
%%%%%%%%%
pref=getpref('DFDBpref','datainput');
% try
% Read in time series data for entered symbols and separate
%%%%%%%%%
Symbols = findobj(gcf,'tag','symstr');
Strings = get(Symbols,'string');
wtspace = find(Strings == ' ');
Strings(wtspace) = [];
commas = find(Strings == ',');
commas = [0, commas, length(Strings)+1];
symcell = cell(length(commas)-1,1);
data = cell(length(symcell),1);
for i=1:length(commas)-1
symcell{i}=Strings(commas(i)+1:commas(i+1)-1);
end
%%%%%%%%%
Startdatebox = findobj(gcf,'tag','sdstr');
% test for data source
if strcmp(pref,'df')
%
% Datafeed data input option
%
try
C=yahoo;
catch
err = errordlg('Connection to Yahoo Failed','Datafeed Connection');
set(err,'windowstyle','modal')
return
end
setappdata(Tseries,'Connection',C);
yesterday = datestr(datenum(date)-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -