Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a Matlab script Create a random plot that looks like a stock price chart:

ID: 3785328 • Letter: W

Question

write a Matlab script

Create a random plot that looks like a stock price chart:

a·previous value + b

where the random coecient a is uniformly distributed in some interval [a1,a2] and b is uniformly distributed in some interval [b1,b2]. Suggestions:

• Begin the script with a1 = ...;a2 = ...;, etc, so that you can easily tweak these values.

• The “price” data should be a row vector of length n where n is about 1000.

• Create random vectors a and b of length n • Set initial price value to 100.

• Using a for loop, calculate subsequent values. • Plot the result.

• Revise the parameters a1,a2,b1,b2 as necessary.

In your answer, point out three sets of parameters a1,a2,b1,b2
(a) Those that produce your best imitation of a stock price chart

(b) Those that result in price dropping to near zero.

(c) Those that result in price growing extremely large.

Explanation / Answer

% 1 dates 1xN No
% 2 High prices 1xN No
% 3 Low prices 1xN No
% 4 Open prices 1xN No
% 5 Close prices 1xN No
% 6 Volume 1xN Yes
% 7 Moving Average lag 1 1xN Yes
% 8 Moving Average lag 2 1xN Yes
%
% If no parameters are provided, random data
% will be used
%
% For educational purposes only
%
% IT'S NOT FANCY BUT IT WORKS


if nargin > 5
    DOVO = 1;
else
    DOVO = 0;
end;
if nargin > 7
    DOMA = 1;
else
    DOMA = 0;
end;
if nargin == 0
    DOVO = 1;
    DOMA = 1;
    MA1=30;
    MA2=60;

    % CREATE VERY NAIVE RANDOM DATA
    % I'm sure there's a vectorized solution,
    % but I'm lazy
    t = today-99:today;
    Lo = 99;
    Hi = 101;
    Op = 100;
    Cl = 101;
    Range = Hi - Lo;
    for i=2:100
        Mid = Range./2+Lo(i-1);
        Lo(i) = Mid-5.*rand(1)-2.5;
        Hi(i) = Mid+5.*rand(1)+2.5;
        Range = Hi(i) - Lo(i);
        Op(i) = Range.*rand(1)+Lo(i);
        Cl(i) = Range.*rand(1)+Lo(i);
    end;
    Vo = 1000.*rand(1,100);
end;

% PLOT HI-LO
figure;
if DOVO subplot(3,3,1:6); end;
h=line([t(:) t(:)].',[Lo(:) Hi(:)].');
set(h,'color','k');
datetick;

% PLOT OPEN
h=line([t(:)-0.5 t(:)].',[Op(:) Op(:)].');
set(h,'color','k');

% PLOT CLOSE
h=line([t(:) t(:)+0.5].',[Cl(:) Cl(:)].');
set(h,'color','k');

% MOVING AVERAGES
% I'm sure there's a vectorized solution,
% but I'm lazy. I think you can use FILTER
% instead of the loops.
if DOMA
    title(sprintf( ...
        'Price with Moving Average [%d,%d]', ...
        MA1,MA2));
    for j=1:2
        if j==1
            MA=MA1;
            c = 'b';
        else
            MA=MA2;
            c = 'r';
        end;
        MAval = [];
        for i=length(t):-1:MA
            MAval = [MAval mean(Cl(i:-1:i-MA+1))];
        end;
        hold on;
        plot(t(end:-1:MA),MAval,c);
    end;
else
    title('Price');
end;

% VOL
if DOVO
    subplot(3,3,7:9);
    bar(t,Vo);
    datetick;
    title('Volume');
end;