I iterate 250 times through a for loop, in this iteration an investment is invested into a risky asset and is performing.
If the value of the invested money reaches a certain treshold I need to stop the for loop, so that I can re-invest the money out of the risky asset into the riskless asset.
From there on I want the loop to continue with the amount of steps which are left (until 250) but this time the value needs to be multiplied with the riskfree rate.
My skript stops until the moment where the treshold is reached, can anyone help me out here how i can continue the iteration?
If you are looking for the Treshold part where the If clause is set--- > its in line 65 and line 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
Aucun commentaire:
Enregistrer un commentaire