-p 0 (plotting) -f orders/orders0.csv (ordersfile) -n -1 (order_id) -c 1000000 (start_value) -t 9.95 (transaction_fee) -m 0.005 (market_impact_factor) -l 0 (do_leverage) -s solution (do_leverage) --- begin student code ------------------------------------------------ 1) orders are (panda frame): Symbol Order Shares Date 2011-01-05 AAPL BUY 1500 2011-01-20 AAPL SELL 1500: 2) orders are (sorted): Symbol Order Shares Date 2011-01-05 AAPL BUY 1500 2011-01-20 AAPL SELL 1500 4) 'change' order (list) is: [('2011-01-05', 'AAPL', 1500), ('2011-01-20', 'AAPL', -1500)] 5) symbols to pull are: ['AAPL', 'AAPL'] 6) symbols (non duplicates) to pull are: ['AAPL'] 7) dates: DatetimeIndex(['2011-01-05', '2011-01-06', '2011-01-07', '2011-01-08', '2011-01-09', '2011-01-10', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', '2011-01-15', '2011-01-16', '2011-01-17', '2011-01-18', '2011-01-19', '2011-01-20'], dtype='datetime64[ns]', freq='D') 8) get adjusting closing prices of symbols date range : SPY AAPL 2011-01-05 123.83 332.57 2011-01-06 123.59 332.30 2011-01-07 123.35 334.68 2011-01-10 123.19 340.99 2011-01-11 123.63 340.18 2011-01-12 124.74 342.95 2011-01-13 124.54 344.20 2011-01-14 125.44 346.99 2011-01-18 125.65 339.19 2011-01-19 124.42 337.39 2011-01-20 124.26 331.26 9) extract SPY from prices : AAPL 2011-01-05 332.57 2011-01-06 332.30 2011-01-07 334.68 2011-01-10 340.99 2011-01-11 340.18 2011-01-12 342.95 2011-01-13 344.20 2011-01-14 346.99 2011-01-18 339.19 2011-01-19 337.39 2011-01-20 331.26 10) address the NaNs : AAPL 2011-01-05 332.57 2011-01-06 332.30 2011-01-07 334.68 2011-01-10 340.99 2011-01-11 340.18 2011-01-12 342.95 2011-01-13 344.20 2011-01-14 346.99 2011-01-18 339.19 2011-01-19 337.39 2011-01-20 331.26 11) add cash column (value of cash $1 is worth $1) to prices : AAPL _CASH 2011-01-05 332.57 1.0 2011-01-06 332.30 1.0 2011-01-07 334.68 1.0 2011-01-10 340.99 1.0 2011-01-11 340.18 1.0 2011-01-12 342.95 1.0 2011-01-13 344.20 1.0 2011-01-14 346.99 1.0 2011-01-18 339.19 1.0 2011-01-19 337.39 1.0 2011-01-20 331.26 1.0 12) create change frame with index and symbols : AAPL 2011-01-05 NaN 2011-01-06 NaN 2011-01-07 NaN 2011-01-10 NaN 2011-01-11 NaN 2011-01-12 NaN 2011-01-13 NaN 2011-01-14 NaN 2011-01-18 NaN 2011-01-19 NaN 2011-01-20 NaN 13) fill changes with zeros : AAPL 2011-01-05 0 2011-01-06 0 2011-01-07 0 2011-01-10 0 2011-01-11 0 2011-01-12 0 2011-01-13 0 2011-01-14 0 2011-01-18 0 2011-01-19 0 2011-01-20 0 14) A separae cash series index by prices with zeros: 2011-01-05 0.0 2011-01-06 0.0 2011-01-07 0.0 2011-01-10 0.0 2011-01-11 0.0 2011-01-12 0.0 2011-01-13 0.0 2011-01-14 0.0 2011-01-18 0.0 2011-01-19 0.0 2011-01-20 0.0 dtype: float64 15) first value in cash series is amount that we start out with: 2011-01-05 1000000.0 2011-01-06 0.0 2011-01-07 0.0 2011-01-10 0.0 2011-01-11 0.0 2011-01-12 0.0 2011-01-13 0.0 2011-01-14 0.0 2011-01-18 0.0 2011-01-19 0.0 2011-01-20 0.0 dtype: float64 16) iterate through sorted series of orders and execute the orders impacts our cash series, and the change frame 2011-01-05: BUYING 1500 AAPL Other costs +2,504.2250 price +498,855.0000 @ +332.5700 Total cash CHANGE is: -501,359.2250 Total cash available: +498,640.7750 2011-01-20: SELLING -1500 AAPL Other costs +2,494.4000 price -496,890.0000 @ +331.2600 Total cash CHANGE is: +494,395.6000 Total cash available: +993,036.3750 16) append cash to changes: AAPL _CASH 2011-01-05 1500 498640.775 2011-01-06 0 0.000 2011-01-07 0 0.000 2011-01-10 0 0.000 2011-01-11 0 0.000 2011-01-12 0 0.000 2011-01-13 0 0.000 2011-01-14 0 0.000 2011-01-18 0 0.000 2011-01-19 0 0.000 2011-01-20 -1500 494395.600 17) compute the holding everyday (same as) cumulative sum (cumsum): AAPL _CASH 2011-01-05 1500.0 498640.775 2011-01-06 1500.0 498640.775 2011-01-07 1500.0 498640.775 2011-01-10 1500.0 498640.775 2011-01-11 1500.0 498640.775 2011-01-12 1500.0 498640.775 2011-01-13 1500.0 498640.775 2011-01-14 1500.0 498640.775 2011-01-18 1500.0 498640.775 2011-01-19 1500.0 498640.775 2011-01-20 0.0 993036.375 18) compute the portfolue value (multiply * holdings): AAPL _CASH 2011-01-05 498855.0 498640.775 2011-01-06 498450.0 498640.775 2011-01-07 502020.0 498640.775 2011-01-10 511485.0 498640.775 2011-01-11 510270.0 498640.775 2011-01-12 514425.0 498640.775 2011-01-13 516300.0 498640.775 2011-01-14 520485.0 498640.775 2011-01-18 508785.0 498640.775 2011-01-19 506085.0 498640.775 2011-01-20 0.0 993036.375 19) sum the rows you got the value of portfolio!: 2011-01-05 997495.775 2011-01-06 997090.775 2011-01-07 1000660.775 2011-01-10 1010125.775 2011-01-11 1008910.775 2011-01-12 1013065.775 2011-01-13 1014940.775 2011-01-14 1019125.775 2011-01-18 1007425.775 2011-01-19 1004725.775 2011-01-20 993036.375 dtype: float64 --- end student code ------------------------------------------------ portfolio values: 2011-01-05 997495.775 2011-01-06 997090.775 2011-01-07 1000660.775 2011-01-10 1010125.775 2011-01-11 1008910.775 2011-01-12 1013065.775 2011-01-13 1014940.775 2011-01-14 1019125.775 2011-01-18 1007425.775 2011-01-19 1004725.775 2011-01-20 993036.375 dtype: float64 --- begin statistic ------------------------------------------------- Date Range: 2011-01-05 to 2011-01-20 (portfolio) Date Range: 2011-01-10 to 2011-12-20 (on web page) Number of Trading Days: 11 Sharpe Ratio of Fund: -1.00015025363 Average Daily Return of Fund: -0.00042721019 Final Portfolio Value: +993,036.37500000000 XX solution 0 11 -1.00015025363 -0.00042721019 +993,036.37500000000