Portfolio Weighting Program

Ryan Sweeny
6 min readJan 2, 2023


I created a program to help mitigate portfolio risk of a stock portfolio, using the Markowitz Efficient Frontier, correlation, and VaR. This program will not remove all portfolio risk and will not mitigate any other type of risk but will help me weight the stocks I’ve selected, in a more methodical way. And, if there’s one thing I know, it’s that taking aim at a target is better than shooting from the hip, even if the sights on the rifle leave something to be desired.


Centrally, this entire program is based off of the Markowitz Efficient Frontier, which uses previous data to determine optimal current weights of components, in an equity portfolio. Applying an MEF model only helps investors mitigate portfolio risk and that’s all this program, broadly speaking, does. Portfolio risk is able to be viewed through many lenses so while the program begins by determining an optimal portfolio using the MEF, there’s a number of other tools that show it from different viewpoints. These views won’t necessarily mitigate risk but will paint a more complete/descriptive picture than a simple MEF and backtest alone.

Before we go any further, I just want to mention that if you put garbage into this program, the results will be trash. That is, if you want to fill your portfolio with a bunch of highly speculative stocks that lack any fundamentals, especially with where we’re at in the credit cycle, portfolio risk is the least of your concerns. I wrote this program to be the final step in my risk management pipeline, meaning that I only use it after spending hours and often days on due diligence when choosing each stock.


Data Entry

Punching in the requisite information is very strait forward. Users may input up to 10 stocks and decide what range they’d like each to be composed of in the portfolio. Delegating the optimal portfolio weights to be completely derived from the MEF model opens the door for allocation risk, because the model will assign the highest weights to components that previously had the best returns and may effectively remove other components that had a bad year. I chose to cap things at ten stocks because as a retail investor, without a big research team, there’s zero chance I’ll be taking more than ten positions. A benchmark is also required which I generally use SPY. Finally, the portfolio’s value needs to be entered in order for the program to calculate how many shares of each stock should be purchased.


Once the last 12 months of data for each of the stocks has been retrieved from IEX, 10,000 differently weighted portfolios are backtested to find the volatility and return for each (Which is how the MEF model works). Next, the portfolios are filtered to find which one satisfies the weigh ranges entered, while maximizing returns and minimizing volatility. If no portfolio exists, the weight tolerances are expanded beyond user inputs to get an approximation. Finally, the data displayed is calculated and graphs generated.


A picture’s worth a thousand words. The program’s pictures and information are divided into roughly five levels which are all reasonably self-expletory, so I’ll keep the commentary fairly brief.

Right at the top, we see how the suggested portfolio would have performed against the benchmark as well as how correlated the two are, the VaR of both, and the tracking error. I put very little faith in the backtest results because, obviously, they’re backward looking and 2023 may be very different from 2022. Still, it’s a fine enough jumping off point and if conditions this year are similar to last year, could provide some insight.

Next up we have the allocation weights and a graphic showing the MEF, where the ideal portfolio suggested by our model is placed, and where our filtered (restricted) model sits. Below these graphics sits a table with details on how many shares need to be purchased and what the company’s segments are. Of course, segments should have been determined during due diligence, but it never hurts to have a third party verify what you know. Finally, clicking the Heat Map button displays a graphic showing how correlated each stock is to one another in the portfolio.

VaR and tail movements sit further down the page. VaR is controversial and I’m not suggesting it’s a great metric to observe risk. However, I like taking a look at how the distribution of price moves fall. Between the two graphs on top, sits a box showing how many days fell outside the 2 standard deviation threshold, the average drawdown outside 2 STD and the maximum single-day drop. At the bottom of the section, users may scroll through each stock to see its distribution and how the tail graphs. The tail is important to me because I like to know how badly exposed to price shocks each stock is. That being said, black swans come along all the time so this information may or may not accurately depict the next big panic in a specific stock. Still, it’s good to know what ‘normal’ looks like if for no other reason than to measure the severity of future price moves more accurately.

Second to last is a stress-test for the portfolio. The Covid panic provides a great example for how bad things might get. Again, that’s not to say things couldn’t get worse, but on balance it’s better to have some reference of severity than nothing at all.

At the very bottom sits a table showing the 21-day Average True Range for each stock. I’m a big believer in setting stops based on ATR so I want to see what these stops will be. It’s also important to me that they mitigate most of the tail events, which is why I have them in percentage and not just dollars.


There’s no silver bullet when managing risk in financial markets. The very act of owning stocks opens investors up to a number of different types of risk. This program is designed to help mitigate portfolio risk by weighting components and by defining what normal and abnormal price movements are. It will not completely mitigate risk, but just like body armor for an Infantryman, helps mitigate some measure of it.

Because this is self-designed software, and because I’m currently spending most of my time applying for jobs after finishing my degree, there’s still some bugs and quarks that will undoubtedly be delt with in the coming weeks. As it stands though, this is a practical tool and functions very well. I’m excited to use it for my market operations in 2023.



Ryan Sweeny

Practitioner of speculation. Student of finance & economics.