Engineering 25 Prob 7-19 Solution (Stock Mkt Sim) Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engineering-25: Computational Methods 1 Bruce Mayer, PE ENGR-25_Prob_6-12_Solution.ppt The MATLAB Code % Bruce Mayer, PE % ENGR25 * 26Apr09 % Prob 7.19 * Prob7_19_Stock_Buy_Sell_Simulation_0904.m % clear for m = 1:100 % perform 100 annual trials BA = 0; % Set Bank Account to Zero initially S = 0; % Set No. Shares Held to Zero initially % Check Prices over 250 days in one year for k = 1:250 P = 5*randn + 100; % simulate the daily price with randn if P < 100 % BUY in this case BC = max(40, 50*0.06); % BrokerCommission to Buy 50 shares BA = BA - 50*P - BC; % Reduce BankAccount due to purchase S = S + 50; % Increase the number of Shares due to purchase elseif (P>105)&(S>0) % IF have any Shares, then Sell ALL if P exceeds $105 BC = max(40, S*0.06); % BrokerCommission to Sell ALL shares BA = BA + S*P - BC; % Net INCREASE in BankAccnt S = 0; % Sold ALL Shares else BA = BA; S = S; % NO Change if 100 < P < 105 end end YrEndSale = S*P; % Sell ALL Shares at Year End using Last Price BA = BA + YrEndSale; % This the final Bank Accnt $-value for Trial-m Profit(m) = BA; % Record the Profit for Each Trial in the Vector "Profit" end % Engineering-25: Computational Methods 2 Bruce Mayer, PE ENGR-25_Prob_6-12_Solution.ppt The MATLAB Code % % Calc stats for 100 trials Avg_Profit = mean(Profit) Max_Profit = max(Profit) Min_Profit = min(Profit) SD_Profit = std(Profit) % % Plot Yearly and 100-Trial Avg Profit for all trials on Bar Chart PlotProfit = [Profit, Avg_Profit]; barh(PlotProfit/1000), xlabel('Trial'), ylabel('Profit ($k)'), title('Prob 7.19'), grid Prob 7.19 120 100 Profit ($k) 80 60 40 20 0 0 10 20 30 Engineering-25: Computational Methods 3 40 50 Trial 60 70 80 Bruce Mayer, PE ENGR-25_Prob_6-12_Solution.ppt 90 The Results Avg_Profit = 6.4799e+004 Max_Profit = 8.5629e+004 Min_Profit = 5.1189e+004 SD_Profit = 6.1951e+003 Engineering-25: Computational Methods 4 Bruce Mayer, PE ENGR-25_Prob_6-12_Solution.ppt