GitHub で見る

Bollinger Supertrend 戦略

この戦略はボリンジャーバンドとSupertrendインジケーターを組み合わせて、強い方向性のある動きの中でエントリーポイントを特定します。ボリンジャーバンドはボラティリティの拡大を測定し、Supertrend線は全体的なトレンドを追跡してトレーリングストップとして機能します。

テストでは年間平均リターン約79%を示しています。株式市場で最もパフォーマンスが高いです。

価格が上部ボリンジャーバンドの上で引け、Supertrend線の上に留まると、モメンタムとトレンドの一致を確認してロングトレードが発動します。価格が下部バンドの下で引け、Supertrendレベルの下に留まるとショートトレードが発生します。価格がSupertrendを反対方向にクロスバックすると、モメンタムが薄れたことを示してトレードは終了します。

システムは通常のボラティリティを超えたブレイクアウトを待つため、素早い反転よりも持続的な動きを捉えたいトレーダーに適しています。Supertrendストップは市場の動きに合わせて動的に調整され、手動介入なしでリスク管理を助けます。

詳細

  • エントリー条件:
    • ロング: Close > upper Bollinger Band && Close > Supertrend
    • ショート: Close < lower Bollinger Band && Close < Supertrend
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 価格がSupertrendを下回ったときに終了
    • ショート: 価格がSupertrendを上回ったときに終了
  • ストップ: はい、Supertrendトレーリングストップを使用。
  • デフォルト値:
    • BollingerPeriod = 20
    • BollingerDeviation = 2
    • SupertrendPeriod = 10
    • SupertrendMultiplier = 3
    • CandleType = TimeSpan.FromMinutes(15) ロングは価格がSupertrendラインの下で引けたとき、ショートは上で引けたときに決済します。 保有ポジションと逆方向のエントリーシグナルはドテンになります。
  • フィルター:
    • カテゴリ: トレンド
    • 方向: 両方
    • インジケーター: Bollinger Bands, Supertrend
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Bollinger Supertrend strategy.
/// A close above the upper Bollinger band that is also above the Supertrend line goes long and a close below the lower band that is also
/// below the Supertrend goes short, reversing an opposite position. The Supertrend is the trailing stop: a long closes when price closes
/// below the line and a short when it closes above it.
/// </summary>
public class BollingerSupertrendStrategy : Strategy
{
	private readonly StrategyParam<int> _bollingerPeriod;
	private readonly StrategyParam<decimal> _bollingerDeviation;
	private readonly StrategyParam<int> _supertrendPeriod;
	private readonly StrategyParam<decimal> _supertrendMultiplier;
	private readonly StrategyParam<DataType> _candleType;

	/// <summary>
	/// Period of the Bollinger Bands.
	/// </summary>
	public int BollingerPeriod
	{
		get => _bollingerPeriod.Value;
		set => _bollingerPeriod.Value = value;
	}

	/// <summary>
	/// Standard deviation multiplier of the bands.
	/// </summary>
	public decimal BollingerDeviation
	{
		get => _bollingerDeviation.Value;
		set => _bollingerDeviation.Value = value;
	}

	/// <summary>
	/// ATR period of Supertrend.
	/// </summary>
	public int SupertrendPeriod
	{
		get => _supertrendPeriod.Value;
		set => _supertrendPeriod.Value = value;
	}

	/// <summary>
	/// ATR multiplier of Supertrend.
	/// </summary>
	public decimal SupertrendMultiplier
	{
		get => _supertrendMultiplier.Value;
		set => _supertrendMultiplier.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public BollingerSupertrendStrategy()
	{
		_bollingerPeriod = Param(nameof(BollingerPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("BB Period", "Period of the Bollinger Bands", "Bollinger");

		_bollingerDeviation = Param(nameof(BollingerDeviation), 2m)
			.SetGreaterThanZero()
			.SetDisplay("BB Deviation", "Standard deviation multiplier of the bands", "Bollinger");

		_supertrendPeriod = Param(nameof(SupertrendPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Supertrend Period", "ATR period of Supertrend", "Supertrend");

		_supertrendMultiplier = Param(nameof(SupertrendMultiplier), 3m)
			.SetGreaterThanZero()
			.SetDisplay("Supertrend Multiplier", "ATR multiplier of Supertrend", "Supertrend");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var bollinger = new BollingerBands { Length = BollingerPeriod, Width = BollingerDeviation };
		var supertrend = new SuperTrend { Length = SupertrendPeriod, Multiplier = SupertrendMultiplier };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(bollinger, supertrend, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, bollinger);
			DrawIndicator(area, supertrend);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue bollingerValue, IIndicatorValue supertrendValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!bollingerValue.IsFormed || !supertrendValue.IsFormed)
			return;

		var bands = (BollingerBandsValue)bollingerValue;

		if (bands.UpBand is not decimal upper || bands.LowBand is not decimal lower || supertrendValue is not SuperTrendIndicatorValue trend)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var line = trend.Value;
		var close = candle.ClosePrice;

		if (close > upper && close > line && Position <= 0)
			BuyMarket(Volume + Math.Abs(Position));
		else if (close < lower && close < line && Position >= 0)
			SellMarket(Volume + Math.Abs(Position));
		else if (Position > 0 && close < line)
			SellMarket(Position);
		else if (Position < 0 && close > line)
			BuyMarket(-Position);
	}
}