Investing with code
This course aims to provide a thorough introduction into the principles of investing based on interactive Python code examples. If you are interested in investing and programming (regardless of the languages you already know), this course will teach you basic as well as advanced methods used in quantitative finance and show you how to implement them efficiently in Python. We believe that programming offers a very approachable way to math-heavy subjects, as code snippets can be iteratively tested and fiddled with to build an intuitive understanding of the inner workings of even very complex mathematical models.
This first part of the course will introduce the absolute basics of any investment strategy, including:
- how to calculate profit and loss
- how to estimate the risk of investing in different assets
- how to account for the costs of frequent trading
More importantly, we will discuss certain problems that investors regularly face, and by that motivate the use of a number of quantitative methods that help to build solutions which are specifically tailored to one's own financial requirements. Some highlighted problems are:
- If stocks only go up in the longterm, why can't we simply invest in a leveraged stock index ETF and become rich? Hint: Because in general, you cannot predict the price of your investment when you finally need to withdraw money from your investment, and this uncertainty forces you to be risk-averse.
- Why can't we simply invest in a mix of stocks and government bonds? If one goes down, the other goes up, thereby hedging each other's risk. Hint: Because in many cases, relationships between different assets may significantly change over longer times. After the pandemic, stocks and government bonds have been moving in the same direction for quite some time, rather than the opposite. This change in correlation greatly increased the risk (and losses) of such popular mixed portfolios.
- A curious fact about the S&P500 stock index is that mostly gains value over night, when the market is closed. So why can't we simply buy the index at market close in the evening and sell it again in the morning to get rich? Hint: Because transaction costs and the bid/ask price spread will eat away the profits very quickly. To test active strategies, one needs to properly account for such costs.
As we will see in the upcoming sections, these exemplary problems will guide us from simple calculations to advanced statistical methods that help us as investors to keep track of changing market conditions and to protect investments from hidden risk.