在 GitHub 上查看

Milestone 趋势策略

该策略是 Milestone 22.5 专家的 StockSharp 移植版本。它通过结合两条平滑移动平均线以及波动性和尖刺过滤器,在趋势方向上交易回调。当K线突破前一根的极值且快速均线支持该动作时,策略沿主趋势开仓。ATR 用于避免在低波动时期交易,而大实体K线被视为尖刺并被忽略。

原始 MQL 版本在主要外汇对上表现良好。C# 版本侧重于清晰性,仅使用市价单进出场。

详情

  • 入场条件:
    • 趋势强度位于 MinTrendMaxTrend 之间。
    • K线突破前高或前低,并得到快速 SMA 的确认。
    • ATR 高于 MinRange,且K线实体小于 CandleSpike
  • 多空方向: 双向。
  • 退出条件: 反向信号平仓。
  • 止损: 未实现,反向信号充当止损。
  • 默认值:
    • SlowMaPeriod = 120
    • FastMaPeriod = 30
    • AtrPeriod = 14
    • MinTrend = 10
    • MaxTrend = 100
    • MinRange = 5
    • CandleSpike = 10
    • CandleType = TimeSpan.FromMinutes(1)
  • 过滤器:
    • 类型: 趋势跟随
    • 方向: 双向
    • 指标: SMA, ATR
    • 止损: 无
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中
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>
/// Milestone trend strategy using EMA crossover with trend confirmation.
/// </summary>
public class MilestoneTrendStrategy : 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 MilestoneTrendStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "General");

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "General");

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

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

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

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

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

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

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}