在 GitHub 上查看

Exp Super Trend 策略

该策略由 MQL 脚本 Exp_Super_Trend.mq5(ID 14269)转换而来。它跟随 SuperTrend 指标的方向,在趋势反转时立即反向开仓。实现基于 StockSharp 的高级 API,并使用内置的 SuperTrend 指标。

该指标依据 ATR 计算动态支撑或阻力线。价格高于该线时视为多头趋势,低于该线时视为空头趋势。策略在多头趋势期间建立多头头寸,在空头趋势期间建立空头头寸。每当指标翻转时,当前仓位会立即平仓并在相反方向开仓。

此方法适用于趋势明显的市场,在突破后可能出现大幅波动。同时它也是学习示例,展示如何使用 BindEx 连接指标并在收盘后执行市价单。

细节

  • 入场条件
    • 多头:SuperTrend 显示上升趋势。
    • 空头:SuperTrend 显示下降趋势。
  • 多/空:均可。
  • 出场条件:SuperTrend 给出相反信号(仓位反转)。
  • 止损:无显式止损;指标线充当跟踪止损。
  • 默认值
    • AtrPeriod = 10
    • Multiplier = 3m
    • CandleType = TimeSpan.FromHours(1).TimeFrame()
  • 过滤器
    • 类别:趋势跟随
    • 方向:多空皆可
    • 指标:SuperTrend
    • 止损:基于指标
    • 复杂度:基础
    • 时间框架:中期(默认 1 小时)
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// SuperTrend expert strategy.
/// Opens positions following the SuperTrend direction.
/// </summary>
public class ExpSuperTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private SuperTrend _superTrend;

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

	/// <summary>
	/// ATR multiplier for SuperTrend.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of <see cref="ExpSuperTrendStrategy"/>.
	/// </summary>
	public ExpSuperTrendStrategy()
	{
		_atrPeriod = Param(nameof(AtrPeriod), 10)
			.SetDisplay("ATR Period", "ATR period for SuperTrend", "SuperTrend")
			.SetGreaterThanZero()
			
			.SetOptimize(5, 20, 1);

		_multiplier = Param(nameof(Multiplier), 3m)
			.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "SuperTrend")
			.SetGreaterThanZero()
			
			.SetOptimize(1m, 5m, 0.5m);

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

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

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

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

		_superTrend = new SuperTrend
		{
			Length = AtrPeriod,
			Multiplier = Multiplier
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(_superTrend, ProcessCandle)
			.Start();
	}

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

		if (stValue is not SuperTrendIndicatorValue st)
			return;

		var isUpTrend = st.IsUpTrend;

		if (isUpTrend && Position <= 0)
		{
			BuyMarket();
		}
		else if (!isUpTrend && Position >= 0)
		{
			SellMarket();
		}
	}
}