在 GitHub 上查看

Moving Average Shift WaveTrend 策略

该策略结合可配置移动平均线和类 WaveTrend 振荡器。 当价格高于均线且振荡器上升时做多,长期 EMA 与波动率过滤器确认趋势; 相反条件下做空。 头寸由百分比止损、止盈和追踪止损保护。

细节

  • 入场条件:
    • 多头: 价格高于 MA,振荡器 > 0 且上升,长期趋势向上,ATR 高于其平均值,处于交易时间,且当前不在波中。
    • 空头: 价格低于 MA,振荡器 < 0 且下降,长期趋势向下,ATR 高于其平均值,处于交易时间,且当前不在波中。
  • 多头/空头: 双向。
  • 出场条件:
    • 振荡器反转并与 MA 交叉,或触发追踪止损,或保护性止损/止盈。
  • 止损: 是。
  • 默认值:
    • MaType = SMA
    • MaLength = 40
    • OscLength = 15
    • TakeProfitPercent = 1.5
    • StopLossPercent = 1
    • TrailPercent = 1
    • LongMaLength = 200
    • AtrLength = 14
    • StartHour = 9
    • EndHour = 17
  • 筛选:
    • 分类: Trend
    • 方向: Both
    • 指标: MA, Hull MA, ATR
    • 止损: Yes
    • 复杂度: Intermediate
    • 时间框架: Medium-term
using System;
using System.Linq;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Moving Average Shift WaveTrend Strategy.
/// Uses EMA crossover with momentum oscillator.
/// </summary>
public class MovingAverageShiftWaveTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<decimal> _minSpreadPercent;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;
	private int _barIndex;
	private int _lastTradeBar = -1000000;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }
	public decimal MinSpreadPercent { get => _minSpreadPercent.Value; set => _minSpreadPercent.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MovingAverageShiftWaveTrendStrategy()
	{
		_fastLength = Param(nameof(FastLength), 8).SetGreaterThanZero();
		_slowLength = Param(nameof(SlowLength), 21).SetGreaterThanZero();
		_cooldownBars = Param(nameof(CooldownBars), 6).SetGreaterThanZero();
		_minSpreadPercent = Param(nameof(MinSpreadPercent), 0.01m).SetGreaterThanZero();
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

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

		_hasPrev = false;
		_barIndex = 0;
		_lastTradeBar = -1000000;

		var fast = new ExponentialMovingAverage { Length = FastLength };
		var slow = new ExponentialMovingAverage { Length = SlowLength };

		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;

		_barIndex++;

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

		var spreadPercent = candle.ClosePrice != 0m
			? Math.Abs(fast - slow) / candle.ClosePrice * 100m
			: 0m;
		var canTrade = _barIndex - _lastTradeBar >= CooldownBars;
		var crossUp = _prevFast <= _prevSlow && fast > slow && spreadPercent >= MinSpreadPercent;
		var crossDown = _prevFast >= _prevSlow && fast < slow && spreadPercent >= MinSpreadPercent;

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

		_prevFast = fast;
		_prevSlow = slow;
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_prevFast = 0m;
		_prevSlow = 0m;
		_hasPrev = false;
		_barIndex = 0;
		_lastTradeBar = -1000000;
	}
}