필터 지우기
필터 지우기

For loops and referencing matricies

조회 수: 2 (최근 30일)
rb250660
rb250660 2016년 6월 18일
편집: Stephen23 2016년 6월 18일
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
Stephen23
Stephen23 2016년 6월 18일
편집: Stephen23 2016년 6월 18일
@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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Financial Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by