๐Ÿ“Š Trading with Heikin Ashi Trend Strategy Using Python Automation

 Heikin Ashi is a powerful candlestick technique used by traders to identify market trends more clearly. Unlike traditional candles, Heikin Ashi smooths price action, making it easier to spot trends and reversals.

In this blog, we'll explore:

  • ✅ What is Heikin Ashi?

  • ๐Ÿ” How to use it for trend trading

  • ๐Ÿค– Automating trades using Python (with backtest example)


๐Ÿ”น What is Heikin Ashi?

Heikin Ashi means "average bar" in Japanese. It uses modified formulas to generate candles:

HA_Close = (Open + High + Low + Close) / 4
HA_Open = (previous_HA_Open + previous_HA_Close) / 2 HA_High = max(High, HA_Open, HA_Close) HA_Low = min(Low, HA_Open, HA_Close)

๐Ÿ” These smoothed candles help eliminate market noise and reduce false signals.


๐Ÿ“ˆ Heikin Ashi Trend Strategy Rules

We’ll use a simple trend-following strategy:

  • Buy when Heikin Ashi candles are green continuously for 3 days

  • Sell when candles turn red continuously for 2 days

  • ๐Ÿ›‘ Optional stop-loss: Recent swing low

  • ๐ŸŽฏ Optional target: Trailing with HA close or 10%


๐Ÿ Python Code for Backtesting & Signal Generation

Here’s a full working script:

import pandas as pd
import yfinance as yf import matplotlib.pyplot as plt # Fetch data symbol = 'ONGC.NS' data = yf.download(symbol, start='2022-01-01') # Calculate Heikin Ashi ha_data = pd.DataFrame(index=data.index) ha_data['HA_Close'] = (data['Open'] + data['High'] + data['Low'] + data['Close']) / 4 ha_data['HA_Open'] = 0.0 ha_data['HA_Open'].iloc[0] = (data['Open'][0] + data['Close'][0]) / 2 for i in range(1, len(data)): ha_data['HA_Open'].iloc[i] = (ha_data['HA_Open'].iloc[i - 1] + ha_data['HA_Close'].iloc[i - 1]) / 2 ha_data['HA_High'] = data[['High', 'Open', 'Close']].max(axis=1) ha_data['HA_Low'] = data[['Low', 'Open', 'Close']].min(axis=1) # Signal Generation ha_data['Color'] = ['Green' if ha_data['HA_Close'][i] > ha_data['HA_Open'][i] else 'Red' for i in range(len(ha_data))] ha_data['Signal'] = '' green_count = 0 red_count = 0 for i in range(len(ha_data)): if ha_data['Color'][i] == 'Green': green_count += 1 red_count = 0 elif ha_data['Color'][i] == 'Red': red_count += 1 green_count = 0 if green_count >= 3: ha_data['Signal'].iloc[i] = 'Buy' elif red_count >= 2: ha_data['Signal'].iloc[i] = 'Sell' # Show latest signal print(ha_data[['HA_Open', 'HA_Close', 'Color', 'Signal']].tail(10))

๐Ÿ’น Backtest Output Sample

| Date | HA_Open | HA_Close | Color | Signal |
|------------|---------|----------|-------|--------| | 2025-07-01 | 265.33 | 270.45 | Green | | | 2025-07-02 | 268.24 | 273.76 | Green | | | 2025-07-03 | 270.80 | 275.50 | Green | Buy | | 2025-07-04 | 273.15 | 278.65 | Green | |

๐Ÿค– Want to Automate? Use with Broker API

You can place orders automatically using:

  • Zerodha Kite Connect

  • Fyers API

  • AngelOne SmartAPI

Example for placing a buy order:

from kiteconnect import KiteConnect
kite = KiteConnect(api_key="your_api_key") kite.set_access_token("your_access_token") kite.place_order( variety=kite.VARIETY_REGULAR, exchange=kite.EXCHANGE_NSE, tradingsymbol="ONGC", transaction_type=kite.TRANSACTION_TYPE_BUY, quantity=1, product=kite.PRODUCT_CNC, order_type=kite.ORDER_TYPE_MARKET )

๐Ÿ“Œ Conclusion

Heikin Ashi is a simple yet powerful trend strategy when paired with:

  • Python automation

  • Clear entry/exit rules

  • Optional stop-loss or trailing logic

✅ Great for swing traders or those looking to automate positional trades.


Comments

Popular posts from this blog

๐Ÿ“ˆ Opening Range Breakout Strategy Explained with Python Code

๐Ÿข Turtle Trading – Classic Breakout and Trend-Following Method (with Python)

๐Ÿ“ˆ Dual Moving Average Strategy Using Python