【问题标题】:Continuation of For Loop iterationFor循环迭代的继续
【发布时间】: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


    【解决方案1】:

    无需停止循环,您可以使用if-statement 和逻辑变量作为某种标志:

    FLAG_REINVEST = false;
    for i = 1:250 % loop through 250 iterations
    
        if value > Threshold
            % risk-free case
            if FLAG_REINVEST
                % do reinvestment
                FLAG_REINVEST = false; % set flag
            end
            % do the other stuff in the risk-free case
        else
            % risky asset
        end
    end
    

    【讨论】:

    • 你能用我的代码来实现吗?对我来说是行不通的,达到Treshold之后你应该拿钱去投资无风险的案例,有NaN
    • 对不起,你的代码又长又笨,我不会为你免费编码,但我会帮助你自己学习。如果您将代码分解为函数(例如investMoney-function),则将其放入我建议的结构中会更容易=)
    【解决方案2】:

    使用两个while循环:

    t = 1;
    while t < M-1
        t=t+1
        //invest
    
        if ... break
    end
    
    if t < M-1
        //re-invest
        ...
    end
    
    while t < M-1
        t=t+1
        //riskfree stuff
        ....
    end
    

    【讨论】:

    • 嘿!感谢您的快速回答......我对编程有点陌生,你能告诉我你将如何实现它吗?
    • 只需将您的代码复制粘贴到上面的结构中:在第一个 while 循环中,您放置了投资部分。在第二个循环中,您将无风险的东西放入您所要求的继续迭代,因为 t 没有改变
    • 它表示 (t++
    • 如果我把 t++ 改成 t+1 MATLAB 会崩溃
    • 这不是有效的 MATLAB 语法。
    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多