How To Build A Doji Candlestick Trading Strategy Using Python

How To Build A Doji Candlestick Trading Strategy Using Python (Code, Setup, Backtest, Performance)

Candlestick patterns have been a fundamental part of technical analysis in the world of trading for many years. Among the multitude of candlestick patterns, the Doji is one of the most widely recognized. We make a doji candlestick trading strategy.

A Doji is a candlestick pattern that represents indecision in the market, and it can provide valuable insights for traders. 

In this article, we will explore how to build a Doji candlestick trading strategy using Python.

Related reading:

What is a Doji Candlestick?

A Doji candlestick is a candlestick pattern that forms when the opening and closing prices of an asset are very close or virtually identical. The result is a candlestick with a very small body and long wicks. Doji patterns come in various forms, and their interpretations can vary slightly, but they all signify market indecision.

Doji candlestick pattern
Doji candlestick pattern

The different types of Doji patterns include:

  • Standard Doji: The opening and closing prices are virtually identical, and the candlestick has a very small body.
  • Long-Legged Doji: The opening and closing prices are virtually identical, but the candlestick has long upper and lower wicks, indicating greater uncertainty.
  • Dragonfly Doji: The opening and closing prices are at the high of the session, with a long lower shadow.
  • Gravestone Doji: The opening and closing prices are at the low of the session, with a long upper shadow.
Doji Candlestick Trading Strategy

In this article, we are going to show you how to backtest the standard doji pattern in Python. However, we will show you how with a slight modification in the code you can backtest all four of them. Let’s get to it.

How to build a Doji candlestick trading strategy using Python – code and backtest

To build a Doji candlestick trading strategy, you’ll need historical price data, a Python environment, and some essential libraries.

We’ll use the popular Pandas and Matplotlib libraries for data manipulation and visualization, yfinance to download historical price data, and Pandas_TA, which is excellent for technical analysis, including identifying candlestick patterns.

Then, you’ll need historical price data for the stock you want to analyze. With the finance library, we’ll use the yf.download() function, and then we’ll create a new column in the data frame to calculate the daily change in price using the .pct_change()+1 function.

Here is the code:

Once you have the data, you can use the Pandas_TA library to identify Doji patterns. This is done with the following line of code:

The code appends a new column to the dataframe called ‘CDL_DOJI_10_0.1’ and assigns a value of 100 when the pattern is identified.

Please note that we use the name ‘doji’ to identify the standard doji pattern. However, keep in mind that you can change the name to any of the other doji patterns (dojistar, dragonflydoji, or gravestonedoji) and backtest them with the same code.

Now, we can generate the trading signals. In this example, we will hold SPY the day after the doji pattern is identified. To do this, we will create a new column and loop through the dataframe to calculate returns.

The returns column now holds all the returns for the day after a doji pattern is identified. The only thing left to do is to plot the returns using matplotlib.

Here is the graph and equity curve of the strategy:

Doji candlestick trading strategy
Doji candlestick trading strategy

We also decided to calculate some statistics and metrics about the pattern:

  • CAGR is 0.30%
  • Time spent in the market is 12.87%
  • Risk-adjusted return is 2,33% (CAGR divided by time spent in the market)
  • The win rate is 54.31%
  • The average win is 0.77%
  • Average loss is 0.80%

The pattern is profitable but not tradable.

Doji candlestick trading strategy – improved trading rules

Let’s make a second backtest where we put in improved rules.

We make the following trading rules:

  • A doji candlestick pattern is formed, and
  • The IBS indicator is below 0.4.
  • If both conditions above are true, we buy at the close.
  • We use the standard QS sell trigger to exit a position (or we sell after 10 trading days).

These trading rules made the following equity curve for S&P 500 (SPY) from inception until today (Amibroker):

Doji trading rules
Doji trading rules

The 267 trades returned 0.56% per trade and 4.8% annually, and you are invested around 11% of the time.

How to build a Doji candlestick trading strategy using Python – conclusion

To sum up, today we showed you how to backtest a Doji candlestick trading strategy using Python plus an improved strategy. However, remember that this is just a starting point. Developing a successful trading strategy involves thorough research, testing, and risk management.

Similar Posts