In order to discuss this topic of Practical Guide to Automated Detection Trading Patterns with Python, we need to introduce the basics first. I’m sure everyone is familiar with what a trading graph looks like, often represented with green and red bars forming a line graph. There is a lot of data and information condensed into this simple shape. A candle represents one interval of data E.g. one hour on the graph. It will contain four vital pieces of information. The Open, High, Low, and Close for the interval E.g. hour. This is often referred to as OHLC data. The candle will be displayed green if the candle / hour has an Close higher than the Open, and red if the Close is lower than the Open. This is better explained by the image below and hope this enjoy this Practical Guide to Automated Detection Trading Patterns with Python.

Image from ig.com

Register & Get Data

An example of what this looks like is as follows:

US500 hourly from ig.com

What you may notice is that certain patterns emerge with the candle sequence, and these are known as candlestick patterns. There are trading strategies based on these patterns.

Using S&P 500 from EODHD APIs to demonstrate this

The first step is to retrieve the dataset we need, and we will use the official EODHD API Python library for this. The code below will retrieve 720 hours of data which is 30 days.

Screenshot by Author

Register & Get Data

We can start with a simple candlestick pattern called the hammer or hammer pattern. It is a single-candle bullish reversal pattern that be be spotted at the end of a downtrend. The Open, Close, at the top are approximately at the same price, while there is a long wick that extends lower, twice as big as the short body. For more information on this, I highly recommend the description on Investopedia.

A modified version of the candlestick image above to represent this is as follows:

To summarise, the hammer pattern is a bullish signal and is considered a weak reversal pattern. In other words it suggests that the market may change direction from down to up.

I’ve written some code to identify these candles in our dataset.

Screenshot by Author

For illustration purposes, I’m printing the dataset twice. The first dataset shows the detection of the hammer candle. The second dataset shows only the rows where the hammer pattern was detected. Over the last 720 hours, the hammer pattern has been detected 67 times.

Following on from this there is a closely related candlestick pattern that follows on nicey from this and it is the inverted hammer pattern. It is the opposite of the hammer and looks like hammer standing on its head on the graph. It’s still a bullish candle and will be seen at the bottom of a downtrend.

Screenshot by Author

Register & Get Data

Candlestick Patterns

Candlestick patterns are generally categorised into two groups:

  1. Bullish or Bearish
  2. Indecision / Neutral, Weak, Reliable, or Strong

Here is a summary of the common candlestick patterns:

Indecision / Neutral

  • Doji

Weak

  • Hammer (bullish)
  • Inverted Hammer (bearish)
  • Shooting Star (bearish)

Reliable

  • Hanging Man (bearish)
  • Three Line Strike (bullish)
  • Two Black Gapping (bearish)
  • Abandoned Baby (bullish)
  • Morning Doji Star (bullish)
  • Evening Doji Star (bearish)

Strong

  • Three White Soldiers (bullish)
  • Three Black Crows (bearish)
  • Morning Star (bullish)
  • Evening Star (bearish)

I’ve translated these candlestick patterns into Python functions. I’ve used Numpy for some of the more complicated candlestick patterns.

Let’s see if we can find any of the Strong patterns in our dataset…

It looks like the Strong patterns have been detected several times over the last 30 days. Based on the data above you can see when a candlestick pattern was detected, and what it was. A challenge for yourself would be to look at an S&P 500 hourly graph at those time intervals and see if you can spot them.

Conclusion

It’s important to remember, however, that no single strategy guarantees success in the markets. The effectiveness of candlestick pattern strategies, like all trading strategies, can be influenced by market conditions, liquidity, and volatility. Therefore, traders should consider these strategies as part of a broader, diversified trading approach, complementing them with other analysis and tools.