在 GitHub 上查看

Simple Multiple Time Frame Moving Average 策略

该策略改编自 simple_multiple_time_frame_moving_average.mq4,通过在两个不同周期的简单移动平均线之间寻找趋势一致性来交易。

策略逻辑

  • 对1小时和4小时K线计算周期为 Length 的SMA。
  • 当两条SMA同时向上时开多。
  • 当两条SMA同时向下时开空。
  • 若任一SMA开始下降则平掉多单。
  • 若任一SMA开始上升则平掉空单。
  • 策略同一时间仅持有一个方向的仓位。

参数

  • MA Length (Length):两条SMA的周期长度。
  • Short Time Frame (ShortCandleType):短周期K线,默认为1小时。
  • Long Time Frame (LongCandleType):长周期K线,默认为4小时。

订单数量使用策略的 Volume 属性。

说明

该实现仅保留原MQL版本中的小时和四小时均线逻辑,未包含更高时间框架的计算。

using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy using fast and slow SMA slopes for trade direction.
/// Buys when both SMAs are rising, sells when both are falling.
/// </summary>
public class SimpleMultipleTimeFrameMovingAverageStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SimpleMultipleTimeFrameMovingAverageStrategy()
	{
		_fastLength = Param(nameof(FastLength), 5)
			.SetDisplay("Fast MA", "Fast moving average period", "General");

		_slowLength = Param(nameof(SlowLength), 20)
			.SetDisplay("Slow MA", "Slow moving average period", "General");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = _prevSlow = null;
	}

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

		var fastSma = new ExponentialMovingAverage { Length = FastLength };
		var slowSma = new ExponentialMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastSma, slowSma, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastSma);
			DrawIndicator(area, slowSma);
			DrawOwnTrades(area);
		}
	}

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

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

		if (_prevFast is decimal pf && _prevSlow is decimal ps)
		{
			var fastUp = fast > pf;
			var fastDown = fast < pf;
			var slowUp = slow > ps;
			var slowDown = slow < ps;

			if (fastUp && slowUp && Position <= 0)
				BuyMarket();
			else if (fastDown && slowDown && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}