GitHub で見る

Firebird MA エンベロープ枯渇戦略

この戦略は、Firebird v0.60 エンベロープ復元エキスパートを複製します。単純な移動平均を測定し、それをパーセンテージでオフセットして、上部エンベロープと下部エンベロープを形成します。価格が上部バンドを突き抜けた場合、戦略は売り、下部バンドを突破した場合、戦略は買います。追加のポジションは、価格が前のエントリを超えて少なくとも 1 つの設定可能なピップ ステップを超えた場合にのみ平均化されます。ストップロスの合計はすべてのエントリーで共有され、暴走トレンドが同じ方向に繰り返し再エントリーするのを防ぎます。

詳細

  • エントリー基準:
    • ローソク足の始点または高値/安値の中点のいずれかで SMA を計算します。
    • 上部エンベロープ = SMA × (1 + パーセント/100);下側エンベロープ = SMA × (1 − パーセント/100)。
    • 上部バンドより上の終値でショートにエントリーし(直近のストップがショートにロックされている場合を除く)、下部バンドより下の終値でロングにエントリーします(ロングがロックされている場合を除く)。
    • 価格が最新の約定値を超えて PipStep ピップ (オプションでべき乗でスケール) 移動すると、平均イン取引が許可されます。
  • 長い/短い: 長いと短い。
  • 終了基準:
    • 平均エントリー価格 ± TakeProfit ピップでの共有テイクプロフィット。
    • 平均エントリー価格 ∓ StopLoss / position count ピップでの共有ストップロス。
    • ブロッキングフラグは、停止後に反対の信号がトリガーされるまで、同じ方向への再進入を防ぎます。
  • ストップ: はい、ストップロスとテイクプロフィットの合計です。
  • デフォルト値:
    • MaLength = 10
    • Percent = 0.3
    • TradeOnFriday = true
    • UseHighLow = false (オープンを使用)
    • PipStep = 30
    • IncreasementPower = 0
    • TakeProfit = 30
    • StopLoss = 200
    • TradeVolume = 1
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: SMA 封筒
    • 停車駅: はい
    • 複雑さ: 中
    • 期間: 任意
    • 季節性: オプションの金曜日フィルター
    • ニューラルネットワーク: いいえ
    • 発散: いいえ
    • リスクレベル: 平均化のため高
using System;
using System.Collections.Generic;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Firebird MA Envelope Exhaustion strategy - Bollinger Bands mean reversion.
/// Buys when close drops below lower band (exhaustion).
/// Sells when close rises above upper band (exhaustion).
/// Exits at the middle band.
/// </summary>
public class FirebirdMaEnvelopeExhaustionStrategy : Strategy
{
	private readonly StrategyParam<int> _bbPeriod;
	private readonly StrategyParam<decimal> _bbWidth;
	private readonly StrategyParam<DataType> _candleType;

	public int BbPeriod { get => _bbPeriod.Value; set => _bbPeriod.Value = value; }
	public decimal BbWidth { get => _bbWidth.Value; set => _bbWidth.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public FirebirdMaEnvelopeExhaustionStrategy()
	{
		_bbPeriod = Param(nameof(BbPeriod), 10)
			.SetDisplay("BB Period", "Bollinger Bands period", "Indicators");

		_bbWidth = Param(nameof(BbWidth), 2m)
			.SetDisplay("BB Width", "Bollinger Bands width", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
	protected override void OnReseted() { base.OnReseted(); }

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var bb = new BollingerBands { Length = BbPeriod, Width = BbWidth };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(bb, ProcessCandle)
			.Start();
	}

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

		if (!value.IsFinal || value.IsEmpty)
			return;

		var bbVal = value as BollingerBandsValue;
		if (bbVal == null)
			return;

		var upper = bbVal.UpBand;
		var lower = bbVal.LowBand;
		var middle = bbVal.MovingAverage;

		if (upper == null || lower == null || middle == null)
			return;

		var close = candle.ClosePrice;

		// Close below lower band = exhaustion, buy
		if (close < lower.Value && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Close above upper band = exhaustion, sell
		else if (close > upper.Value && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}
	}
}