在 GitHub 上查看

ADX System

ADX System 策略使用平均方向指数 (ADX) 及其 +DI 和 -DI 线进行交易。当 ADX 上升且某一方向线突破 ADX 时开仓。策略设定固定的止盈和止损,并使用追踪止损来保护盈利。

细节

  • 入场条件
    • ADX 上升(前一值低于当前值)。
    • 多头:前一 +DI 低于前一 ADX,且当前 +DI 高于当前 ADX。
    • 空头:前一 -DI 低于前一 ADX,且当前 -DI 高于当前 ADX。
  • 出场条件
    • ADX 与 DI 线给出相反信号。
    • 价格触及追踪止损。
    • 价格达到固定的止盈或止损。
  • 方向:双向。
  • 止损/止盈:以绝对价位设置固定止损、止盈和追踪止损。
  • 默认值
    • AdxPeriod = 14
    • TakeProfit = 15
    • StopLoss = 100
    • TrailingStop = 20
    • CandleType = TimeSpan.FromMinutes(5)
  • 筛选器
    • 分类:趋势跟随
    • 方向:双向
    • 指标:ADX、+DI、-DI
    • 止损:有
    • 复杂度:初级
    • 时间框架:短期
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:中等
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>
/// ADX based trading system with trailing stop and fixed take profit/stop loss.
/// </summary>
public class AdxSystemStrategy : Strategy
{
	private readonly StrategyParam<int> _adxPeriod;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _trailingStop;
	private readonly StrategyParam<DataType> _candleType;

	private AverageDirectionalIndex _adx;

	private decimal _prevAdx;
	private decimal _prevPlusDi;
	private decimal _prevMinusDi;

	private decimal? _entryPrice;
	private decimal? _stopPrice;
	private decimal? _takePrice;

	/// <summary>
	/// ADX period.
	/// </summary>
	public int AdxPeriod
	{
		get => _adxPeriod.Value;
		set => _adxPeriod.Value = value;
	}

	/// <summary>
	/// Take profit distance in price units.
	/// </summary>
	public decimal TakeProfit
	{
		get => _takeProfit.Value;
		set => _takeProfit.Value = value;
	}

	/// <summary>
	/// Stop loss distance in price units.
	/// </summary>
	public decimal StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in price units.
	/// </summary>
	public decimal TrailingStop
	{
		get => _trailingStop.Value;
		set => _trailingStop.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="AdxSystemStrategy"/> class.
	/// </summary>
	public AdxSystemStrategy()
	{
		_adxPeriod = Param(nameof(AdxPeriod), 10)
		.SetGreaterThanZero()
		.SetDisplay("ADX Period", "Period for ADX indicator", "Indicators");

		_takeProfit = Param(nameof(TakeProfit), 150m)
		.SetGreaterThanZero()
		.SetDisplay("Take Profit", "Distance for profit target", "Risk");

		_stopLoss = Param(nameof(StopLoss), 250m)
		.SetGreaterThanZero()
		.SetDisplay("Stop Loss", "Distance for protective stop", "Risk");

		_trailingStop = Param(nameof(TrailingStop), 120m)
		.SetGreaterThanZero()
		.SetDisplay("Trailing Stop", "Distance for trailing stop", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
		.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_prevAdx = 0m;
		_prevPlusDi = 0m;
		_prevMinusDi = 0m;
		_entryPrice = null;
		_stopPrice = null;
		_takePrice = null;
	}

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

		_adx = new AverageDirectionalIndex { Length = AdxPeriod };

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

		StartProtection(null, null);

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

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

		if (!adxValue.IsFinal)
		return;

		var adxVal = (AverageDirectionalIndexValue)adxValue;

		if (adxVal.MovingAverage is not decimal currentAdx)
		return;

		var dx = adxVal.Dx;
		if (dx.Plus is not decimal currentPlusDi || dx.Minus is not decimal currentMinusDi)
		return;

		if (_prevAdx != 0m)
		{
			if (Position == 0)
			{
				if (currentAdx >= 20m && currentAdx > _prevAdx && _prevPlusDi <= _prevMinusDi && currentPlusDi > currentMinusDi)
				{
					BuyMarket();
					_entryPrice = candle.ClosePrice;
					_stopPrice = _entryPrice - StopLoss;
					_takePrice = _entryPrice + TakeProfit;
				}
				else if (currentAdx >= 20m && currentAdx > _prevAdx && _prevMinusDi <= _prevPlusDi && currentMinusDi > currentPlusDi)
				{
					SellMarket();
					_entryPrice = candle.ClosePrice;
					_stopPrice = _entryPrice + StopLoss;
					_takePrice = _entryPrice - TakeProfit;
				}
			}
			else if (Position > 0)
			{
				if (_prevPlusDi >= _prevMinusDi && currentPlusDi < currentMinusDi)
				{
					SellMarket(Position);
					_entryPrice = null;
					_stopPrice = null;
					_takePrice = null;
					return;
				}

				if (_entryPrice is decimal entry)
				{
					if (candle.ClosePrice - entry > TrailingStop)
					{
						var newStop = candle.ClosePrice - TrailingStop;
						if (_stopPrice is decimal stop && newStop > stop)
						_stopPrice = newStop;
					}

					if (_takePrice is decimal take && candle.HighPrice >= take)
					{
						SellMarket(Position);
						_entryPrice = null;
						_stopPrice = null;
						_takePrice = null;
					}
					else if (_stopPrice is decimal s && candle.LowPrice <= s)
					{
						SellMarket(Position);
						_entryPrice = null;
						_stopPrice = null;
						_takePrice = null;
					}
				}
			}
			else if (Position < 0)
			{
				if (_prevMinusDi >= _prevPlusDi && currentMinusDi < currentPlusDi)
				{
					BuyMarket(Math.Abs(Position));
					_entryPrice = null;
					_stopPrice = null;
					_takePrice = null;
					return;
				}

				if (_entryPrice is decimal entry)
				{
					if (entry - candle.ClosePrice > TrailingStop)
					{
						var newStop = candle.ClosePrice + TrailingStop;
						if (_stopPrice is decimal stop && newStop < stop)
						_stopPrice = newStop;
					}

					if (_takePrice is decimal take && candle.LowPrice <= take)
					{
						BuyMarket(Math.Abs(Position));
						_entryPrice = null;
						_stopPrice = null;
						_takePrice = null;
					}
					else if (_stopPrice is decimal s && candle.HighPrice >= s)
					{
						BuyMarket(Math.Abs(Position));
						_entryPrice = null;
						_stopPrice = null;
						_takePrice = null;
					}
				}
			}
		}

		_prevAdx = currentAdx;
		_prevPlusDi = currentPlusDi;
		_prevMinusDi = currentMinusDi;
	}
}