MACD Adaptive Histogram
MACD Adaptive Histogram 策略基于相关指标构建。
测试表明年均收益约为 184%,该策略在加密市场表现最佳。
当指标在指定周期的数据上确认条件时触发信号,适合积极交易者。
止损依赖ATR倍数及其他参数,可根据需要调整默认值以平衡风险和收益。
详细信息
- 入场条件: see implementation for indicator conditions.
- 多空: Both directions.
- 出场条件: opposite signal or stop logic.
- 止损: Yes, using indicator-based calculations.
- 默认值:
FastPeriod = 12SlowPeriod = 26SignalPeriod = 9HistogramAvgPeriod = 20StdDevMultiplier = 2.0mStopLossPercent = 2.0mCandleType = TimeSpan.FromMinutes(15).TimeFrame()
- 过滤器:
- 分类: Trend following
- 方向: Both
- 指标: Histogram
- 止损: Yes
- 复杂度: Intermediate
- 时间框架: Intraday (15m)
- 季节性: No
- 神经网络: No
- 背离: No
- 风险级别: Medium
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()