Using basic pandas to determine when to go in and out of an investment
Disclaimer: This is not a form of investment advice. Use this method at your own risk.
For this analysis, we hope to observe some form of the seasonal trend in a stock market and develop a simple strategy to take advantage of that trend. A basic level of Python (more precisely pandas) is used to manipulate the data. As this is supposedly a simple strategy, no statistical analysis or prediction techniques are used (these can and will be addressed in the future).
Here we are going to take a look at the Singapore stock market. A common barometer for the Singapore economy would be the Straits Times Index (STI), which tracks the performance of 30 stocks listed on the Singapore Exchange (SGX).
We can’t buy exactly one unit of the STI, but we can get into it. One method is to use an Exchange Traded Fund (ETF). In this case I am using the SPDR Straits Times Index ETF (ES3.SI). This ETF is intended to replicate the performance of the STI and is available for trading through various brokers.
Here are the adjusted closing prices of ES3 from January 1, 2008 to January 1, 2018:
Adjusted closing price data is used to omit the impact of corporate actions like dividends, stock splits, etc. and is a more accurate representation of stock price movements.
We assume that there are certain months that tend to produce better returns than others. We calculate the arithmetic average of the logarithmic returns of the different months (aggregated over the period).
For example, if we look at January, we look at the daily returns from January 2008, January 2009 … January 2018 and calculate the arithmetic mean over these values.
This is what the daily log returns from ES3 look like:
One of the reasons for using log returns is the common assumption that stock returns follow a log normal distribution.
The following is a summary of the arithmetic mean of the daily log returns for each month from January 1, 2008 to January 1, 2018 (1 corresponds to January and 12 corresponds to December):
Name: Daily Returns, dtype: float64
It is observed that these are the months that achieved an average daily positive return during that period:
Therefore, we will buy the maximum amount of ES3 in early March and sell all of our inventory in early June for each year (this also applies to the other months in the list).
In the next sections we define two portfolios:
- The Standard Portfolio: A buy-and-hold portfolio where the full amount of capital is used to purchase the maximum number of ES3 shares at the beginning of the reporting period
- The strategic portfolio: A portfolio that buys the maximum number of ES3 shares on the first trading day of a “good month” and is eliminated on the first trading day of a “bad month”.
The simulations were carried out with a seed capital of 10,000 SGD. Some assumptions include:
- No transaction costs (bid-ask spreads, commissions, etc.)
- Fractional shares are permitted
A simulation from January 1, 2008 to January 1, 2018 resulted in the following portfolio values:
Based on the table, the standard portfolio produced an annualized return of -1.8%, while the strategic portfolio produced an annualized return of 10.1%. A visualization of the portfolio values is shown below.
Of course, we would expect the strategic portfolio to perform better as the “good months” and the “bad months” were determined based on the same data.
Therefore, we will apply our observations to the period from January 1, 2018 to May 1, 2021. Note that all observations made so far were independent of this period.
The standard portfolio produced an annualized return of -3.5%, while the strategic portfolio produced an annualized return of -3.0%.
Well, the simulation results show that the strategic portfolio only marginally outperformed the standard portfolio.
It was found that in both cases the strategic portfolio performed better than the standard portfolio. Please note, however, that in the test case, depending on your investment capital, the improvement over the standard portfolio can be offset by transaction costs.
The strategic portfolio did well for most of 2018 but was unable to avoid the negative effects of COVID-19 and missed the strong recovery at the end of 2020. The strong recovery allowed the standard portfolio to close much of the gap.
- Easy to implement with simple Python and doesn’t require complex analysis (it was just done with a simple df.groupby Surgery)
- A maximum of 12 transactions per year, which is pretty reasonable
- For other stocks, a strategy can be implemented that may better fit the pattern
- Removes emotions from the investment process and avoids hasty decisions
- Getting in and out of the market would mean avoiding potentially large losses due to sudden adverse events (e.g. COVID-19 outbreak).
- A very naive strategy with results that are more likely due to luck than statistical evidence
- Discretizes time periods into exact months, where time periods (if such patterns exist) can range from any number of days
- Sudden profits may be missing because they are out of the market
- Assume that economic conditions within a year will be similar for years but may not be due to factors such as technology, regulations, guidelines, etc.
Just because the strategy is simple doesn’t mean it is useless. A few changes could make the model more profitable. Here are a few ideas:
- Multi-Regime Model: If returns were observed to be mostly seasonal, we could have a model that toggles between one seasonal strategy and another for abnormal changes (which could be determined using statistical testing or even machine learning).
- Irregular Periods Model: Seasonal returns don’t necessarily need to be discretized by months. This can be 15 days, 93 days or even periods that are not of constant length within the year
- Sector rotation model: Seasonal trends differ by industry. This strategy offers an alternative to “resting” your money during certain months of the year
I believe returns are seasonal (as are business cycles). There are various events throughout the year that can contribute to these cycles, such as: B. earnings data, budget announcements or even celebrations. These events affect certain industries (and companies) more than others. For this reason there are hypotheses like the January effect.
The January Effect is a perceived seasonal rise in stock prices due to reasons such as harvesting tax losses (selling assets at a loss to offset capital gains tax, does not apply to all countries) and even investor psychology.
If we were to determine the existence of such patterns, it would likely hold for a handful of stocks. However, this would be a good start to building a stronger strategy.