MATLAB: How to calculate the highest consecutive negative results in the code

consecutive negative resultshighest

Hi,
I'm new to Matlab and I have been having help to write this code. I am trying to learn on my own and I am trying now to write something that would calculate the highest consecutive negative results in my code. I would be happy to recieve some help!
clear variables
k = 5000;
risk = 0.007;
riskM = 0.1;
maxR = 0.022;
numberSteps = 105;
markets = zeros(numberSteps,1);
markets(1) = k;
for i = 1:size(markets,1) - 1
markets(i+1) = markets(i)*(1+0.1);
end
% ka = 1100
%%
numberSim = 1000;
distR = randsrc(numberSteps, numberSim,[-1 -.6 -.36 -.07 0 0.17 0.37 0.39 0.5 0.81 1.37 1.39 1.4 2.2 2.57 3.16 3.55 5.49 6.56 7.34 7.82 8.63 8.7 9.15 9.42 11.55 17.94 19.9 21.18 22.28 28.4 28.51;...
.648 .0095 .0095 .0095 0.067 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095]);
cumR = cumsum(distR,1);
MaxD = maxdrawdown(cumR,'arithmetic');
[maxMaxD,ii] = max(MaxD);
meanD = mean(MaxD);
stdD = std(MaxD);
figure
subplot(1,2,1)
plot(cumR)
subplot(1,2,2)
plot(cumR(:,ii))
sgtitle(['Mean Drawdown=',num2str(meanD,3),' std=',num2str(stdD,3), ' Max Possible DD=',num2str(maxMaxD,3)])

Best Answer

This is trivial if you have the Image Processing Toolbox. Simply use regionprops():
v = [2 4 3 -1 -2 5 7 8 -1 -0.3 -1 -1 -1 -1 -1 -1 -1 7] % Sample data.
% Find elements where v is negative.
negativeValues = v < 0
% Measure area and mean of the negative regions.
props = regionprops(negativeValues, v, 'MeanIntensity', 'Area');
% Extract values from structure into arrays, for convenience.
allMeanIntensities = [props.MeanIntensity]
allAreas = [props.Area]
% Get the sum of all values in each region.
integratedValues = allMeanIntensities .* allAreas
% Find the sum that is MOST negative.
mostNegative = min(integratedValues)
Related Question