Clear Filters
Clear Filters

For loops and referencing matricies

2 views (last 30 days)
rb250660
rb250660 on 18 Jun 2016
Edited: Stephen23 on 18 Jun 2016
Hi all,
I am having trouble with the for loop that generates the "MC_Equity" matrix in the code below. I am obviously not referencing the correct corresponding "MC_Equity_chg" matrix values to perform the desired mathematical operations.
I have checked the "MC_Equity_chg" matrix in Excel and it is good. I have checked the "MC_Equity" matrix in Excel and its output is bad (the first column is good).
Background: The code is supposed to generate random equity curves for stock trading analysis. I have simulated trade returns with random numbers in the "data" matrix. The strawbroom plot shows my equity curves closely following each other and finishing at the same equity which is wrong.
Any help appreciated.
% SIMULATION
% Simulation - Initial equity, number of positions, duration and samples
MC_Runs = 100;
MC_Equity_init = 100000;
MC_NumPos = 8;
MC_Trades = 50;
% Population - Describe population trades
data = ( rand( 184, 1 ) - 0.5 ) / 100;
data_chg = [0; data ./ MC_NumPos]; % apply position sizing to population
MC_data_chg = rot90( data ./ MC_NumPos );
% MONTE CARLO - RANDOMIZE TRADE SEQUENCE
% Equity curves
MC_Equity_chg = zeros( MC_Runs, MC_Trades + 1 );
for j = 1:MC_Runs;
MC_Equity_chg(j,:) = [0, datasample( MC_data_chg, MC_Trades, 'Replace', true )];
end
MC_Equity_chg = flipud( rot90( MC_Equity_chg ) );
MC_Equity = [MC_Equity_init * ones( 1, MC_Runs ); zeros( MC_Trades, MC_Runs ) ];
for j = 2:MC_Trades + 1;
MC_Equity(j,:) = MC_Equity(j-1) + MC_Equity(j-1) * MC_Equity_chg(j,:);
end
% Monte Carlo - Equity curves
figure
hold on;
plot( MC_Equity(:,1), 'r-' );
plot( MC_Equity(:,3), 'k-' );
plot( MC_Equity(:,5), 'g-' );
plot( MC_Equity(:,7), 'b-' );
  1 Comment
Stephen23
Stephen23 on 18 Jun 2016
Edited: Stephen23 on 18 Jun 2016
@Ross Brennan: please tell us what you mean by "its output is bad (the first column is good)". We don't know what you want your code to do, so we cannot judge what is a "good" or "bad" result.
Note that this code requires the Statistics Toolbox, because of the datasample function.

Sign in to comment.

Answers (0)

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!