Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In [2]: import mpt
- In [3]: p = mpt.Portfolio(symbols=["AAPL", "GT", "MMM", "GNW", "KO"], startdate="2005-1-1", enddate="2008-8-23", dbfilename="data/stocks.db")
- Adding: AAPL
- Adding: GNW
- Adding: GT
- Adding: KO
- Adding: MMM
- In [4]: rt = 2.0 # Risk tolerance, equivalent to a 200% annual volatility
- In [5]: drt = rt/252.0 # Convert to a daily volatility using trading days per year
- In [6]: lb = 0.0 # lower bound of allowed weighting for an asset
- In [7]: ub = 1.0 # upper bound of allowed weighting for an asset
- In [8]: p.optimize_portfolio(rt=drt, lower_bound_weight=lb, upper_bound_weight=ub) Optimization completed in [ 5 ] iterations.
- Ending weights:
- {'MMM': 0.0, 'AAPL': 1.0, 'GT': 0.0, 'KO': 0.0, 'GNW': 0.0}
- Optimized Variance: 0.155 and Portfolio Return: 54.093%
- In [9]: rt = 0.5 # Risk tolerance, equivalent to a 50% annual volatility
- In [10]: drt = rt/252.0 # Convert to a daily volatility using trading days per year
- In [11]: del p.port_opt # Clear out the cached optimized portfolio
- In [12]: p.optimize_portfolio(rt=drt, lower_bound_weight=lb, upper_bound_weight=ub)
- Optimization completed in [ 5 ] iterations.
- Ending weights:
- {'MMM': 0.0, 'AAPL': 0.79253926811721775, 'GT': 0.0, 'KO': 0.20746073188278225, 'GNW': 0.0}
- Optimized Variance: 0.103 and Portfolio Return: 44.936%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement