Engineering 25 Prob 7.18 Solution Stock Market Simulation: Buy? Sell? Hold? Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Scenario Behavior of Certain Stock • Price, P, is randomly set by NORMAL distribution – µ = $100 – σ = $5 • Simulate Stock behavior over 250 days (1 trading year) Trading Algorithm • If P<100, → BUY 50 Shares • If P>105 → SELL ALL shares • If 100< P < 105 → HOLD • Add to Profit the Value of Stock HELD at END of Year Engineering/Math/Physics 25: Computational Methods 2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Key to Problem The Critical Issue required to get close to the BOOK answer is to realize: That at YEAR’S END we must Assess the Value of Any Shares NOT SOLD • We will Very Likely (84.13% chance) have shares “Left Over” if P250 < $105 Add the Held-Over Value to the previous selling profit Engineering/Math/Physics 25: Computational Methods 3 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt MATLAB Code: Method-1 % Bruce Mayer, PE % ENGR25 * 24Mar12 % StockMarket Timing Algorithm % file = P7_18_Stock_Profit_Method1_1203.m % P = 5*randn(1,250) + 100; % stock price; a random no. % % Initalize changing variable Profit = 0; n = 0; % no. shares owned s = 0; % days to SELL b = 0; % days to BUY na = 0; % days of No Activity SH = 0; % Shares Held Vector lenP = length(P) for k = 1:length(P) if P(k) <100 b = b+1; % buy days BrokerFee = max(40, 50*.06); Profit = Profit - 50*P(k) - BrokerFee; n = n+50; SH(k) = n; elseif P(k)>105 s = s+1; % sell days BrokerFee = max(40, n*.06); Profit = Profit + n*P(k) - BrokerFee; n = 0; SH(k) = n; else n = n; Profit = Profit; na = na+1; % no activity days SH(k) = n; end end disp(' ') disp('===== Stock purchase Profit summary ======') % SellDays = s BuyDays = b HoldDays = na NoSharesLeftInPortfolio = n YearEndPrice = P(250) % Current Values shares leftover in Portfolio at Year's end YearEndValue = n*P(250) DailyProfit = Profit % Add the year end "lump sum" value to previous day-to-day profit TotalProfit = DailyProfit + YearEndValue % Make frequency Plot Engineering/Math/Physics 25: Computational Methods 4 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Method-1 Results (1-Run) =Stock purchase Profit summary = SellDays = 38 BuyDays = 133 HoldDays = 79 NoSharesLeftInPortfolio = 50 YearEndPrice = 98.9097 YearEndValue = 4.9455e+003 DailyProfit = 6.4715e+004 TotalProfit = 6.9660e+004 Engineering/Math/Physics 25: Computational Methods 5 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Method-1 Day-by-Day 35 30 No. Days 25 20 15 10 5 0 85 90 95 100 stock price 105 110 115 700 Shares Held 600 500 400 300 200 100 0 0 50 100 150 trading day Engineering/Math/Physics 25: Computational Methods 6 200 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt 250 Method-1 Single Run 35 30 No. Days 25 20 15 10 5 0 80 85 90 95 100 stock price 105 110 115 800 700 Shares Held 600 500 400 300 200 100 0 0 50 100 150 trading day Engineering/Math/Physics 25: Computational Methods 7 200 250 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt 300 MATLAB Code: Method-2 % Bruce Mayer, PE % ENGR25 * 25Mar12 % StockMarket Timing Algorithm % P7_18_Stock_Profit_Method2_1203.m % METHOD-2 % clear % m = 100; % central Location s = 5; % Std Dev n = 499; % number of runs % for p = 1:n price = s*randn(1,250) + m; % generate 250 random prices in a vector % % initialize accumulation vectors shares_held = 0; income_selling = zeros(1,250); cost_new_shares = zeros(1,250); commission = zeros(1,250); for k = 1:250 if price(k) < 100 % BUY Shares in this case shares_held = shares_held + 50; commission(k) = max([40, .06*50]); cost_new_shares(k) = 50*price(k) + commission(k); shares(k)=shares_held; end if (price(k) > 105)&(shares_held > 0) % SELL Shares commission(k) = max([40, .06*shares_held]); income_selling(k) = shares_held*price(k) - commission(k); shares_held = 0; shares(k)=shares_held; end end cost(p) = sum(cost_new_shares); income(p) = sum(income_selling); profit(p) = income(p) - cost(p) + price(250)*shares_held; total_commission(p) = sum(commission); shares_held_at_year_end(p) = shares_held; YrEndPrice(p) = price(250); end disp('############################')% to break up runs mean_yearly_profit = mean(profit) min_yearly_profit = min(profit) max_yearly_profit = max(profit) std_profit = std(profit) MEAN_YrEnd_Price = mean(YrEndPrice) AVG_YrEnd_Shares = mean(shares_held_at_year_end) Engineering/Math/Physics 25: Computational Methods 8 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Method-2 Results mean_yearly_profit = 6.5054e+004 min_yearly_profit = 4.9024e+004 max_yearly_profit = 9.0948e+004 std_profit = 5.7442e+003 MEAN_YrEnd_Price = 99.8867 AVG_YrEnd_Shares = 154.4088 MIN_YrEnd_Shares = 0 MAX_YrEnd_Shares = 1250 Engineering/Math/Physics 25: Computational Methods 9 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Method-2 Plots 4 10 x 10 8 profit 6 4 2 0 0 50 100 150 200 250 300 Run No. 350 400 450 500 0 50 100 150 200 250 300 Run No. 350 400 450 500 1400 YearEnd Shares 1200 1000 800 600 400 200 0 Engineering/Math/Physics 25: Computational Methods 10 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt MATLAB Code: Method-1 →MultiRun % Bruce Mayer, PE % ENGR25 * 03Nov11 * Rev 26Mar12 % StockMarket Timing Algorithm % file = P7_18_Stock_Pr1ce_Simulation_1111.m % METHOD-1 clear % run almost 500 Simulations for m = 1:499 P = 5*randn(1,250) + 100; % stock price; a random no. % Initalize changing variable Profit = 0; n = 0; % no. shares owned s = 0; % days to SELL b = 0; % days to BUY na = 0; % days of No Activity (HOLD shares) SH = 0; % Shares Held Vector % lenP = length(P) => used for DeBugging Only for k = 1:length(P) if P(k) <100 b = b+1; % buy days BrokerFee = max(40, 50*.06); Profit = Profit - 50*P(k) - BrokerFee; n = n+50; SH(k) = n; elseif P(k)>105 s = s+1; % sell days BrokerFee = max(40, n*.06); Profit = Profit + n*P(k) - BrokerFee; n = 0; SH(k) = 0; else na = na+1; % no activity days; HOLD shares SH(k) = n; end end disp(' ') % have some Shares Left in portfolio. Sell ALL these at year's end EndIncome = P(250)*n; % % Build Vectors using m-counter TotalProfit = Profit + EndIncome; ProfitByRun(m) = TotalProfit; SharesLeftInPortfolio(m) = SH(length(P)); SellDays(m) = s; BuyDays(m) = b; NoActivityDays(m) = na; EndSharesByRun(m) = n % Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 11 BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Method-1: 499 Runs = Stock purchase Profit summary = MAXProfit = 8.5555e+004 MINProfit = 4.6147e+004 AvgProfit = 6.4593e+004 StdPprofit = 5.7947e+003 AvgEndShares = 168.0361 Engineering/Math/Physics 25: Computational Methods 12 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt Method-1: 499 Runs 4 10 x 10 8 profit 6 4 2 0 0 50 100 150 200 250 300 Run No. 350 400 450 500 0 50 100 150 200 250 300 Run No. 350 400 450 500 1200 YearEnd Shares 1000 800 600 400 200 0 Engineering/Math/Physics 25: Computational Methods 13 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_HW-01_Solution.ppt