MACD上の線形戦略
価格と出来高の MACD シグナルを線形回帰と組み合わせた戦略。
詳細
- エントリー条件: 両方の MACD がシグナルを上回り、回帰価格が始値と終値の間にあるときロング。逆の条件でショート。
- ロング/ショート: 両方向。
- エグジット条件: 逆方向のシグナル。
- ストップ: いいえ。
- デフォルト値:
FastLength= 12SlowLength= 26SignalLength= 9Lookback= 21RiskHigh= falseCandleType= TimeSpan.FromMinutes(5)
- フィルター:
- カテゴリ: トレンド
- 方向: 両方
- インジケーター: MACD, Linear Regression
- ストップ: いいえ
- 複雑さ: 中級
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 変動
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;
/// <summary>
/// Linear On MACD strategy.
/// One MACD runs on the close and another on candle volume, and a Lookback linear regression projects the price. A long opens when
/// both MACDs are above their signal lines and the regression price lies between the candle open and close; a short opens when both
/// are below their signals under the same regression condition. With RiskHigh enabled one MACD agreeing is enough. Opposite signals
/// reverse the position.
/// </summary>
public class LinearOnMacdStrategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<int> _signalLength;
private readonly StrategyParam<int> _lookback;
private readonly StrategyParam<bool> _riskHigh;
private readonly StrategyParam<DataType> _candleType;
private MovingAverageConvergenceDivergenceSignal _volumeMacd;
/// <summary>
/// Fast EMA period of both MACDs.
/// </summary>
public int FastLength
{
get => _fastLength.Value;
set => _fastLength.Value = value;
}
/// <summary>
/// Slow EMA period of both MACDs.
/// </summary>
public int SlowLength
{
get => _slowLength.Value;
set => _slowLength.Value = value;
}
/// <summary>
/// Signal line period of both MACDs.
/// </summary>
public int SignalLength
{
get => _signalLength.Value;
set => _signalLength.Value = value;
}
/// <summary>
/// Linear regression lookback.
/// </summary>
public int Lookback
{
get => _lookback.Value;
set => _lookback.Value = value;
}
/// <summary>
/// Accept a signal when only one of the two MACDs agrees.
/// </summary>
public bool RiskHigh
{
get => _riskHigh.Value;
set => _riskHigh.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public LinearOnMacdStrategy()
{
_fastLength = Param(nameof(FastLength), 12)
.SetGreaterThanZero()
.SetDisplay("Fast Length", "Fast EMA period of both MACDs", "MACD");
_slowLength = Param(nameof(SlowLength), 26)
.SetGreaterThanZero()
.SetDisplay("Slow Length", "Slow EMA period of both MACDs", "MACD");
_signalLength = Param(nameof(SignalLength), 9)
.SetGreaterThanZero()
.SetDisplay("Signal Length", "Signal line period of both MACDs", "MACD");
_lookback = Param(nameof(Lookback), 21)
.SetGreaterThanZero()
.SetDisplay("Lookback", "Linear regression lookback", "Regression");
_riskHigh = Param(nameof(RiskHigh), false)
.SetDisplay("Risk High", "Accept a signal when only one MACD agrees", "General");
_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()
{
return [(Security, CandleType)];
}
private MovingAverageConvergenceDivergenceSignal CreateMacd()
{
return new MovingAverageConvergenceDivergenceSignal
{
Macd =
{
ShortMa = { Length = FastLength },
LongMa = { Length = SlowLength },
},
SignalMa = { Length = SignalLength }
};
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var priceMacd = CreateMacd();
_volumeMacd = CreateMacd();
var regression = new LinearReg { Length = Lookback };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(priceMacd, regression, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, regression);
DrawOwnTrades(area);
var oscillators = CreateChartArea();
if (oscillators != null)
DrawIndicator(oscillators, priceMacd);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue priceMacdValue, IIndicatorValue regressionValue)
{
if (candle.State != CandleStates.Finished)
return;
var volumeMacdValue = _volumeMacd.Process(new DecimalIndicatorValue(_volumeMacd, candle.TotalVolume, candle.OpenTime) { IsFinal = true });
if (!priceMacdValue.IsFormed || !regressionValue.IsFormed || !volumeMacdValue.IsFormed)
return;
if (priceMacdValue is not IMovingAverageConvergenceDivergenceSignalValue { Macd: decimal priceMacd, Signal: decimal priceSignal } ||
volumeMacdValue is not IMovingAverageConvergenceDivergenceSignalValue { Macd: decimal volumeMacd, Signal: decimal volumeSignal })
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var predicted = regressionValue.GetValue<decimal>();
var bodyLow = Math.Min(candle.OpenPrice, candle.ClosePrice);
var bodyHigh = Math.Max(candle.OpenPrice, candle.ClosePrice);
var inBody = predicted >= bodyLow && predicted <= bodyHigh;
var priceUp = priceMacd > priceSignal;
var volumeUp = volumeMacd > volumeSignal;
var priceDown = priceMacd < priceSignal;
var volumeDown = volumeMacd < volumeSignal;
var bullish = RiskHigh ? priceUp || volumeUp : priceUp && volumeUp;
var bearish = RiskHigh ? priceDown || volumeDown : priceDown && volumeDown;
if (inBody && bullish && !bearish && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (inBody && bearish && !bullish && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import MovingAverageConvergenceDivergenceSignal, LinearReg
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class linear_on_macd_strategy(Strategy):
"""
Linear On MACD strategy.
One MACD runs on the close and another on candle volume, and a Lookback linear regression projects the price. A long opens when
both MACDs are above their signal lines and the regression price lies between the candle open and close; a short opens when both
are below their signals under the same regression condition. With RiskHigh enabled one MACD agreeing is enough. Opposite signals
reverse the position.
"""
def __init__(self):
super(linear_on_macd_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 12).SetGreaterThanZero().SetDisplay("Fast Length", "Fast EMA period of both MACDs", "MACD")
self._slow_length = self.Param("SlowLength", 26).SetGreaterThanZero().SetDisplay("Slow Length", "Slow EMA period of both MACDs", "MACD")
self._signal_length = self.Param("SignalLength", 9).SetGreaterThanZero().SetDisplay("Signal Length", "Signal line period of both MACDs", "MACD")
self._lookback = self.Param("Lookback", 21).SetGreaterThanZero().SetDisplay("Lookback", "Linear regression lookback", "Regression")
self._risk_high = self.Param("RiskHigh", False).SetDisplay("Risk High", "Accept a signal when only one MACD agrees", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._volume_macd = None
@property
def candle_type(self):
return self._candle_type.Value
def _create_macd(self):
macd = MovingAverageConvergenceDivergenceSignal()
macd.Macd.ShortMa.Length = self._fast_length.Value
macd.Macd.LongMa.Length = self._slow_length.Value
macd.SignalMa.Length = self._signal_length.Value
return macd
def OnStarted2(self, time):
super(linear_on_macd_strategy, self).OnStarted2(time)
price_macd = self._create_macd()
self._volume_macd = self._create_macd()
regression = LinearReg()
regression.Length = self._lookback.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(price_macd, regression, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, regression)
self.DrawOwnTrades(area)
oscillators = self.CreateChartArea()
if oscillators is not None:
self.DrawIndicator(oscillators, price_macd)
def _process_candle(self, candle, price_macd_value, regression_value):
if candle.State != CandleStates.Finished:
return
volume_macd_value = process_float(self._volume_macd, candle.TotalVolume, candle.OpenTime, True)
if not price_macd_value.IsFormed or not regression_value.IsFormed or not volume_macd_value.IsFormed:
return
price_macd = price_macd_value.Macd
price_signal = price_macd_value.Signal
volume_macd = volume_macd_value.Macd
volume_signal = volume_macd_value.Signal
if price_macd is None or price_signal is None or volume_macd is None or volume_signal is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
predicted = regression_value.GetValue[Decimal](None)
body_low = min(candle.OpenPrice, candle.ClosePrice)
body_high = max(candle.OpenPrice, candle.ClosePrice)
in_body = predicted >= body_low and predicted <= body_high
price_up = price_macd > price_signal
volume_up = volume_macd > volume_signal
price_down = price_macd < price_signal
volume_down = volume_macd < volume_signal
if self._risk_high.Value:
bullish = price_up or volume_up
bearish = price_down or volume_down
else:
bullish = price_up and volume_up
bearish = price_down and volume_down
if in_body and bullish and not bearish and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif in_body and bearish and not bullish and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
def CreateClone(self):
return linear_on_macd_strategy()