Main Page > Articles > Kumo Breakout > Implementing the Ichimoku Kinko Hyo Indicator from Scratch in Python

Implementing the Ichimoku Kinko Hyo Indicator from Scratch in Python

From TradingHabits, the trading encyclopedia · 5 min read · February 28, 2026
The Black Book of Day Trading Strategies
Free Book

The Black Book of Day Trading Strategies

1,000 complete strategies · 31 chapters · Full trade plans

From Theory to Code

Understanding the theory behind the Ichimoku Kinko Hyo system is one thing; implementing it in code is another. The ability to programmatically calculate the Ichimoku components is a important skill for any quantitative trader who wants to backtest Ichimoku-based strategies or to use Ichimoku as part of a larger algorithmic trading system. This article will provide a step-by-step guide to implementing the Ichimoku Kinko Hyo indicator from scratch in Python using the pandas library.

The Building Blocks of the Ichimoku Indicator

Before we can calculate the Ichimoku components, we first need to have a Pandas DataFrame with the historical price data for the asset we want to analyze. The DataFrame should have columns for the high, low, and closing prices.

Once we have the data, we can start to calculate the five Ichimoku components:

  1. Tenkan-Sen: The Tenkan-Sen is the average of the highest high and the lowest low over the past 9 periods.
  2. Kijun-Sen: The Kijun-Sen is the average of the highest high and the lowest low over the past 26 periods.
  3. Senkou Span A: The Senkou Span A is the average of the Tenkan-Sen and the Kijun-Sen, plotted 26 periods ahead.
  4. Senkou Span B: The Senkou Span B is the average of the highest high and the lowest low over the past 52 periods, plotted 26 periods ahead.
  5. Chikou Span: The Chikou Span is the current closing price, plotted 26 periods behind.

A Python Function for Calculating Ichimoku

We can encapsulate the logic for calculating the Ichimoku components in a single Python function. This function will take a Pandas DataFrame as input and will return a new DataFrame with the Ichimoku components added as new columns.

python
import pandas as pd

def ichimoku(df):
    # Tenkan-Sen
    df['tenkan_sen'] = (df['High'].rolling(window=9).max() + df['Low'].rolling(window=9).min()) / 2

    # Kijun-Sen
    df['kijun_sen'] = (df['High'].rolling(window=26).max() + df['Low'].rolling(window=26).min()) / 2

    # Senkou Span A
    df['senkou_span_a'] = ((df['tenkan_sen'] + df['kijun_sen']) / 2).shift(26)

    # Senkou Span B
    df['senkou_span_b'] = ((df['High'].rolling(window=52).max() + df['Low'].rolling(window=52).min()) / 2).shift(26)

    # Chikou Span
    df['chikou_span'] = df['Close'].shift(-26)

    return df

A Data-Driven Example

Let's see how this function works with some sample data:

DayHighLowClosetenkan_senkijun_sensenkou_span_asenkou_span_bchikou_span
52110100105107.5105106.25105115
53112102108108106107106118
54114104110109107108107120
55116106112111108109.5108122
56118108115113109111109125

Optimizing for Performance

The rolling() function in pandas is very efficient, but it can still be slow on very large datasets. If you need to calculate the Ichimoku components on a very large dataset, you may want to consider using a more optimized library, such as numpy or numba. However, for most backtesting and analysis purposes, the pandas implementation is more than sufficient.

Applying the Ichimoku Function to Real-World Data

Now that we have our ichimoku() function, we can apply it to a real-world dataset. In this example, we will download historical data for Apple (AAPL) and then use our function to calculate the Ichimoku components.

python
import yfinance as yf

# Download historical data for Apple (AAPL)
data = yf.download('AAPL', start='2020-01-01', end='2023-01-01')

# Calculate the Ichimoku components
ichimoku_data = ichimoku(data.copy())

# Print the last 5 rows of the data
print(ichimoku_data.tail())

Conclusion

Implementing the Ichimoku Kinko Hyo indicator from scratch in Python is a valuable exercise for any quantitative trader. It not only deepens your understanding of the indicator, but it also gives you the ability to backtest Ichimoku-based strategies and to use Ichimoku as part of a larger algorithmic trading system. The Python function provided in this article is a robust and efficient implementation that can be used as a starting point for your own Ichimoku analysis.

References:

[1] The Pandas Development Team. (2020). pandas-dev/pandas: Pandas. Zenodo. https://doi.org/10.5281/zenodo.3509134