## Introduction

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.

## 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:

\begin{align*} \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)} \end{align*}

## 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

**function with three parameters**

*download_data***security_name**(“AGG”),

**(“2020-01-01”), and**

*start***(“2023-01-01”).**

*end*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

**, this series is generated using a rolling window with nine periods.**

*result[“High”]*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

**; next, we find in that rectangle the maximum value, which is 103.577689.**

*result[“Hight”]*The second value of ** tenkan_high** is the maximum value of the green rectangle, which is 103.642616, etc. The procedure to obtain

**is similar to**

*tenkan_low***, but rather than finding the maximum value, the pandas package finds the minimum.**

*tenkan_high*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

**and**

*result[“TenkanLine”]***, and then, shifted 26 days. The method**

*result[“KijunLine”]***moves the series n-days ahead; the following image illustrates with simulated values what**

*.shift***does:**

*.shift*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

**using the same window length.**

*result[“Low”]*Meanwhile, the third component ** result[“lead_span_B”]** is a time series with the mid value of

**and**

*senkou_b_high***then it shifts those values 26 periods ahead.**

*senkou_b_low*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:

\begin{align*} \text{Kumo} &= \text{Cloud} = \text{Senkou A Span } - \text{Senkou B Span } \end{align*}

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***) Python package to add plot and chart the candle chart. The**

*mpf***command adds the series**

*mpf.make_plot***and**

*lead_span_A***to the main chart.**

*lead_span_B*Then, ** mpf.plot** will generate a candle chart, and it will add the blue cloud with

**. The following image shows the graph from the previous code:**

*fill_between*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:

**Buy**

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

**Sell**

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

**Hold**

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

**are the upper and lower bands from the cloud, respectively.**

*lean_span_min*Then we will create a new series with ** np.where** it will have a one (1) value when the closing price is higher than

**(the buy condition), it will be minus one (-1) when the closing price is lower than**

*lean_span_max***(the sell condition), and zero (0) otherwise.**

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

**function:**

*kumo_cloud_plot*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: