Strategy vault / Trend following
Multi Timeframe Smoothed Heikin Ashi Trend Following Quantitative Trading System
This strategy is a trend following system based on smoothed Heikin Ashi candlesticks. By calculating Heikin Ashi candlesticks at a higher timeframe and applying them to trading decisions at lower timeframes, it…

Overview
This strategy is a trend following system based on smoothed Heikin Ashi candlesticks. By calculating Heikin Ashi candlesticks at a higher timeframe and applying them to trading decisions at lower timeframes, it effectively reduces market noise. The strategy offers flexible trading direction options, allowing long-only, short-only, or bi-directional trading, and integrates stop-loss and take-profit functions for fully automated trading.
Strategy Principles
The core logic utilizes the smoothing characteristics of Heikin Ashi candlesticks at higher timeframes to identify trends. Heikin Ashi candlesticks effectively filter market noise and highlight major trends through moving average calculations of opening and closing prices. The system enters long positions in long-only mode when green candles appear, indicating an uptrend, and enters short positions in short-only mode when red candles appear, indicating a downtrend. The strategy also includes percentage-based stop-loss and take-profit mechanisms to help control risk and lock in profits.
Strategy Advantages
- Multi-timeframe integration reduces false signals: Calculating Heikin Ashi indicators at higher timeframes effectively reduces interference from short-term fluctuations.
- Comprehensive risk management: Integrated stop-loss and take-profit functions with flexible parameters adjustable to market volatility.
- Flexible direction selection: Can choose long-only, short-only, or bi-directional trading based on market characteristics.
- Fully automated operation: Clear strategy logic with adjustable parameters, suitable for automated trading.
- Strong adaptability: Applicable to different markets and timeframes with good universality.
Strategy Risks
- Trend reversal risk: May experience significant drawdowns during trend reversals, requiring proper stop-loss settings.
- Range-bound market risk: May incur losses due to frequent trading in sideways markets.
- Parameter optimization risk: Over-optimization may lead to poor performance in live trading.
- Slippage cost risk: Frequent trading may result in high transaction costs.
Strategy Optimization Directions
- Add trend confirmation indicators: Can introduce other technical indicators like RSI or MACD as auxiliary confirmation.
- Optimize stop-loss mechanism: Can implement trailing stops or volatility-based dynamic stop-losses.
- Incorporate volume analysis: Combine volume indicators to improve entry signal reliability.
- Develop adaptive parameters: Automatically adjust stop-loss and take-profit ratios based on market volatility.
- Add time filters: Avoid frequent trading during non-active trading hours.
Summary
This strategy effectively captures market trends through the smoothing characteristics of multi-timeframe Heikin Ashi indicators while controlling drawdowns through comprehensive risk management mechanisms. The strategy's flexibility and scalability give it good practical value, and through continuous optimization and improvement, it can adapt to different market environments. While certain risks exist, stable trading performance can be achieved through appropriate parameter settings and risk management.
Source Pine Script
/*backtest
start: 2024-11-10 00:00:00
end: 2024-12-09 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Optimized Heikin Ashi Strategy with Buy/Sell Options", overlay=true)
// User inputs for customizing backtest settings
startDate = input(timestamp("2023-01-01 00:00"), title="Backtest Start Date", tooltip="Start date for the backtest")
endDate = input(timestamp("2024-01-01 00:00"), title="Backtest End Date", tooltip="End date for the backtest")
// Input for Heikin Ashi timeframe optimization
ha_timeframe = input.timeframe("D", title="Heikin Ashi Timeframe", tooltip="Choose the timeframe for Heikin Ashi candles")
// Inputs for optimizing stop loss and take profit
use_stop_loss = input.bool(true, title="Use Stop Loss")
stop_loss_percent = input.float(2.0, title="Stop Loss (%)", minval=0.0, tooltip="Set stop loss percentage")
use_take_profit = input.bool(true, title="Use Take Profit")
take_profit_percent = input.float(4.0, title="Take Profit (%)", minval=0.0, tooltip="Set take profit percentage")
// Input to choose Buy or Sell
trade_type = input.string("Buy Only", options=["Buy Only", "Sell Only"], title="Trade Type", tooltip="Choose whether to only Buy or only Sell")
// Heikin Ashi calculation on a user-defined timeframe
ha_open = request.security(syminfo.tickerid, ha_timeframe, ta.sma(open, 2), barmerge.gaps_off, barmerge.lookahead_on)
ha_close = request.security(syminfo.tickerid, ha_timeframe, ta.sma(close, 2), barmerge.gaps_off, barmerge.lookahead_on)
ha_high = request.security(syminfo.tickerid, ha_timeframe, math.max(high, close), barmerge.gaps_off, barmerge.lookahead_on)
ha_low = request.security(syminfo.tickerid, ha_timeframe, math.min(low, open), barmerge.gaps_off, barmerge.lookahead_on)
// Heikin Ashi candle colors
ha_bullish = ha_close > ha_open // Green candle
ha_bearish = ha_close < ha_open // Red candle
// Backtest period filter
inDateRange = true
// Trading logic depending on user input
if (inDateRange) // Ensures trades happen only in the selected period
if (trade_type == "Buy Only") // Buy when green, Sell when red
if (ha_bullish and strategy.position_size <= 0) // Buy on green candle only if no position is open
strategy.entry("Buy", strategy.long)
if (ha_bearish and strategy.position_size > 0) // Sell on red candle (close the long position)
strategy.close("Buy")
if (trade_type == "Sell Only") // Sell when red, Exit sell when green
if (ha_bearish and strategy.position_size >= 0) // Sell on red candle only if no position is open
strategy.entry("Sell", strategy.short)
if (ha_bullish and strategy.position_size < 0) // Exit the sell position on green candle
strategy.close("Sell")
// Add Stop Loss and Take Profit conditions if enabled
if (use_stop_loss)
strategy.exit("Stop Loss", from_entry="Buy", stop=strategy.position_avg_price * (1 - stop_loss_percent / 100))
if (use_take_profit)
strategy.exit("Take Profit", from_entry="Buy", limit=strategy.position_avg_price * (1 + take_profit_percent / 100))
// Plot Heikin Ashi candles on the chart
plotcandle(ha_open, ha_high, ha_low, ha_close, color=ha_bullish ? color.green : color.red)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-12-11. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Multi-Timeframe-Smoothed-Heikin-Ashi-Trend-Following-Quantitative-Trading-System.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- WaveTrend and DER Based Swing Trading StrategyPine Script · Trend following · 1d
- Dual Moving Average Crossover Strategy 432902Pine Script · Trend following · 1d
- Dynamic Moving Average Trend Trading StrategyPine Script · Trend following · 1d
- Multiple Timeframe Filtered Trend Following StrategyPine Script · Trend following · 1h
- Concise Dynamic Trend StrategyPine Script · Trend following · 2h
- Quantitative Trading Strategy Based on Double Trend FilterPine Script · Trend following · 1h