ccFinance – Stock Portfolio Analysis in C# (Part 1)

I have been a casual “portfolio manager” for a number of years now. What this really means, is that  I have some money that I personally actively, yet casually, manage and by doing so also reduce my life expectancy by pegging my blood pressure to market volatility. After getting an MBA a few years back, my knowledge base pertaining to securities investments reached “enough to be dangerous” level. However, one positive thing that came out of my education was a better grasp of portfolio metrics and also a realization that portfolio management tools provided by discount brokers fall far short of what would be required to make educated decisions on portfolio risk, asset allocation and more importantly information on how these metrics change over time. Thus, I set out to write some number crunching algorithms of my own in order to better understand how my money is actually invested.

This is what I hope to be a first in a series of articles (depending on the feedback I receive) on performing financial analysis in C#. I plan to cover a few things, namely:

  • Calculating basic portfolio statistics
  • Mining stock data from various online resources
  • Calculating sector sensitivity
  • Monitoring changes in portfolio sensitivity and risk profiles
  • and maybe down the road, I’ll talk about some goofy stuff I’m doing with neural network analysis pertaining to re-allocation decisions

Please understand, that all of this is provided without warranty of any kind and is not meant to be an investment advice. For advice on investment decisions you should most definitely seek help from a licensed professional.

Basics of Portfolio Finance

Ok, so I am going to skip the very basics like calculating gain or loss on a single security or a portfolio. Of course, this functionality will be included in the code I’ll be sharing but I will not go into lengthy discussion of those calculation unless there are enough requests to do so. Besides, these are the types of stats that ETrade, Scottrade, and even Yahoo and Google will provide for you.

Risk vs. Return vs. the Market

Hopefully, I am not going to surprise anyone by saying that the most important factor to consider when investing money is the relationship between risk and return. If you only consider the risk, then the only place you would be investing is a bank account (and no more than $100k). If you only consider return, you may find yourself undergoing plastic surgery in order to escape the guys with funny accents that are after you (don’t ask me how I know..). So really, it’s the relationship between the two that is relevant.

Investing in the stock market is an intrinsically risky business – just ask anyone who started doing it in 2007-early 2008 period. Theory says it that the market encompasses two types of risk:

  • Systematic risk – this is the general risk associated with the stock market, which includes things like risk of war, risk of the dollar becoming worthless, recessions, etc. Everyone who invests in the market is subject to it (unless you have a very balanced long and short position, in which case you need to have your head examined). 
  • Non-systematic risk – this is a risk associated with investing in specific companies. If you invested in Research in Motion yesterday, you may be biting your elbows today because they revised their earnings forecast and the stock dropped 20% despite the fact that S&P average as a whole is in the positive territory. This is an example of non-systematic risk associated with investing in RIMM. Other examples of sources of non-systematic risk include lawsuits, drug breakthroughs (risk is not necessarily a bad thing), health of executives (think Steve Jobs stepping down), etc.

The nice thing is that while you cannot escape systematic risk, non-systematic risk can be virtually eliminated through portfolio diversification. This is due to the fact that, at any given time, one company in your portfolio is being sued, while another may have discovered a cure for cancer and all-in-all they balance each other out. What constitutes a diversified portfolio is up for debate, of course, and there are entire college courses on the matter so I will not go there. These days, ETFs (exchange traded funds) allow you to diversify with buying just a singe security.. sorta. SPY, for example, is an ETF that tracks the daily return of the S&P500 index. Thus, purchasing SPY is loosely equivalent to a weighted purchase of the stock of every company in S&P500 index.. It’s close, but it’s not perfect

SP500 vs SPY

The point is that if you buy SPY, the only risk you are assuming is the overall market risk (systematic risk). Since SPY has no products, cannot be sued and cannot discover anything awesome, there is no non-systematic risk whatsoever.

For the purposes of standardization, economists defined systematic risk as the risk associated with investing into S&P500. For the purposes of quantitative standardization, they also said that this risk equals to 1.

What the hell does that mean? Nothing really – it is just a value used to compare the risks associated with other investment to the overall market risk.

For example, if yesterday S&P500 dropped by 3% but ABC Widget Company dropped by 6%, we say that the risk of investing in ABC Widget company is equal to twice the market risk (i.e.equals 2, since we defined market risk=1). Of course, this relationship would have to exist on a day -to-day basis in order for us to make a general conclusion…

In finance, risk is denoted by Greek letter beta (β) so in reality we say:

  • S&P500 has β=1
  • ABC Widget Company has  β=2
Now I will rapidly clarify / obfuscate what i’ve said up till now:
  • Risk is not just about losing money! Just like ABC Widget Company dropped by 6% when the S&P500 dropped by 3%, we would expect ABC Widget Company to increase by 6% if the S&P500 increased by 3%. The greater the risk, the greater the volatility of return. 
  • Can a company have β<1? Sure! Think toilet paper – if a recession hits, people may stop buying iPads, but they won’t stop pooping.. So investing into a toilet paper sector is probably safer than the market as a whole. 
  • Can the risk (β) assocaited with investing into a company change? You bet! Which is why computing β is not a straight forward endeavour. The time period for which β is calculated can have a huge impact. In the example above, ABC Widget Comapny may have had an awful year in 2008 (due to non-systematic risk!) and if we were to look at the return relative to S&P500 for 5 years, we could arrive at a different figure. In this article series, I’ll cover computing β for various time periods, including a moving average β. I will talk about this in more detail, but essentially this has to do with evaluating how your risk profile changes over time.

I have to run for today, but in the next part I’ll cover portfolio β, which is the risk of the entire portfolio, not just a single stock and actually show some code used to compute risk.

Before I go though, let me show you another graph. SSO is a ETF (exchange-traded fund) that “seeks daily investment results that correspond to twice (200%) the daily performance of the S&P 500 Index”. Can you guess what is the β associated with SSO?