【发布时间】:2020-05-25 15:11:42
【问题描述】:
我通过一个 for 循环迭代 250 次,在此迭代中,一项投资被投资到一项风险资产中并且正在执行。
如果投资资金的价值达到某个阈值,我需要停止 for 循环,以便我可以将风险资产中的资金重新投资到无风险资产中。
从那里开始,我希望循环继续剩余的步数(直到 250),但这次需要将值乘以无风险利率。
我的脚本停止直到达到阈值的那一刻,任何人都可以在这里帮助我如何继续迭代?
如果您正在寻找设置了 If 子句的 Treshold 部分--->它在第 65 行和第 84 行
%% market data & investor's preferences
% market
rF = 0.01; %Fixed return for riskfree asset
mu = 0.0031; %mean log return r- N(mu,sigma)
sigma = 0.19; %volatility
S0 = 100; %initial price
%investor
V0 = 100; %amount to invest, initial wealth
T = 1; %investment horizon years
adjust_every = 1; %once every (for example 25 days if = 25) 50would be 5 times a year
% try also 5, 63, 250
P = (0.05 * V0) + V0; %critical value of profit %take profit TP
Lc = V0 - (0.03 * V0); %critical value of losses %stop losses SL
alpha = 1; %fraction of wealth in risky asset
dt = 1/250; %time increments.. we are interested in daily, weekly .... (1/250) should be daily for example
% try also 1/50, 1/4 , 1
nExp = 1; %number of simulations
deltaAlphaCrit = 0.001; %try also for 0.001 0.005 , 0,01 , 0,02
%% simulate
%initialize variables for over time
M = round(T/dt) +1; %number of points in time to consider (we start at point 0)
rSim = randn(M,nExp) * sigma * sqrt(dt)+ mu * dt;
rSim(1,:) = 0;
S = S0 .* exp(cumsum(rSim));
for adjust_every =[1]
cash = nan(M,nExp);
risky = nan(M,nExp);
nStock = nan(M,nExp);
wealth = nan(M,nExp);
t = 1; %at the beginning of the investment horizon ...
cash(t,:) = V0 * (1-alpha); %cash initially
nStock(t,:) = (V0 * alpha) / S0;
risky(t,:) = nStock(t,:) .* S(t,:); % the risky asset is worth...
wealth(t,:) = cash(t,:) + risky(t,:);
% over time (what is happening)
for t = 1:(M-1)
%at tb (begining of period t)
tb = t;
%transcost = 0,005 * S; %transaction costs
if mod(tb-1, adjust_every) == 0
alphaCur = risky(tb,:) ./ wealth(tb,:); %current alpha , current fraction in risky asset
nOpt = wealth(tb,:) * alpha ./ S(tb,:); %optimal number of risky assets you want to own
deltaN = nOpt - nStock(tb,:); % how many stocks to buy/sell ? if delta is pos-> buy , neg-> sell
if risky(tb,:) > P
nStock(tb,:) = nStock(tb,:);
risky(tb,:) = risky(tb,:);
cash(tb,:) = risky(tb,:)+exp(rF*dt);
wealth(tb,:) = cash(tb,:);
continue
%at te (end of period t)
te = t+1;
nStock(te,:) = nStock(tb,:);
risky(te,:) = risky(tb,:);
cash(te,:) = cash(tb,:) * exp(rF*dt); %new cash postion %why exp ? -> log return
wealth(te,:) = cash(te,:);
%results
area([cash,risky])
plot(wealth)
VT = wealth(end,:);
rT = VT./V0-1; %result of overall performance
%break
elseif risky(tb,:) < Lc
nStock(tb,:) = nStock(tb,:);
risky(tb,:) = risky(tb,:);
cash(tb,:) = risky(tb,:)*exp(rF*dt);
wealth(tb,:) = cash(tb,:);
continue
%at te (end of period t)
te = t+1;
nStock(te,:) = nStock(tb,:);
risky(te,:) = risky(tb,:);
cash(te,:) = cash(tb,:) * exp(rF*dt); %new cash postion %why exp ? -> log return
wealth(te,:) = cash(te,:);
%results
area([cash,risky])
plot(wealth)
VT = wealth(end,:);
rT = VT./V0-1; %result of overall performance
%break
else
nStock(tb,:) = nStock(tb,:);
risky(tb,:) = nStock(tb,:) .* S(tb,:); %-transcost;
cash(tb,:) = cash(tb,:) - deltaN.*S(tb,:); %-transcost;
wealth(tb,:) = cash(tb,:) + risky(tb,:);
%results
area([cash,risky])
plot(wealth)
VT = wealth(end,:);
rT = VT./V0-1; %result of overall performance
%at te (end of period t)
te = t+1;
nStock(te,:) = nStock(tb,:);
risky(te,:) = nStock(te,:) .* S(te,:);
cash(te,:) = cash(tb,:) * exp(rF*dt); %new cash postion %why exp ? -> log return
wealth(te,:) = cash(te,:) + risky(te,:);
end
% changing postitions... what happened ?
% nStock(tb,:) = nStock(tb,:) + deltaN;
% risky(tb,:) = nStock(tb,:) .* S(tb,:); %-transcost;
% cash(tb,:) = cash(tb,:) - deltaN.*S(tb,:); %-transcost;
% wealth(tb,:) = cash(tb,:) + risky(tb,:);
end
%at te (end of period t)
%te = t+1;
%nStock(te,:) = nStock(tb,:);
%risky(te,:) = nStock(te,:) .* S(te,:);
%cash(te,:) = cash(tb,:) * exp(rF*dt); %new cash postion %why exp ? -> log return
%wealth(te,:) = cash(te,:) + risky(te,:);
end
%results
%area([cash,risky])
%plot(wealth)
%VT = wealth(end,:);
%rT = VT./V0-1; %result of overall performance
end
【问题讨论】:
标签: matlab loops for-loop if-statement finance