MACDアダプティブヒストグラム戦略
MACD Adaptive Histogram戦略は、適応型ヒストグラム閾値を持つMACDを中心に構築されています。
テストでは平均年間リターンが約184%であることが示されています。暗号通貨市場で最もよいパフォーマンスを発揮します。
HistogramがイントラデイI(15m)データでトレンド変化を確認したときにシグナルが発動します。これにより、この手法はアクティブトレーダーに適しています。
ストップはATRの倍数とFastPeriod、SlowPeriodなどの要素に基づいています。デフォルト値を調整してリスクとリワードのバランスを取ってください。
詳細
- エントリー条件: インジケーター条件の実装を参照。
- ロング/ショート: 両方。
- エグジット条件: 反対シグナルまたはストップロジック。
- ストップ: はい、インジケーターに基づく計算を使用。
- デフォルト値:
FastPeriod = 12SlowPeriod = 26SignalPeriod = 9HistogramAvgPeriod = 20StdDevMultiplier = 2.0mStopLossPercent = 2.0mCandleType = TimeSpan.FromMinutes(15).TimeFrame()
- フィルター:
- カテゴリ: トレンドフォロー
- 方向: 両方
- インジケーター: Histogram
- ストップ: はい
- 複雑さ: 中級
- 時間軸: イントラデイ (15m)
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// MACD strategy that adapts entry thresholds to the rolling distribution of the histogram.
/// </summary>
public class MacdAdaptiveHistogramStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _signalPeriod;
private readonly StrategyParam<int> _histogramAvgPeriod;
private readonly StrategyParam<decimal> _stdDevMultiplier;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<DataType> _candleType;
private MovingAverageConvergenceDivergenceSignal _macd;
private SimpleMovingAverage _histAvg;
private StandardDeviation _histStdDev;
private int _cooldown;
/// <summary>
/// Fast EMA period for MACD.
/// </summary>
public int FastPeriod
{
get => _fastPeriod.Value;
set => _fastPeriod.Value = value;
}
/// <summary>
/// Slow EMA period for MACD.
/// </summary>
public int SlowPeriod
{
get => _slowPeriod.Value;
set => _slowPeriod.Value = value;
}
/// <summary>
/// Signal line period for MACD.
/// </summary>
public int SignalPeriod
{
get => _signalPeriod.Value;
set => _signalPeriod.Value = value;
}
/// <summary>
/// Lookback period for the histogram statistics.
/// </summary>
public int HistogramAvgPeriod
{
get => _histogramAvgPeriod.Value;
set => _histogramAvgPeriod.Value = value;
}
/// <summary>
/// Standard deviation multiplier for adaptive histogram thresholds.
/// </summary>
public decimal StdDevMultiplier
{
get => _stdDevMultiplier.Value;
set => _stdDevMultiplier.Value = value;
}
/// <summary>
/// Stop loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Bars to wait after each order.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public MacdAdaptiveHistogramStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 12)
.SetRange(2, 50)
.SetDisplay("Fast Period", "Fast EMA period for MACD", "MACD");
_slowPeriod = Param(nameof(SlowPeriod), 26)
.SetRange(3, 100)
.SetDisplay("Slow Period", "Slow EMA period for MACD", "MACD");
_signalPeriod = Param(nameof(SignalPeriod), 9)
.SetRange(2, 50)
.SetDisplay("Signal Period", "Signal line period for MACD", "MACD");
_histogramAvgPeriod = Param(nameof(HistogramAvgPeriod), 20)
.SetRange(5, 100)
.SetDisplay("Histogram Avg Period", "Lookback period for histogram statistics", "Signals");
_stdDevMultiplier = Param(nameof(StdDevMultiplier), 1.2m)
.SetRange(0.1m, 5m)
.SetDisplay("StdDev Multiplier", "Standard deviation multiplier for adaptive thresholds", "Signals");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetRange(0.5m, 10m)
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
_cooldownBars = Param(nameof(CooldownBars), 16)
.SetRange(1, 200)
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Type of candles for the strategy", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
if (Security != null)
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_macd = null;
_histAvg = null;
_histStdDev = null;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
if (Security == null)
throw new InvalidOperationException("Security is not specified.");
_macd = new MovingAverageConvergenceDivergenceSignal
{
Macd =
{
ShortMa = { Length = FastPeriod },
LongMa = { Length = SlowPeriod },
},
SignalMa = { Length = SignalPeriod },
};
_histAvg = new SimpleMovingAverage { Length = HistogramAvgPeriod };
_histStdDev = new StandardDeviation { Length = HistogramAvgPeriod };
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(_macd, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _macd);
DrawOwnTrades(area);
}
StartProtection(new Unit(0, UnitTypes.Absolute), new Unit(StopLossPercent, UnitTypes.Percent), false);
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue macdValue)
{
if (candle.State != CandleStates.Finished)
return;
var typedValue = (MovingAverageConvergenceDivergenceSignalValue)macdValue;
if (typedValue.Macd is not decimal macd ||
typedValue.Signal is not decimal signal)
return;
var histogram = macd - signal;
var histogramAverage = _histAvg.Process(histogram, candle.OpenTime, true).ToDecimal();
var histogramStdDev = _histStdDev.Process(histogram, candle.OpenTime, true).ToDecimal();
if (!_macd.IsFormed || !_histAvg.IsFormed || !_histStdDev.IsFormed)
return;
if (ProcessState != ProcessStates.Started)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
if (histogramStdDev <= 0)
return;
var upperThreshold = histogramAverage + StdDevMultiplier * histogramStdDev;
var lowerThreshold = histogramAverage - StdDevMultiplier * histogramStdDev;
if (Position == 0)
{
if (histogram >= upperThreshold && histogram > 0)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (histogram <= lowerThreshold && histogram < 0)
{
SellMarket();
_cooldown = CooldownBars;
}
return;
}
if (Position > 0 && histogram <= histogramAverage)
{
SellMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
else if (Position < 0 && histogram >= histogramAverage)
{
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, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import MovingAverageConvergenceDivergenceSignal, SimpleMovingAverage, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class macd_adaptive_histogram_strategy(Strategy):
"""
MACD strategy that adapts entry thresholds to the rolling distribution of the histogram.
"""
def __init__(self):
super(macd_adaptive_histogram_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 12) \
.SetDisplay("Fast Period", "Fast EMA period for MACD", "MACD")
self._slow_period = self.Param("SlowPeriod", 26) \
.SetDisplay("Slow Period", "Slow EMA period for MACD", "MACD")
self._signal_period = self.Param("SignalPeriod", 9) \
.SetDisplay("Signal Period", "Signal line period for MACD", "MACD")
self._histogram_avg_period = self.Param("HistogramAvgPeriod", 20) \
.SetDisplay("Histogram Avg Period", "Lookback period for histogram statistics", "Signals")
self._std_dev_multiplier = self.Param("StdDevMultiplier", 1.2) \
.SetDisplay("StdDev Multiplier", "Standard deviation multiplier for adaptive thresholds", "Signals")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._cooldown_bars = self.Param("CooldownBars", 16) \
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Type of candles for the strategy", "General")
self._macd = None
self._hist_avg = None
self._hist_std_dev = None
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(macd_adaptive_histogram_strategy, self).OnReseted()
self._macd = None
self._hist_avg = None
self._hist_std_dev = None
self._cooldown = 0
def OnStarted2(self, time):
super(macd_adaptive_histogram_strategy, self).OnStarted2(time)
hist_period = int(self._histogram_avg_period.Value)
self._macd = MovingAverageConvergenceDivergenceSignal()
self._macd.Macd.ShortMa.Length = int(self._fast_period.Value)
self._macd.Macd.LongMa.Length = int(self._slow_period.Value)
self._macd.SignalMa.Length = int(self._signal_period.Value)
self._hist_avg = SimpleMovingAverage()
self._hist_avg.Length = hist_period
self._hist_std_dev = StandardDeviation()
self._hist_std_dev.Length = hist_period
self._cooldown = 0
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(self._macd, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, self._macd)
self.DrawOwnTrades(area)
self.StartProtection(Unit(0, UnitTypes.Absolute), Unit(self._stop_loss_percent.Value, UnitTypes.Percent), False)
def _process_candle(self, candle, macd_value):
if candle.State != CandleStates.Finished:
return
macd_val = macd_value.Macd
signal_val = macd_value.Signal
if macd_val is None or signal_val is None:
return
macd_f = float(macd_val)
signal_f = float(signal_val)
histogram = macd_f - signal_f
histogram_average = float(process_float(self._hist_avg, Decimal(histogram), candle.OpenTime, True))
histogram_std_dev = float(process_float(self._hist_std_dev, Decimal(histogram), candle.OpenTime, True))
if not self._macd.IsFormed or not self._hist_avg.IsFormed or not self._hist_std_dev.IsFormed:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
if self._cooldown > 0:
self._cooldown -= 1
return
if histogram_std_dev <= 0:
return
sdm = float(self._std_dev_multiplier.Value)
upper_threshold = histogram_average + sdm * histogram_std_dev
lower_threshold = histogram_average - sdm * histogram_std_dev
cd = int(self._cooldown_bars.Value)
if self.Position == 0:
if histogram >= upper_threshold and histogram > 0:
self.BuyMarket()
self._cooldown = cd
elif histogram <= lower_threshold and histogram < 0:
self.SellMarket()
self._cooldown = cd
return
if self.Position > 0 and histogram <= histogram_average:
self.SellMarket(Math.Abs(self.Position))
self._cooldown = cd
elif self.Position < 0 and histogram >= histogram_average:
self.BuyMarket(Math.Abs(self.Position))
self._cooldown = cd
def CreateClone(self):
return macd_adaptive_histogram_strategy()