在 GitHub 上查看

3MA Bunny Cross 策略

概述

ThreeMaBunnyCrossStrategy 是 MetaTrader 4 专家顾问 “3MA Bunny Cross” 的移植版本。该策略通过监控两个线性加权移动平均线(LWMA)在所选周期收盘价上的交叉来捕捉趋势反转。StockSharp 版本保留了原始策略在交叉后立即反向持仓的思想,并借助高级 API 提供的指标绑定与内置保护功能简化实现。

原始 MQL 描述

原始 EA 使用周期分别为 5 和 20 的两条 LWMA。当快速 LWMA 穿越慢速 LWMA 时,如果存在相反方向的持仓,则立即平仓并按交叉方向开立新仓位。系统始终只允许存在一个方向的持仓。此外,脚本在开仓前会检查最少的历史柱数以及账户可用保证金。

StockSharp 实现细节

  • 策略根据 CandleType 参数(默认 15 分钟)订阅蜡烛数据,并绑定到两个 LinearWeightedMovingAverage 指标。
  • 通过 Bind 直接把指标数值传递给处理方法,无需手动维护缓冲区。
  • 缓存上一根蜡烛的快慢均线数值,以与 MQL 版本相同的方式检测交叉(快速均线上穿或下穿慢速均线)。
  • 当出现看多交叉且当前仓位为空或为空头时,按 Volume + |Position| 的数量提交市价买单,从而平掉空头并建立新的多头;看空信号则对称提交卖单。
  • 启动后调用 StartProtection(),开启内置的风险保护机制。
  • 图表中绘制订阅的蜡烛、两条均线以及策略自身的成交点。

参数

  • CandleType – 订阅蜡烛数据的类型(默认 15 分钟)。
  • FastPeriod – 快速 LWMA 周期,默认值 5,可优化。
  • SlowPeriod – 慢速 LWMA 周期,默认值 20,可优化。

指标

  • LinearWeightedMovingAverage(快速线,默认周期 5)。
  • LinearWeightedMovingAverage(慢速线,默认周期 20)。

交易规则

  1. 仅在蜡烛收盘后处理信号,同时确认策略已形成、在线并允许交易。
  2. 如果上一根蜡烛中快速 LWMA 低于或等于慢速 LWMA,而当前蜡烛高于或等于慢速 LWMA,则判定为看多交叉,平掉空头并开多。
  3. 如果上一根蜡烛中快速 LWMA 高于或等于慢速 LWMA,而当前蜡烛低于或等于慢速 LWMA,则判定为看空交叉,平掉多头并开空。
  4. 每笔新单的数量均为 Volume + |Position|,确保能够完全反转仓位,使账户在任意时刻仅持有一个方向。
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>
/// 3MA Bunny Cross strategy using weighted moving average crossover.
/// Goes long on fast WMA crossing above slow WMA, short on opposite.
/// </summary>
public class ThreeMaBunnyCrossStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ThreeMaBunnyCrossStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 5)
			.SetDisplay("Fast WMA", "Fast weighted MA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 20)
			.SetDisplay("Slow WMA", "Slow weighted MA period", "Indicators");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0m;
		_prevSlow = 0m;
		_hasPrev = false;
	}

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

		_hasPrev = false;

		var fast = new WeightedMovingAverage { Length = FastPeriod };
		var slow = new WeightedMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_hasPrev)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fast > slow;
		var crossDown = _prevFast >= _prevSlow && fast < slow;

		if (crossUp && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}