Choppiness Index Breakout
The Choppiness Index gauges whether the market is trending or ranging. When the indicator drops below a threshold, it signals the start of a trend from a choppy environment.
Testing indicates an average annual return of about 172%. It performs best in the forex market.
This strategy enters in the direction of price relative to its moving average when choppiness falls. It exits if choppiness rises back above a high threshold or a stop-loss hits.
The goal is to catch new trends emerging from consolidation periods.
Details
- Entry Criteria: Choppiness below
ChoppinessThresholdwith price above/below MA. - Long/Short: Both directions.
- Exit Criteria: Choppiness above
HighChoppinessThresholdor stop. - Stops: Yes.
- Default Values:
MAPeriod= 20ChoppinessPeriod= 14ChoppinessThreshold= 38.2mHighChoppinessThreshold= 61.8mCandleType= TimeSpan.FromMinutes(5)
- Filters:
- Category: Breakout
- Direction: Both
- Indicators: Choppiness, MA
- Stops: Yes
- Complexity: Intermediate
- Timeframe: Intraday
- Seasonality: No
- Neural Networks: No
- Divergence: No
- Risk Level: Medium
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Choppiness Index Breakout strategy.
/// Enters when market transitions from choppy to trending state.
/// </summary>
public class ChoppinessIndexBreakoutStrategy : Strategy
{
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<int> _choppinessPeriod;
private readonly StrategyParam<decimal> _choppinessThreshold;
private readonly StrategyParam<decimal> _highChoppinessThreshold;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private decimal _prevChoppiness;
private int _cooldown;
/// <summary>
/// MA Period.
/// </summary>
public int MAPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
/// <summary>
/// Choppiness Index Period.
/// </summary>
public int ChoppinessPeriod
{
get => _choppinessPeriod.Value;
set => _choppinessPeriod.Value = value;
}
/// <summary>
/// Choppiness Threshold (low = trending).
/// </summary>
public decimal ChoppinessThreshold
{
get => _choppinessThreshold.Value;
set => _choppinessThreshold.Value = value;
}
/// <summary>
/// High Choppiness Threshold (for exit).
/// </summary>
public decimal HighChoppinessThreshold
{
get => _highChoppinessThreshold.Value;
set => _highChoppinessThreshold.Value = value;
}
/// <summary>
/// Candle type for strategy calculation.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Cooldown bars between trades.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Initialize the Choppiness Index Breakout strategy.
/// </summary>
public ChoppinessIndexBreakoutStrategy()
{
_maPeriod = Param(nameof(MAPeriod), 20)
.SetDisplay("MA Period", "Period for Moving Average calculation", "Indicators")
.SetOptimize(10, 50, 10);
_choppinessPeriod = Param(nameof(ChoppinessPeriod), 14)
.SetDisplay("Choppiness Period", "Period for Choppiness Index calculation", "Indicators")
.SetOptimize(10, 30, 5);
_choppinessThreshold = Param(nameof(ChoppinessThreshold), 99m)
.SetDisplay("Choppiness Threshold", "Threshold below which market is trending", "Entry")
.SetOptimize(90m, 100m, 1m);
_highChoppinessThreshold = Param(nameof(HighChoppinessThreshold), 99.5m)
.SetDisplay("High Choppiness", "Threshold above which to exit positions", "Exit")
.SetOptimize(95m, 100m, 0.5m);
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_cooldownBars = Param(nameof(CooldownBars), 500)
.SetRange(1, 1000)
.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevChoppiness = 100m;
_cooldown = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevChoppiness = 100m;
_cooldown = 0;
var ma = new SimpleMovingAverage { Length = MAPeriod };
var choppinessIndex = new ChoppinessIndex { Length = ChoppinessPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ma, choppinessIndex, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal maValue, decimal choppinessValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (_cooldown > 0)
{
_cooldown--;
_prevChoppiness = choppinessValue;
return;
}
var isTrending = choppinessValue < ChoppinessThreshold;
var isChoppy = choppinessValue > HighChoppinessThreshold;
if (Position == 0 && isTrending)
{
if (candle.ClosePrice > maValue)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (candle.ClosePrice < maValue)
{
SellMarket();
_cooldown = CooldownBars;
}
}
else if (Position > 0 && isChoppy)
{
SellMarket();
_cooldown = CooldownBars;
}
else if (Position < 0 && isChoppy)
{
BuyMarket();
_cooldown = CooldownBars;
}
_prevChoppiness = choppinessValue;
}
}
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.Indicators import SimpleMovingAverage, ChoppinessIndex
from StockSharp.Algo.Strategies import Strategy
class choppiness_index_breakout_strategy(Strategy):
def __init__(self):
super(choppiness_index_breakout_strategy, self).__init__()
self._ma_period = self.Param("MAPeriod", 20) \
.SetDisplay("MA Period", "Period for Moving Average calculation", "Indicators")
self._choppiness_period = self.Param("ChoppinessPeriod", 14) \
.SetDisplay("Choppiness Period", "Period for Choppiness Index calculation", "Indicators")
self._choppiness_threshold = self.Param("ChoppinessThreshold", 99.0) \
.SetDisplay("Choppiness Threshold", "Threshold below which market is trending", "Entry")
self._high_choppiness_threshold = self.Param("HighChoppinessThreshold", 99.5) \
.SetDisplay("High Choppiness", "Threshold above which to exit positions", "Exit")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._cooldown_bars = self.Param("CooldownBars", 500) \
.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General")
self._prev_choppiness = 100.0
self._cooldown = 0
@property
def MAPeriod(self):
return self._ma_period.Value
@MAPeriod.setter
def MAPeriod(self, value):
self._ma_period.Value = value
@property
def ChoppinessPeriod(self):
return self._choppiness_period.Value
@ChoppinessPeriod.setter
def ChoppinessPeriod(self, value):
self._choppiness_period.Value = value
@property
def ChoppinessThreshold(self):
return self._choppiness_threshold.Value
@ChoppinessThreshold.setter
def ChoppinessThreshold(self, value):
self._choppiness_threshold.Value = value
@property
def HighChoppinessThreshold(self):
return self._high_choppiness_threshold.Value
@HighChoppinessThreshold.setter
def HighChoppinessThreshold(self, value):
self._high_choppiness_threshold.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def CooldownBars(self):
return self._cooldown_bars.Value
@CooldownBars.setter
def CooldownBars(self, value):
self._cooldown_bars.Value = value
def OnStarted2(self, time):
super(choppiness_index_breakout_strategy, self).OnStarted2(time)
self._prev_choppiness = 100.0
self._cooldown = 0
ma = SimpleMovingAverage()
ma.Length = self.MAPeriod
choppiness_index = ChoppinessIndex()
choppiness_index.Length = self.ChoppinessPeriod
self.SubscribeCandles(self.CandleType) \
.Bind(ma, choppiness_index, self.ProcessCandle) \
.Start()
def ProcessCandle(self, candle, ma_value, choppiness_value):
if candle.State != CandleStates.Finished:
return
ma_f = float(ma_value)
chop_f = float(choppiness_value)
chop_threshold = float(self.ChoppinessThreshold)
high_chop_threshold = float(self.HighChoppinessThreshold)
cooldown_bars = int(self.CooldownBars)
if self._cooldown > 0:
self._cooldown -= 1
self._prev_choppiness = chop_f
return
is_trending = chop_f < chop_threshold
is_choppy = chop_f > high_chop_threshold
close = float(candle.ClosePrice)
if self.Position == 0 and is_trending:
if close > ma_f:
self.BuyMarket()
self._cooldown = cooldown_bars
elif close < ma_f:
self.SellMarket()
self._cooldown = cooldown_bars
elif self.Position > 0 and is_choppy:
self.SellMarket()
self._cooldown = cooldown_bars
elif self.Position < 0 and is_choppy:
self.BuyMarket()
self._cooldown = cooldown_bars
self._prev_choppiness = chop_f
def OnReseted(self):
super(choppiness_index_breakout_strategy, self).OnReseted()
self._prev_choppiness = 100.0
self._cooldown = 0
def CreateClone(self):
return choppiness_index_breakout_strategy()