在 GitHub 上查看

平滑均线策略

概述

该策略围绕简单移动平均线(SMA)进行交易,并使用额外的平滑偏移量。当收盘价与移动平均线相距一定距离时,策略尝试进入市场,以捕捉价格回归。

工作原理

  • 计算所选周期的 SMA。
  • 若当前没有持仓:
    • 当收盘价低于 SMA + Smoothing 时做空。
    • 当收盘价高于 SMA - Smoothing 时做多。
  • 若持有空头:
    • 当收盘价升至 SMA + Smoothing 之上时平仓。
  • 若持有多头:
    • 当收盘价跌至 SMA - Smoothing 之下时平仓。

该策略仅在蜡烛线完成后使用市价单进行操作。

参数

  • MA Period – SMA 的计算周期。
  • Smoothing – 与 SMA 比较的价格偏移量。
  • Candle Type – 用于计算的蜡烛线时间框架。

备注

本策略根据 MQL4 脚本 smoothingaverage.mq4 改写。

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>
/// Moving average strategy with smoothing offset.
/// </summary>
public class SmoothingAverageStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<decimal> _smoothing;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private int _cooldown;

	public int MaPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	public decimal Smoothing
	{
		get => _smoothing.Value;
		set => _smoothing.Value = value;
	}

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

	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	public SmoothingAverageStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 200)
			.SetDisplay("MA Period", "Moving average period", "MA")
			;
		_smoothing = Param(nameof(Smoothing), 1400m)
			.SetDisplay("Smoothing", "Price offset from moving average", "General")
			;
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
		_cooldownBars = Param(nameof(CooldownBars), 36)
			.SetDisplay("Cooldown Bars", "Bars to wait between new signals", "General")
			;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_cooldown = 0;
	}

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

		// create moving average indicator
		var sma = new SimpleMovingAverage { Length = MaPeriod };

		// subscribe to candles
		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 maValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		var price = candle.ClosePrice;
		var offset = (Security.PriceStep ?? 1m) * Smoothing;

		if (Position == 0)
		{
			if (price >= maValue + offset)
			{
				SellMarket();
				_cooldown = CooldownBars;
			}
			else if (price <= maValue - offset)
			{
				BuyMarket();
				_cooldown = CooldownBars;
			}
		}
		else if (Position < 0 && price <= maValue)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		else if (Position > 0 && price >= maValue)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
	}
}