Exploring Stock Market Seasonality Trends with Python

Using Python to explore seasonal effects on stock market and it’s different components

Amir Nejad
QuantJam
Published in
8 min readSep 16, 2018

--

Photo by Balazs Busznyak on Unsplash

Code

Script to create this study is stored at my Github Page.

Fama (1970) introduced efficient market hypothesis (EMH), stating the prices of securities fully reflect available information. Therefore, investors buying securities in an efficient market should expect to obtain an equilibrium rate of return. Later he introduced three different form of market efficiency: weak, semi-strong, and strong. Anomalies are empirical results that seem to be inconsistent with maintained theories of asset-pricing behavior. One class of market anomalies is seasonality and changes in market trend during different seasons. Seasonality in stock returns is a closely related to week-form of market efficiency. There are multiple seasonality effects (aka calendar effects):

  • Weekend effect
  • Monthly effect
  • Holidays effect

Here, we attempt to explore monthly change in stock market returns. The following table demonstrates market return at each month for the past 300 plus years!(Source: SeekingAlpha)

Dow Jones Industrial Average returns with respect to changes in months.

First, we are going to look at SPY calendar anomaly and observe possible trend changes in every month. Data available for this analysis starts from 2012 so we have 16 years of history to study. We are going to calculate monthly return of each year and plot with box plot.

Codes are provided in the Appendix.

Average monthly returns of S&P 500 ETF (SPY) for period of 2002–2018

Observations:

  • Overall market return trend exhibits a wave like pattern: market returns seem to increase from January to April followed by decrease in average returns in May and June. August to December exhibits steady increase in market returns.
  • Highest range of return (high to low range of box plot) in the past 16+ years occured in January, July, and August.
  • October demonstrates low donwside trade probability as low part of the box plot is closely situated outside the first quantile of the data. Therefore, it might provide good opportunity for investor to get in market or add to their investments. However, proper backtesting is needed to prove this hypothesis.
  • Months of June and December provide the smallest quarterfinal range (IQR). Coincidentally, the aforementioned months are half year and end of the trading yea

Now we will utilize the Augmented Dickey-Fuller Test for stationarity. The null hypothesis states that large p values indicate non-stationarity and smaller p values indicate stationarity. (We will be using 0.05 as selected alpha value.)

ADF Results indicates non stationary time series

As suspected, ADF test revealed the time series data (SPY stock data) are non stationary meaning time series is a stochastic process whose unconditional joint probability distribution changes when shifted in time. Now we know our time series is non staitionary, we attempt to exploit seasonality trends using time series decomposition.

We shall think of the time series Yt as comprising three components: a seasonal component, a trend-cycle component (containing both trend and cycle), and a remainder component (containing anything else in the time series). For example, if we assume an additive model, then we can write:
Yt=St+Tt+Et
where Yt is the data at period “t”, St is the seasonal component at period t, Tt is the trend-cycle component at period t and Et is the remainder (or irregular or error) component at period t. Alternatively, a multiplicative model would be written as
Yt=St*Tt*Et

For more detailed information visit (https://www.otexts.org/fpp/6/1)

In this notebook I am using additive decomposition model to extract seasonal model. In order to calculate seasonal effect, I used frequency decomposition of 252 meaning, trend is repeated every 252 days (5 trading days and 9 holidays results in 252 trading day).

Time series decomposition of SPY

As it can be seen from the above chart, overall trend of the market was increasing with the downward trend in 2008–2009. The seasonal trend repeating every year indicate valley and peak. Now, we attempt to isolate this trend and inspect it further:

Extracted seasonality trend of SPY which repeats every year (only 2017 is shown here)

Observations:

  • As it can be seen from the above seasonality chart, the seasonality trend is like a wave with major high happening around May and major lows happening around October.
  • Market expands from beginning of the year to Spring, then it follows by a period of contraction to mid fall.
  • The seasonality trend reveals change in expectation of market performance during the year.

Now we isolated seasonality trend we can, adjust market trend in regard to seasonality. Since we used additive model to identify seasonality, we can simply deduct seasonality from stock data (SPY) and adjust for seasonality. In periods where market has strong bull or bear (upward or downward) trends, seasonality effects might be too weak to observe. However, if market exhibits range bound behavior it, such effect can be more evident. Here, we will look at 2017 data (strong bull market) and 2015 data (sideways behavior)

SPY seasonality adjusted- 2017 Period
SPY seasonality adjusted- 2015 Period

Now we can simply test the return of a simple strategy based on seasonality. We are going to buy SPY in October and sell the entire holdings in May or simply comparing return of SPY from October of each year to May of next year.

Sell in May and go away strategy (excluding purchase of bonds from May to October)

Looking at returns of our strategy we can see; this strategy is not as profitable as simply buying and holding for entire year. During market crash of 2008, this strategy lost less money than average market return. However, this might be due to start month of market crash (further analysis is required). One should note that this strategy is extremely simple without considering proper entry and exit point within each month. Therefore, in order to fully understand the profitability and risk of this strategy, in depth analysis is required which is outside the scope of this article.

Another aspect of market seasonality is different market sectors and their respective seasonality and measure the correlation to SPY seasonality:

Stock market sector’s seasonality

SPY seasonality in the above picture overlaied with black line.

I used Spearman Rank Correlation to identify correlation of seasonality of different market sectors to seasonality of market itself. As it can be seen from the tables below, consumer discretionary has highest correlation to market seasonality and real estate sector has the least seasonality correlation to market.

Sector’s seasonality correlation to SPY seasonality

We can use empirical time series model to identify the effect of each month. The model is defined as below:

𝑅=𝛽+𝛼𝑗𝑎𝑛×𝑟𝑗𝑎𝑛+𝛼𝑓𝑒𝑏×𝑟𝑓𝑒𝑏+…𝛼𝑛𝑜𝑣×𝑟𝑛𝑜𝑣R=β+αjan×rjan+αfeb×rfeb+…αnov×rnov

All we need to do is to find returns of each month then use regression analysis to find the coefficients. β in this case can be considered as December return

The result of this multivariate regression can be seen below:

Regression model results

The parameters of the model are plotted below:

Regression model parameters with respect to each month

Looking at the model parameters we can see, month of April/November has the highest coefficient while July has the least coefficient. This shows possibility of higher return in months with higher coefficient and vice versa. This outcome is consistent with our analysis on average monthly return of the market bar chart presented at the beginning of this article.

It is important to note during this study; the remainder component of seasonality has not been studied because it requires more in-depth statistical analysis which is outside the scope of this study. Now let’s see how market reacted in 2018 so far:

SPY return in 2018 (Jan-Sep)

Stay tuned and follow us to get notified on the future episodes.

Thanks for reading! My name is Amir Nejad,PhD. I’m a data scientist and editor of QuantJam , and I love share my ideas and to collaborate with other fellow data scientists. You can connect with me on Github, Twitter, and LinkedIn.

QuantJam:

You can see my other writings at:

About QuantJam

Quantjam is a medium publishing company aims at sharing reproducible research focused on finance and stock market data.

Follow us to stay up-to-date on all articles and activities: https://medium.com/quantjam

Disclaimer

QuantJam writings do not include any investment advice. Past performance is no guarantee of future results. Please consult with your financial advisor before making any investment decision. Investing involves risk and you may incur a profit or loss regardless of strategy selected, including diversification and asset allocation. Investments mentioned may not be suitable for all investors.

All images have been produced by the author, except where stated otherwise.

--

--

Amir Nejad
QuantJam

PhD. Engineer | Data Scientist | Problem Solver | Solution Oriented (twitter: @Dr_Nejad)