Hull MA CCI Strategy
This strategy uses Hull MA CCI indicators to generate signals. Long entry occurs when HMA(t) > HMA(t-1) && CCI < -100 (HMA rising with oversold conditions). Short entry occurs when HMA(t) < HMA(t-1) && CCI > 100 (HMA falling with overbought conditions). It is suitable for traders seeking opportunities in mixed markets.
Testing indicates an average annual return of about 52%. It performs best in the crypto market.
Details
- Entry Criteria:
- Long: HMA(t) > HMA(t-1) && CCI < -100 (HMA rising with oversold conditions)
- Short: HMA(t) < HMA(t-1) && CCI > 100 (HMA falling with overbought conditions)
- Long/Short: Both sides.
- Exit Criteria:
- Long: Exit long position when HMA starts falling
- Short: Exit short position when HMA starts rising
- Stops: Yes.
- Default Values:
HullPeriod= 9CciPeriod= 20AtrPeriod= 14AtrMultiplier= 2mCandleType= TimeSpan.FromMinutes(5)
- Filters:
- Category: Mixed
- Direction: Both
- Indicators: Hull MA CCI
- Stops: Yes
- Complexity: Intermediate
- Timeframe: Intraday
- Seasonality: No
- Neural networks: No
- Divergence: No
- Risk Level: Medium
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on Hull Moving Average and CCI indicators
/// </summary>
public class HullMaCciStrategy : Strategy
{
private readonly StrategyParam<int> _hullPeriod;
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<DataType> _candleType;
private decimal _previousHullValue;
private decimal _previousCciValue;
private int _cooldown;
/// <summary>
/// Hull MA period
/// </summary>
public int HullPeriod
{
get => _hullPeriod.Value;
set => _hullPeriod.Value = value;
}
/// <summary>
/// CCI period
/// </summary>
public int CciPeriod
{
get => _cciPeriod.Value;
set => _cciPeriod.Value = value;
}
/// <summary>
/// Bars to wait between trades.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// ATR period for stop-loss
/// </summary>
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
/// <summary>
/// ATR multiplier for stop-loss
/// </summary>
public decimal AtrMultiplier
{
get => _atrMultiplier.Value;
set => _atrMultiplier.Value = value;
}
/// <summary>
/// Candle type for strategy
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor
/// </summary>
public HullMaCciStrategy()
{
_hullPeriod = Param(nameof(HullPeriod), 9)
.SetRange(5, 20)
.SetDisplay("Hull MA Period", "Period for Hull Moving Average", "Indicators")
;
_cciPeriod = Param(nameof(CciPeriod), 20)
.SetRange(10, 50)
.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators")
;
_cooldownBars = Param(nameof(CooldownBars), 100)
.SetRange(1, 200)
.SetDisplay("Cooldown Bars", "Bars between trades", "General");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetRange(7, 28)
.SetDisplay("ATR Period", "ATR period for stop-loss calculation", "Risk Management")
;
_atrMultiplier = Param(nameof(AtrMultiplier), 2m)
.SetRange(1m, 4m)
.SetDisplay("ATR Multiplier", "Multiplier for ATR-based stop-loss", "Risk Management")
;
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_previousHullValue = default;
_previousCciValue = 0m;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
// Initialize indicators
var hullMA = new ExponentialMovingAverage { Length = HullPeriod };
var cci = new CommodityChannelIndex { Length = CciPeriod };
var atr = new AverageTrueRange { Length = AtrPeriod };
// Create subscription and bind indicators
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(hullMA, cci, atr, ProcessIndicators)
.Start();
// Setup chart visualization if available
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, hullMA);
DrawIndicator(area, cci);
DrawOwnTrades(area);
}
}
private void ProcessIndicators(ICandleMessage candle, decimal hullValue, decimal cciValue, decimal atrValue)
{
// Skip unfinished candles
if (candle.State != CandleStates.Finished)
return;
// Check if strategy is ready to trade
if (!IsFormedAndOnlineAndAllowTrading())
return;
// Store previous Hull value for slope detection
var previousHullValue = _previousHullValue;
_previousHullValue = hullValue;
var previousCciValue = _previousCciValue;
_previousCciValue = cciValue;
// Skip first candle until we have previous value
if (previousHullValue == 0)
return;
// Trading logic:
// Long: HMA(t) > HMA(t-1) && CCI < -100 (HMA rising with oversold conditions)
// Short: HMA(t) < HMA(t-1) && CCI > 100 (HMA falling with overbought conditions)
var hullSlope = hullValue > previousHullValue;
var crossedUp = previousCciValue <= 100m && cciValue > 100m;
var crossedDown = previousCciValue >= -100m && cciValue < -100m;
if (_cooldown > 0)
_cooldown--;
if (_cooldown == 0 && hullSlope && crossedUp && Position <= 0)
{
var volume = Volume + Math.Abs(Position);
BuyMarket(volume);
_cooldown = CooldownBars;
}
else if (_cooldown == 0 && !hullSlope && crossedDown && Position >= 0)
{
var volume = Volume + Math.Abs(Position);
SellMarket(volume);
_cooldown = CooldownBars;
}
// Exit conditions based on HMA slope change
else if (Position > 0 && !hullSlope && cciValue < 0m)
{
SellMarket(Position);
_cooldown = CooldownBars;
}
else if (Position < 0 && hullSlope && cciValue > 0m)
{
BuyMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage, CommodityChannelIndex, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
from datatype_extensions import *
class hull_ma_cci_strategy(Strategy):
"""Strategy based on Hull Moving Average and CCI indicators."""
def __init__(self):
super(hull_ma_cci_strategy, self).__init__()
self._hull_period = self.Param("HullPeriod", 9) \
.SetRange(5, 20) \
.SetDisplay("Hull MA Period", "Period for Hull Moving Average", "Indicators")
self._cci_period = self.Param("CciPeriod", 20) \
.SetRange(10, 50) \
.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators")
self._cooldown_bars = self.Param("CooldownBars", 100) \
.SetRange(1, 200) \
.SetDisplay("Cooldown Bars", "Bars between trades", "General")
self._atr_period = self.Param("AtrPeriod", 14) \
.SetRange(7, 28) \
.SetDisplay("ATR Period", "ATR period for stop-loss calculation", "Risk Management")
self._atr_multiplier = self.Param("AtrMultiplier", 2.0) \
.SetRange(1.0, 4.0) \
.SetDisplay("ATR Multiplier", "Multiplier for ATR-based stop-loss", "Risk Management")
self._candle_type = self.Param("CandleType", tf(30)) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._previous_hull_value = 0.0
self._previous_cci_value = 0.0
self._cooldown = 0
@property
def CandleType(self):
return self._candle_type.Value
def OnReseted(self):
super(hull_ma_cci_strategy, self).OnReseted()
self._previous_hull_value = 0.0
self._previous_cci_value = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(hull_ma_cci_strategy, self).OnStarted2(time)
self._previous_hull_value = 0.0
self._previous_cci_value = 0.0
self._cooldown = 0
hull_ma = ExponentialMovingAverage()
hull_ma.Length = self._hull_period.Value
cci = CommodityChannelIndex()
cci.Length = self._cci_period.Value
atr = AverageTrueRange()
atr.Length = self._atr_period.Value
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(hull_ma, cci, atr, self.ProcessIndicators).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, hull_ma)
self.DrawIndicator(area, cci)
self.DrawOwnTrades(area)
def ProcessIndicators(self, candle, hull_value, cci_value, atr_value):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
previous_hull_value = self._previous_hull_value
self._previous_hull_value = float(hull_value)
previous_cci_value = self._previous_cci_value
self._previous_cci_value = float(cci_value)
if previous_hull_value == 0:
return
hull_slope = float(hull_value) > previous_hull_value
crossed_up = previous_cci_value <= 100 and float(cci_value) > 100
crossed_down = previous_cci_value >= -100 and float(cci_value) < -100
if self._cooldown > 0:
self._cooldown -= 1
cooldown_val = int(self._cooldown_bars.Value)
if self._cooldown == 0 and hull_slope and crossed_up and self.Position <= 0:
volume = self.Volume + abs(self.Position)
self.BuyMarket(volume)
self._cooldown = cooldown_val
elif self._cooldown == 0 and not hull_slope and crossed_down and self.Position >= 0:
volume = self.Volume + abs(self.Position)
self.SellMarket(volume)
self._cooldown = cooldown_val
elif self.Position > 0 and not hull_slope and float(cci_value) < 0:
self.SellMarket(self.Position)
self._cooldown = cooldown_val
elif self.Position < 0 and hull_slope and float(cci_value) > 0:
self.BuyMarket(abs(self.Position))
self._cooldown = cooldown_val
def CreateClone(self):
return hull_ma_cci_strategy()