PortfolioOptimization2.0: No Microsoft!

PortfolioOptimization2.0: No Microsoft!

Although I was impressed with the previous version of my portfolio optimization module, it wasn’t completely automated. It retrieved and organized the meat of the process to create the correlation matrix, but left more to be desired by an end-user who would require Microsoft Office, familiarity with Excel, and an additional solver add-in to calculate the final weighted optimization. Also, implementing the desired portfolio assets into the Python module would likely be awkward or tedious for someone unfamiliar with the language and they may accidentally alter the code.


I have now improved the module’s usability, automation, and utility. Most importantly, the module now completely calculates the optimized asset weights of the desired portfolio rather than simply producing the correlation matrix required for the calculation in the previous version. This is important because it means that not one click or input is required by the user outside of the initial entry of the desired portfolio equities. This is also important because it allows a user to calculate an optimized portfolio for absolutely zero cost, entirely on an opensource language, without Microsoft products, and without a data provider by utilizing the Investor Exchange’s public application programming interface. Hence, it’s pretty fast. A portfolio of about 15 equities would only take around a minute or two to optimize.


Additionally, mainly just for fun, but also for some enhanced usability, the module now features a crude graphical user interface for portfolio implementation. The module is initiated with this GUI. It includes two text-boxes: one interactive box for entering tickers and one for displaying the updated portfolio. It also includes two buttons: one for submitting the typed ticker and one for deleting a ticker. The GUI protects the user against mistakes by referencing IEX’s equity information json (possibles = (pd.read_json('https://api.iextrading.com/1.0/ref-data/symbols').loc[:, ("symbol")]).tolist()) and notifying the user with an error if the attempted submission does not exist within the database. It also capitalizes any submitted string, since all tickers are capitalized and the user likely just accidentally forgot to capitalize their submission. It’s also quicker and easier to type in lowercase. When the user is done creating their desired portfolio, they simply exit the GUI like any other computer window and the module commences.


After the correlation matrix is calculated by the module, the objective of either minimizing portfolio variance or minimizing the negative Sharpe ratio of the portfolio (maximizing) is calculated by a trial of Jacobian matrices, subject to the constraint that the sum of the weights is equal to one. Then, the descriptive dictionary of the optimized portfolio is printed in the shell like so: (Optimized Asset Weights: {'IBM': 0.33788151037311387, 'MMM': 0.32028568367454596, 'GOOG': 0.3418328059523402} ). I'm having fun learning how much can be independently done with a few hundred lines of code in an opensource language. Python constantly exercises my logic and strains my focus.

To view or add a comment, sign in

More articles by John Hupperts

Others also viewed

Explore content categories