Strategie Choppiness Index Breakout
Der Choppiness Index misst, ob der Markt im Trend oder in einer Seitwärtsbewegung ist. Wenn der Indikator unter einen Schwellenwert fällt, signalisiert er den Beginn eines Trends aus einem choppy Umfeld.
Tests zeigen eine durchschnittliche jährliche Rendite von etwa 172%. Am besten funktioniert es im Devisenmarkt.
Diese Strategie tritt in die Richtung des Preises relativ zu seinem gleitenden Durchschnitt ein, wenn die Choppiness sinkt. Sie verlässt die Position, wenn die Choppiness wieder über einen hohen Schwellenwert steigt oder ein Stop-Loss ausgelöst wird.
Das Ziel ist, neue Trends zu erfassen, die nach Konsolidierungsphasen entstehen.
Details
- Einstiegskriterien: Choppiness unter
ChoppinessThresholdmit Preis über/unter MA. - Long/Short: Beide Richtungen.
- Ausstiegskriterien: Choppiness über
HighChoppinessThresholdoder Stop. - Stops: Ja.
- Standardwerte:
MAPeriod= 20ChoppinessPeriod= 14ChoppinessThreshold= 38.2mHighChoppinessThreshold= 61.8mCandleType= TimeSpan.FromMinutes(5)
- Filter:
- Kategorie: Ausbruch
- Richtung: Beide
- Indikatoren: Choppiness, MA
- Stops: Ja
- Komplexität: Mittel
- Zeitrahmen: Intraday
- Saisonalität: Nein
- Neuronale Netze: Nein
- Divergenz: Nein
- Risikolevel: Mittel
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()