在 GitHub 上查看

ASCTrendND 策略

该策略来源于 MQL5 的 ASCTrendND EA。它使用简单移动平均线作为主要趋势信号,RSI 作为确认过滤器,并以 ATR 乘以倍数作为跟踪止损退出交易。此实现是对 ASCTrend + NRTR + TrendStrength 逻辑在 StockSharp 高级 API 上的简化版本。

详情

  • 入场条件:
    • 做多: 收盘价高于 SMA 且 RSI > 50。
    • 做空: 收盘价低于 SMA 且 RSI < 50。
  • 出场条件:
    • 基于 ATR * 倍数的跟踪止损或反向信号。
  • 止损: 仅使用 ATR 跟踪止损。
  • 默认参数:
    • SmaPeriod = 50
    • RsiPeriod = 14
    • AtrPeriod = 14
    • AtrMultiplier = 2.0
    • CandleType = 5 分钟 K 线
  • 过滤器:
    • 类型:趋势跟随
    • 方向:多空皆可
    • 指标:SMA、RSI、ATR
    • 止损:跟踪止损
    • 复杂度:低
    • 时间框架:5m
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// ASCTrendND-inspired strategy using SMA, RSI and ATR-based trailing stop.
/// </summary>
public class AscTrendNdStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _atrMultiplier;

	private SimpleMovingAverage _sma;
	private RelativeStrengthIndex _rsi;
	private AverageTrueRange _atr;

	private decimal? _stopPrice;

	/// <summary>
	/// Candle type for strategy calculation.
	/// </summary>
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	/// <summary>
	/// SMA period.
	/// </summary>
	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }

	/// <summary>
	/// RSI period.
	/// </summary>
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }

	/// <summary>
	/// ATR period for volatility estimate.
	/// </summary>
	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }

	/// <summary>
	/// ATR multiplier for trailing stop distance.
	/// </summary>
	public decimal AtrMultiplier { get => _atrMultiplier.Value; set => _atrMultiplier.Value = value; }

	/// <summary>
	/// Initializes strategy parameters.
	/// </summary>
	public AscTrendNdStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of source candles", "General");

		_smaPeriod = Param(nameof(SmaPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "Length of simple moving average", "Indicators");

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Length of relative strength index", "Indicators");

		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "Length of average true range", "Risk");

		_atrMultiplier = Param(nameof(AtrMultiplier), 2m)
			.SetGreaterThanZero()
			.SetDisplay("ATR Multiplier", "ATR multiplier for stop trailing", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_sma = null;
		_rsi = null;
		_atr = null;
		_stopPrice = null;
	}

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

		_stopPrice = null;

		_sma = new SimpleMovingAverage { Length = SmaPeriod };
		_rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		_atr = new AverageTrueRange { Length = AtrPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_sma, _rsi, _atr, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var price = candle.ClosePrice;

		if (Position == 0)
		{
			if (price > smaValue && rsiValue > 50m)
			{
				_stopPrice = price - atrValue * AtrMultiplier;
				BuyMarket();
			}
			else if (price < smaValue && rsiValue < 50m)
			{
				_stopPrice = price + atrValue * AtrMultiplier;
				SellMarket();
			}
			return;
		}

		if (_stopPrice is null)
			return;

		if (Position > 0)
		{
			var newStop = price - atrValue * AtrMultiplier;
			if (newStop > _stopPrice)
				_stopPrice = newStop;

			if (price <= _stopPrice)
				SellMarket();
		}
		else
		{
			var newStop = price + atrValue * AtrMultiplier;
			if (newStop < _stopPrice)
				_stopPrice = newStop;

			if (price >= _stopPrice)
				BuyMarket();
		}
	}
}