This strategy trades against Heiken Ashi candles that appear without wicks. A bullish Heiken Ashi candle whose body is larger than the previous one and lacks a lower shadow triggers a short entry. A bearish candle with a longer body and no upper shadow opens a long. Positions close when an opposite candle without the respective wick forms.
Details
Entry Criteria: bullish HA without lower wick and body larger than previous for shorts; bearish HA without upper wick and body larger than previous for longs
Long/Short: Long & Short
Exit Criteria: opposite colored HA candle without wick
Stops: No
Default Values:
CandleType = 15-minute candles
Filters:
Category: Pattern
Direction: Reversal
Indicators: Heikin-Ashi
Stops: No
Complexity: Basic
Timeframe: Intraday
Seasonality: No
Neural networks: No
Divergence: No
Risk level: Medium
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy trading Heiken Ashi candle color changes.
/// Buys when HA turns bullish, sells when HA turns bearish.
/// </summary>
public class HeikenAshiNoWickStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private decimal _prevHaOpen;
private decimal _prevHaClose;
private bool _prevIsBull;
private bool _hasPrev;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public HeikenAshiNoWickStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevHaOpen = 0;
_prevHaClose = 0;
_prevIsBull = false;
_hasPrev = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
SubscribeCandles(CandleType)
.Bind(ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
decimal haOpen;
decimal haClose;
if (_prevHaOpen == 0 && _prevHaClose == 0)
{
haOpen = (candle.OpenPrice + candle.ClosePrice) / 2m;
haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4m;
}
else
{
haOpen = (_prevHaOpen + _prevHaClose) / 2m;
haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4m;
}
var isBull = haClose > haOpen;
if (_hasPrev)
{
// Buy on bearish -> bullish transition
if (isBull && !_prevIsBull && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
// Sell on bullish -> bearish transition
else if (!isBull && _prevIsBull && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
}
_prevHaOpen = haOpen;
_prevHaClose = haClose;
_prevIsBull = isBull;
_hasPrev = true;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class heiken_ashi_no_wick_strategy(Strategy):
def __init__(self):
super(heiken_ashi_no_wick_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_ha_open = 0.0
self._prev_ha_close = 0.0
self._prev_is_bull = False
self._has_prev = False
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(heiken_ashi_no_wick_strategy, self).OnStarted2(time)
self.SubscribeCandles(self.CandleType) \
.Bind(self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle):
if candle.State != CandleStates.Finished:
return
o = float(candle.OpenPrice)
h = float(candle.HighPrice)
l = float(candle.LowPrice)
c = float(candle.ClosePrice)
if self._prev_ha_open == 0.0 and self._prev_ha_close == 0.0:
ha_open = (o + c) / 2.0
ha_close = (o + h + l + c) / 4.0
else:
ha_open = (self._prev_ha_open + self._prev_ha_close) / 2.0
ha_close = (o + h + l + c) / 4.0
is_bull = ha_close > ha_open
if self._has_prev:
if is_bull and not self._prev_is_bull and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif not is_bull and self._prev_is_bull and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_ha_open = ha_open
self._prev_ha_close = ha_close
self._prev_is_bull = is_bull
self._has_prev = True
def OnReseted(self):
super(heiken_ashi_no_wick_strategy, self).OnReseted()
self._prev_ha_open = 0.0
self._prev_ha_close = 0.0
self._prev_is_bull = False
self._has_prev = False
def CreateClone(self):
return heiken_ashi_no_wick_strategy()