在 GitHub 上查看

Supertrend Signal 策略

当收盘价穿越 SuperTrend 线时,本策略开仓。价格上破该线时做多,跌破该线时做空。相反的信号会平掉并反向已有仓位。

SuperTrend 指标基于平均真实波幅 (ATR),跟随价格以确定主导趋势。参数可设置 ATR 周期、倍数以及K线时间框架。

细节

  • 入场条件
    • 多头:收盘价向上突破 SuperTrend 线
    • 空头:收盘价向下跌破 SuperTrend 线
  • 多/空:双向
  • 出场条件
    • 相反的 SuperTrend 突破
  • 止损:无
  • 默认值
    • AtrPeriod = 5
    • Multiplier = 3
    • CandleType = TimeSpan.FromMinutes(15).TimeFrame()
  • 筛选
    • 类别:趋势跟随
    • 方向:双向
    • 指标:SuperTrend(基于 ATR)
    • 止损:否
    • 复杂度:入门
    • 时间框架:中期
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:中
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>
/// Simple SuperTrend crossover strategy.
/// </summary>
public class SupertrendSignalStrategy : Strategy
{
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private bool? _prevIsUpTrend;

	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
	public decimal Multiplier { get => _multiplier.Value; set => _multiplier.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SupertrendSignalStrategy()
	{
		_atrPeriod = Param(nameof(AtrPeriod), 5)
			.SetDisplay("ATR Period", "ATR period for SuperTrend", "Parameters");

		_multiplier = Param(nameof(Multiplier), 3m)
			.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "Parameters");

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

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

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

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

		var st = new SuperTrend { Length = AtrPeriod, Multiplier = Multiplier };
		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(st, ProcessCandle)
			.Start();

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

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

		if (!stValue.IsFormed)
			return;

		var stv = stValue as SuperTrendIndicatorValue;
		if (stv == null)
			return;

		var isUpTrend = stv.IsUpTrend;

		if (IsFormedAndOnlineAndAllowTrading() && _prevIsUpTrend.HasValue)
		{
			if (isUpTrend && !_prevIsUpTrend.Value && Position <= 0)
				BuyMarket();
			else if (!isUpTrend && _prevIsUpTrend.Value && Position >= 0)
				SellMarket();
		}

		_prevIsUpTrend = isUpTrend;
	}
}