MACDアダプティブヒストグラム戦略
統計的に大きな MACD ヒストグラム値を売買します。確定15分足で H = MACD(12,26) − Signal(9) を用い、H の A = SMA(20) と D = StandardDeviation(20) を計算します。全指標の形成と D > 0 を待ちます。
建玉なしで H >= A + 2 × D && H > 0 なら買い、H <= A − 2 × D && H < 0 なら売りです。H <= A で全ロング、H >= A で全ショートを決済します。各シグナル注文後、CooldownBars = 16 本の確定足の間、次のシグナル判断を待ちます。
約定済み建玉には実際の入場約定価格から StopLossPercent = 2 のローカル標準パーセント保護が働きます。銘柄の bid/ask を監視し、シグナル待機期間と独立して成行決済します。足の間の保護には気配配信が必要です。利確は設定しません。
FastPeriod = 12; SlowPeriod = 26; SignalPeriod = 9; HistogramAvgPeriod = 20; StdDevMultiplier = 2; StopLossPercent = 2; CooldownBars = 16.
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), 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), useMarketOrders: true, isLocalStop: true);
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Start();
}
}
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.BusinessEntities")
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.BusinessEntities import Subscription
from StockSharp.Messages import Level1Fields
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", 2.0) \
.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), useMarketOrders=True, isLocalStop=True)
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Start()
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()