在 GitHub 上查看

Multik SMA Exp 策略

概述

该策略从 MetaTrader 5 的 “Multik_SMA_Exp” 专家顾问移植,基于简单移动平均线 (SMA) 斜率的逆势交易方法。

策略跟踪最近三次 SMA 值。如果 SMA 在最近两个完成区间内持续下降,则开多仓;如果 SMA 在最近两个区间内持续上升,则开空仓。当 SMA 斜率反转时平仓。

参数

  • MA Period – 移动平均线周期,默认 50。
  • Candle Type – 计算所用的 K 线类型,默认 1 分钟。

交易规则

  1. 在每根完成的 K 线上计算 SMA。
  2. 计算斜率:
    • dsma1 = SMA[n-1] - SMA[n-2]
    • dsma2 = SMA[n-2] - SMA[n-3]
  3. 入场:
    • dsma1 < 0dsma2 < 0 且当前无多仓,则买入。
    • dsma1 > 0dsma2 > 0 且当前无空仓,则卖出。
  4. 离场:
    • 若持有多仓且 dsma1 > 0,则平多。
    • 若持有空仓且 dsma1 < 0,则平空。

新订单的数量等于策略的 Volume 加上当前仓位的绝对值,以在需要时完全反向。

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>
/// Contrarian strategy based on moving average slope.
/// Buys when SMA decreases for two consecutive periods, sells when it increases.
/// </summary>
public class MultikSmaExpStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _ma0;
	private decimal? _ma1;
	private decimal? _ma2;

	public int Period { get => _period.Value; set => _period.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MultikSmaExpStrategy()
	{
		_period = Param(nameof(Period), 50)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Length of the moving average", "General");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_ma0 = _ma1 = _ma2 = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_ma0 = _ma1 = _ma2 = null;

		var sma = new ExponentialMovingAverage { Length = Period };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		_ma2 = _ma1;
		_ma1 = _ma0;
		_ma0 = smaValue;

		if (_ma2 is null || _ma1 is null)
			return;

		var dsma1 = _ma0.Value - _ma1.Value;
		var dsma2 = _ma1.Value - _ma2.Value;

		// Two consecutive decreases -> contrarian buy
		if (dsma2 < 0 && dsma1 < 0 && Position <= 0)
			BuyMarket();
		// Two consecutive increases -> contrarian sell
		else if (dsma2 > 0 && dsma1 > 0 && Position >= 0)
			SellMarket();
	}
}