Hull MA 出来高スパイク戦略
Hull MA Volume Spike 戦略は、Hull Moving Average と出来高スパイク検出を中心に構築されています。
テストでは年間平均リターン約 43% を示しています。株式市場で最もよく機能します。
インジケーターがイントラデイ (5m) データ上のトレンド変化をスパイクが確認したときにシグナルが発動します。これにより、この手法はアクティブトレーダーに適しています。
ストップは ATR の倍数と HmaPeriod、VolumeAvgPeriod などの要素に依存します。リスクとリワードのバランスをとるためにこれらのデフォルト値を調整してください。
詳細
- エントリー条件: インジケーター条件については実装を参照してください。
- ロング/ショート: 両方。
- エグジット条件: 反対のシグナルまたはストップロジック。
- ストップ: はい、インジケーターベースの計算を使用。
- デフォルト値:
HmaPeriod = 9VolumeAvgPeriod = 20VolumeThresholdFactor = 2.0mCandleType = TimeSpan.FromMinutes(5).TimeFrame()
- フィルター:
- カテゴリ: トレンドフォロー
- 方向: 両方
- インジケーター: Spike
- ストップ: はい
- 複雑さ: 中級
- 時間軸: イントラデイ (5m)
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Trend-following strategy that requires a Hull moving average slope change to be confirmed by a volume spike.
/// </summary>
public class HullMaWithVolumeSpikeStrategy : Strategy
{
private readonly StrategyParam<int> _hmaPeriod;
private readonly StrategyParam<int> _volumeAvgPeriod;
private readonly StrategyParam<decimal> _volumeThresholdFactor;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private HullMovingAverage _hma;
private SimpleMovingAverage _volumeSma;
private StandardDeviation _volumeStdDev;
private decimal _prevHmaValue;
private bool _isInitialized;
private int _cooldown;
/// <summary>
/// Hull moving average period.
/// </summary>
public int HmaPeriod
{
get => _hmaPeriod.Value;
set => _hmaPeriod.Value = value;
}
/// <summary>
/// Period for volume statistics.
/// </summary>
public int VolumeAvgPeriod
{
get => _volumeAvgPeriod.Value;
set => _volumeAvgPeriod.Value = value;
}
/// <summary>
/// Multiplier applied to volume standard deviation.
/// </summary>
public decimal VolumeThresholdFactor
{
get => _volumeThresholdFactor.Value;
set => _volumeThresholdFactor.Value = value;
}
/// <summary>
/// Bars to wait after each order.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Stop loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public HullMaWithVolumeSpikeStrategy()
{
_hmaPeriod = Param(nameof(HmaPeriod), 9)
.SetRange(2, 100)
.SetDisplay("HMA Period", "Period for the Hull moving average", "Indicators");
_volumeAvgPeriod = Param(nameof(VolumeAvgPeriod), 20)
.SetRange(2, 100)
.SetDisplay("Volume Avg Period", "Period for volume statistics", "Indicators");
_volumeThresholdFactor = Param(nameof(VolumeThresholdFactor), 1.8m)
.SetRange(0.1m, 10m)
.SetDisplay("Volume Threshold Factor", "Multiplier for volume spike detection", "Signals");
_cooldownBars = Param(nameof(CooldownBars), 72)
.SetRange(1, 500)
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetRange(0.5m, 10m)
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
if (Security != null)
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_hma = null;
_volumeSma = null;
_volumeStdDev = null;
_prevHmaValue = 0m;
_isInitialized = false;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
if (Security == null)
throw new InvalidOperationException("Security is not specified.");
_hma = new HullMovingAverage { Length = HmaPeriod };
_volumeSma = new SimpleMovingAverage { Length = VolumeAvgPeriod };
_volumeStdDev = new StandardDeviation { Length = VolumeAvgPeriod };
_isInitialized = false;
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_hma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _hma);
DrawOwnTrades(area);
}
StartProtection(new Unit(0, UnitTypes.Absolute), new Unit(StopLossPercent, UnitTypes.Percent), false);
}
private void ProcessCandle(ICandleMessage candle, decimal hmaValue)
{
if (candle.State != CandleStates.Finished)
return;
var volumeAvgValue = _volumeSma.Process(candle.TotalVolume, candle.OpenTime, true).ToDecimal();
var volumeStdDevValue = _volumeStdDev.Process(candle.TotalVolume, candle.OpenTime, true).ToDecimal();
if (!_hma.IsFormed || !_volumeSma.IsFormed || !_volumeStdDev.IsFormed)
return;
if (ProcessState != ProcessStates.Started)
return;
if (!_isInitialized)
{
_prevHmaValue = hmaValue;
_isInitialized = true;
return;
}
if (_cooldown > 0)
{
_cooldown--;
_prevHmaValue = hmaValue;
return;
}
var isHmaRising = hmaValue > _prevHmaValue;
var isHmaFalling = hmaValue < _prevHmaValue;
var volumeThreshold = volumeAvgValue + VolumeThresholdFactor * volumeStdDevValue;
var isVolumeSpiking = candle.TotalVolume >= volumeThreshold;
if (Position == 0)
{
if (isHmaRising && isVolumeSpiking)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (isHmaFalling && isVolumeSpiking)
{
SellMarket();
_cooldown = CooldownBars;
}
}
else if (Position > 0)
{
if (isHmaFalling)
{
SellMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
}
else if (Position < 0)
{
if (isHmaRising)
{
BuyMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
}
_prevHmaValue = hmaValue;
}
}
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, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import HullMovingAverage, SimpleMovingAverage, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class hull_ma_volume_spike_strategy(Strategy):
"""
Trend-following strategy that requires a Hull moving average slope change to be confirmed by a volume spike.
"""
def __init__(self):
super(hull_ma_volume_spike_strategy, self).__init__()
self._hma_period = self.Param("HmaPeriod", 9) \
.SetRange(2, 100) \
.SetDisplay("HMA Period", "Period for the Hull moving average", "Indicators")
self._volume_avg_period = self.Param("VolumeAvgPeriod", 20) \
.SetRange(2, 100) \
.SetDisplay("Volume Avg Period", "Period for volume statistics", "Indicators")
self._volume_threshold_factor = self.Param("VolumeThresholdFactor", 1.8) \
.SetRange(0.1, 10.0) \
.SetDisplay("Volume Threshold Factor", "Multiplier for volume spike detection", "Signals")
self._cooldown_bars = self.Param("CooldownBars", 72) \
.SetRange(1, 500) \
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0) \
.SetRange(0.5, 10.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_hma = 0.0
self._is_initialized = False
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(hull_ma_volume_spike_strategy, self).OnReseted()
self._prev_hma = 0.0
self._is_initialized = False
self._cooldown = 0
def OnStarted2(self, time):
super(hull_ma_volume_spike_strategy, self).OnStarted2(time)
hma = HullMovingAverage()
hma.Length = int(self._hma_period.Value)
vol_period = int(self._volume_avg_period.Value)
self._volume_sma = SimpleMovingAverage()
self._volume_sma.Length = vol_period
self._volume_std_dev = StandardDeviation()
self._volume_std_dev.Length = vol_period
self._is_initialized = False
self._cooldown = 0
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(hma, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, hma)
self.DrawOwnTrades(area)
self.StartProtection(
Unit(0, UnitTypes.Absolute),
Unit(self._stop_loss_percent.Value, UnitTypes.Percent),
False
)
def _process_candle(self, candle, hma_val):
if candle.State != CandleStates.Finished:
return
volume = candle.TotalVolume
volume_avg_value = float(process_float(self._volume_sma, volume, candle.OpenTime, True))
volume_std_dev_value = float(process_float(self._volume_std_dev, volume, candle.OpenTime, True))
if not self.IsFormedAndOnlineAndAllowTrading():
return
if not self._volume_sma.IsFormed or not self._volume_std_dev.IsFormed:
return
hma = float(hma_val)
if not self._is_initialized:
self._prev_hma = hma
self._is_initialized = True
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_hma = hma
return
vtf = float(self._volume_threshold_factor.Value)
volume_threshold = volume_avg_value + vtf * volume_std_dev_value
is_volume_spiking = float(volume) >= volume_threshold
is_hma_rising = hma > self._prev_hma
is_hma_falling = hma < self._prev_hma
cd = int(self._cooldown_bars.Value)
if self.Position == 0:
if is_hma_rising and is_volume_spiking:
self.BuyMarket()
self._cooldown = cd
elif is_hma_falling and is_volume_spiking:
self.SellMarket()
self._cooldown = cd
elif self.Position > 0:
if is_hma_falling:
self.SellMarket(Math.Abs(self.Position))
self._cooldown = cd
elif self.Position < 0:
if is_hma_rising:
self.BuyMarket(Math.Abs(self.Position))
self._cooldown = cd
self._prev_hma = hma
def CreateClone(self):
return hull_ma_volume_spike_strategy()