在 GitHub 上查看

Firebird MA Envelope Exhaustion

该策略复刻 Firebird v0.60 包络均值回归 EA。首先对蜡烛的开盘价或高低价中值计算简单移动平均线,然后按百分比向上、向下偏移形成上下通道。价格突破上轨时开空,跌破下轨时开多。只有当价格相对最近一次成交至少偏离一个可配置的点差步长时才会加仓,从而实现网格式摊平。所有头寸共用同一个止损,止损触发后会暂时禁止同方向再次入场,避免在趋势行情中不断重启。

详情

  • 入场条件
    • 根据设定选择开盘价或 (High+Low)/2 作为 SMA 输入。
    • 上通道 = SMA × (1 + Percent/100),下通道 = SMA × (1 − Percent/100)。
    • 收盘价高于上轨且空头未被锁定时做空;收盘价低于下轨且多头未被锁定时做多。
    • 当价格比最近的入场价多走 PipStep 点(可按幂次放大)后允许继续加仓。
  • 多空方向:双向。
  • 离场规则
    • 以平均持仓价 ± TakeProfit 点设置共用止盈。
    • 以平均持仓价 ∓ StopLoss / 持仓数量 点设置共用止损。
    • 止损触发后锁定该方向,直到出现反向信号才解除。
  • 止损:有,共享止盈止损。
  • 默认参数
    • 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();
		}
	}
}