How I Made a Profitable Ichimoku Cloud Strategy Using Python

How I Made a Profitable Ichimoku Cloud Strategy Using Python


This tutorial will explain the Ichimoku Cloud indicator, and it will implement a trading example in Python using data from the iShares Core US Aggregate Bond ETF (AGG) and the Energy Select Sector SPDR Fund (XLE).

Along the way, you will learn how to use the mplfinance Python package to generate candle charts. The aim of the article is not to produce a fantastic strategy but to show you how it can be done using Python.

Related reading: –Python Trading Strategies (Backtesting, Code, List, And Plenty of Coding Examples)

Ichimoku Cloud Strategy

The Ichimoku Cloud Strategy is a technical indicator that aims to identify support and resistance levels in a trading strategy. The Japanese Goichi Hosoda developed this indicator in the late 1960s.

The estimation needs the following components:

\text{Tenka Line} &= \frac{\max(\text{High}_{\text{last 9 periods}}) - \min(\text{Low}_{\text{last 9 periods}})}{2} \quad \text{(Equation 1)} 
\text{Kijun Line} &= \frac{\max(\text{High}_{\text{last 26 periods}}) - \min(\text{Low}_{\text{last 26 periods}})}{2} \quad \text{(Equation 2)} 
\text{Senkou Span A} &= \left(\frac{\text{Tenka Line} + \text{Kijun Line}}{2}\right)_{\text{shifted 26 periods in the future}} \quad \text{(Equation 3)}
\text{Senkou Span B} &= \left(\frac{\text{Higher High}_{\text{last 52 periods}} + \text{Lower Low}_{\text{last 52 periods}}}{2}\right)_{\text{shifted 26 periods in the future}} \quad \text{(Equation 4)}
\text{Lagging Span} &= \left(\text{Closing Price}\right)_{\text{shifted 26 periods in the past}} \quad \text{(Equation 5)}

Python estimation

Download Data

The first step is to import the Python Libraries:

The mplfinance is a Python package for plotting financial data; we will implement it to plot the candle chart.

The following image shows how to import financial data from Yahoo Finance using the Python package yfinance. We implement the download_data function with three parameters security_name (“AGG”), start (“2020-01-01”), and end (“2023-01-01”).

The output with the first five rows from the previous code is:

Python estimation of Ichimoku Cloud

To estimate the Ichimoku Cloud indicator, we estimate the five equations from the previous section.

The Python code to estimate the Tenka Line in the equation 1 is:

In the previous image, tanken_high generates a new series of the maxing values from result[“High”], this series is generated using a rolling window with nine periods.

Next, tenkan_low generates a new series with the minimum values from result[“Low”], to make this series a nine-period rolling window is used.

Finally, with the previous two pandas series, we generated a new pandas series to obtain TenKanLine, the results are:

To make clear the explanation from the previous paragraph, let’s calculate by hand result[‘High’].rolling(tenkan_window).max(); in the following image, we see in the white rectangle nine values of result[“Hight”]; next, we find in that rectangle the maximum value, which is 103.577689.

The second value of tenkan_high is the maximum value of the green rectangle, which is 103.642616, etc. The procedure to obtain tenkan_low is similar to tenkan_high, but rather than finding the maximum value, the pandas package finds the minimum.

To estimate Kijun Line in equation 2 is necessary to follow the same procedure but using a different window length kijun_window = 26:

The Senkou Span A series (equation 3) in Python is:

In the previous image, lead_span_a (Senkou span A) is the mid-point between the series result[“TenkanLine”] and result[“KijunLine”], and then, shifted 26 days. The method .shift moves the series n-days ahead; the following image illustrates with simulated values what .shift does:

In the following image, we see the estimation of Senkou Span B (equation 4), which has three components. The first component senkou_b_high is a series with the maximum values obtained in a window length of 52 days senkou_b_window.

The second component senkou_b_low is a series with the minimum values of result[“Low”] using the same window length.

Meanwhile, the third component result[“lead_span_B”] is a time series with the mid value of senkou_b_high and senkou_b_low then it shifts those values 26 periods ahead.

The estimation in Python of Lagging Span (or Chikou Span) in equation 5 consists of moving backward 26 periods the closing price:

The Kumo (Cloud)

The last component in the Ichimoku Cloud indicator is to calculate the Kumo (cloud) that is:

\text{Kumo} &= \text{Cloud} = \text{Senkou A Span } - \text{Senkou B Span }

Let’s calculate in Python the cloud:

The code from the previous image eliminates the NaN values and converts the index dates into datetime format.

Subsequently, it implements the one-parameter kumo_cloud_plot function; inside, the function uses the mplfinance (mpf) Python package to add plot and chart the candle chart. The mpf.make_plot command adds the series lead_span_A and lead_span_B to the main chart.

Then, mpf.plot will generate a candle chart, and it will add the blue cloud with fill_between. The following image shows the graph from the previous code:

Let’s make the same chart but with the last 100 values:

Generate signals

In this section, we will implement a trading strategy using the Ichimoku Cloud indicator. The strategy is as follows:


  • when the closing price is higher than both the Senkou A Span series  and the Senkou B Span series


  • when the closing price is lower than both the Senkou A Span series and the Senkou B Span series


  • otherwise

The strategy consists of placing a long position when the closing price is above the cloud, and placing a short position when the closing price is under the cloud.

The procedure to calculate the signals from our strategy is as follows:

In the previous image, lean_span_max and lean_span_min are the upper and lower bands from the cloud, respectively.

Then we will create a new series with np.where it will have a one (1) value when the closing price is higher than lean_span_max (the buy condition), it will be minus one (-1) when the closing price is lower than lean_span_min (the sell condition), and zero (0) otherwise.

The following image shows the code of the equity_curve function that calculates the total return from the strategy and enhances the kumo_cloud_plot function:

The term panel = 1 indicates that we will create a second chart, located at the button, with the values of cumulative returns.  The following image shows a graph from the equity curve function:

In the previous chart, we see the evolution of the stock price, the cloud, and the equity curve. The total return from the strategy is around 10%. It is important to know that during the same period, the ETF AGG price declined from around $107 to $95.

XLE ETF (Energy Select Sector SPDR)

When we run the code with the XLE ETF (Energy Select Sector SPDR), we get the following result:

Similar Posts