Я повторяю 250 раз через a для l oop, в этой итерации инвестиция вкладывается в рискованный актив и работает.
Если стоимость вложенных денег достигает определенного порога, мне нужно остановить их на l oop, чтобы я мог повторно инвестировать деньги из рискового актива в безрисковый актив.
С этого момента я хочу, чтобы l oop продолжал с оставшимся количеством шагов (до 250), но на этот раз значение нужно умножить на безрисковую ставку.
Мой сценарий останавливается до момента, когда будет достигнут порог, может ли кто-нибудь помочь мне здесь, как я могу продолжить итерацию?
Если вы ищете часть Treshold, где установлено предложение If ---> это в строке 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