ホーム
/
戦略のサンプル
GitHub で見る
Hierarchical + K-Means Clustering Strategy
This strategy applies volatility clustering to a SuperTrend system. Average True Range (ATR) values are grouped into three clusters to determine market regime, while the SuperTrend direction triggers entries. An optional moving average and ADX filter confirm trend strength. Positions can be closed early when the bull/bear volume ratio moves toward balance.
Details
Entry Criteria :
Long : SuperTrend turns bullish && cluster trend > 0 && filters pass.
Short : SuperTrend turns bearish && cluster trend < 0 && filters pass.
Long/Short : Both sides.
Exit Criteria :
Volume balance or opposite signal.
Stops : Volume-based only.
Default Values :
ATR Length = 11.
SuperTrend Factor = 3.
Training Data Length = 200.
Moving Average Length = 50.
Trend Strength Period = 14.
Trend Strength Threshold = 20.
Volume Ratio Threshold = 0.9.
Delay Bars = 4.
Filters :
Category: Trend following
Direction: Both
Indicators: Multiple
Stops: Yes
Complexity: Complex
Timeframe: Medium-term
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;
public class HierarchicalKMeansClusteringStrategy : Strategy
{
private readonly StrategyParam<int> _fastEmaPeriod;
private readonly StrategyParam<int> _slowEmaPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFastEma;
private decimal _prevSlowEma;
public int FastEmaPeriod { get => _fastEmaPeriod.Value; set => _fastEmaPeriod.Value = value; }
public int SlowEmaPeriod { get => _slowEmaPeriod.Value; set => _slowEmaPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public HierarchicalKMeansClusteringStrategy()
{
_fastEmaPeriod = Param(nameof(FastEmaPeriod), 120)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
_slowEmaPeriod = Param(nameof(SlowEmaPeriod), 450)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnReseted()
{
base.OnReseted();
_prevFastEma = 0m;
_prevSlowEma = 0m;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fastEma = new ExponentialMovingAverage { Length = FastEmaPeriod };
var slowEma = new ExponentialMovingAverage { Length = SlowEmaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(fastEma, slowEma, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastEma);
DrawIndicator(area, slowEma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal fastEmaValue, decimal slowEmaValue)
{
if (candle.State != CandleStates.Finished) return;
if (_prevFastEma == 0m || _prevSlowEma == 0m)
{
_prevFastEma = fastEmaValue;
_prevSlowEma = slowEmaValue;
return;
}
if (_prevFastEma <= _prevSlowEma && fastEmaValue > slowEmaValue && Position <= 0)
BuyMarket();
else if (_prevFastEma >= _prevSlowEma && fastEmaValue < slowEmaValue && Position >= 0)
SellMarket();
_prevFastEma = fastEmaValue;
_prevSlowEma = slowEmaValue;
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class hierarchical_k_means_clustering_strategy(Strategy):
"""
EMA crossover strategy.
Buys when fast EMA crosses above slow EMA, sells when it crosses below.
"""
def __init__(self):
super(hierarchical_k_means_clustering_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 120) \
.SetDisplay("Fast Period", "Fast EMA period", "General")
self._slow_period = self.Param("SlowPeriod", 450) \
.SetDisplay("Slow Period", "Slow EMA period", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(hierarchical_k_means_clustering_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
def OnStarted2(self, time):
super(hierarchical_k_means_clustering_strategy, self).OnStarted2(time)
fast_ema = ExponentialMovingAverage()
fast_ema.Length = self._fast_period.Value
slow_ema = ExponentialMovingAverage()
slow_ema.Length = self._slow_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast_ema, slow_ema, self._process_candle).Start()
def _process_candle(self, candle, fast_val, slow_val):
if candle.State != CandleStates.Finished:
return
fast = float(fast_val)
slow = float(slow_val)
if self._prev_fast != 0.0 and self._prev_slow != 0.0:
if self._prev_fast <= self._prev_slow and fast > slow:
if self.Position <= 0:
self.BuyMarket()
elif self._prev_fast >= self._prev_slow and fast < slow:
if self.Position >= 0:
self.SellMarket()
self._prev_fast = fast
self._prev_slow = slow
def CreateClone(self):
return hierarchical_k_means_clustering_strategy()