在 GitHub 上查看

ADX 平滑交叉策略

概要

该策略基于双重平滑的平均方向指数 (ADX)。通过比较平滑后的 +DI 和 -DI 线来识别趋势变化。当平滑后的 +DI 向上穿越 -DI 时开多单,反之则开空单。

工作原理

  • 使用可配置周期的 ADX 指标。
  • 通过 Alpha1Alpha2 参数进行两次指数平滑。
  • 当之前的平滑 +DI 低于平滑 -DI 且当前平滑 +DI 高于平滑 -DI 时产生买入信号。
  • 当出现相反交叉时产生卖出信号。
  • 可选参数允许禁用多头或空头交易,并控制是否在相反信号出现时平仓。
  • 内置风险管理在点数上设置止损和止盈。

参数

名称 说明
AdxPeriod ADX 计算周期。
Alpha1 第一次平滑系数 (0-1)。
Alpha2 第二次平滑系数 (0-1)。
StopLoss 止损点数。
TakeProfit 止盈点数。
AllowBuy 允许做多。
AllowSell 允许做空。
AllowCloseBuy 允许在卖出信号时平掉多单。
AllowCloseSell 允许在买入信号时平掉空单。
CandleType 指标使用的时间框架。

说明

  • 只处理已完成的 K 线。
  • 策略使用带绑定功能的高级 API。
  • 止损和止盈通过 StartProtection 实现。
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>
/// Smoothed EMA crossover strategy (ADX-inspired directional cross concept).
/// Buys when fast EMA crosses above slow EMA, sells when below.
/// </summary>
public class AdxSmoothedCrossStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AdxSmoothedCrossStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicator");

		_slowPeriod = Param(nameof(SlowPeriod), 25)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicator");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = null;
		_prevSlow = null;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (_prevFast is null || _prevSlow is null)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastValue > slowValue;
		var crossDown = _prevFast >= _prevSlow && fastValue < slowValue;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}