For循环迭代的继续

问题描述 投票:0回答:1

通过for循环迭代250次,在此迭代中,一项投资被投资到有风险的资产中并正在执行。

如果投资的货币价值达到某个阈值,我需要停止for循环,以便可以将其从风险资产中重新投资到无风险资产中。

[从那以后,我希望循环继续剩余的步数(直到250),但这一次,该值需要乘以无风险利率。

我的skript会停下来直到到达阈值的那一刻,有人可以在这里帮助我如何继续迭代?

如果正在寻找其中设置了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
1个回答
0
投票

使用两个while循环:

t = 1;
while t < M-1
    //invest

    if ... break
end

if t < M-1
    //re-invest
    ...
end

while t < M-1
    //riskfree stuff
    ....
end
© www.soinside.com 2019 - 2024. All rights reserved.