在 GitHub 上查看

导数零交叉策略

该策略基于价格导数的符号变化进行交易。导数计算为价格动量除以周期再乘以100。当导数穿越零线时,当前仓位被关闭并开立相反方向的新仓位。

参数

  • DerivativePeriod - 导数计算的平滑周期。
  • PriceType - 用于计算的价格类型。
  • BuyEntry - 允许做多。
  • SellEntry - 允许做空。
  • BuyExit - 允许平多。
  • SellExit - 允许平空。
  • StopLoss - 以点数表示的止损。
  • TakeProfit - 以点数表示的止盈。
  • CandleType - 蜡烛图时间框架。

逻辑

  1. 订阅蜡烛并计算所选价格的动量。
  2. 导数等于动量除以周期乘以100。
  3. 当导数从正值变为非正值时,关闭空头并开多头。
  4. 当导数从负值变为非负值时,关闭多头并开空头。
  5. 使用止损和止盈来控制风险。
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>
/// Strategy based on zero crossing of the price derivative.
/// The derivative is calculated as momentum divided by period.
/// When the derivative switches sign, the position is reversed.
/// </summary>
public class DerivativeZeroCrossStrategy : Strategy
{
	private readonly StrategyParam<int> _derivativePeriod;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevDerivative;

	public int DerivativePeriod
	{
		get => _derivativePeriod.Value;
		set => _derivativePeriod.Value = value;
	}

	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public DerivativeZeroCrossStrategy()
	{
		_derivativePeriod = Param(nameof(DerivativePeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Derivative Period", "Smoothing period for derivative", "Indicator");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
			.SetGreaterThanZero();

		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk")
			.SetGreaterThanZero();

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).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();
		_prevDerivative = null;
	}

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

		var momentum = new Momentum { Length = DerivativePeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(momentum, (candle, momValue) =>
		{
			if (candle.State != CandleStates.Finished)
				return;

			var derivative = momValue / DerivativePeriod * 100m;

			if (_prevDerivative is null)
			{
				_prevDerivative = derivative;
				return;
			}

			var prev = _prevDerivative.Value;

			// Derivative crossed up through zero -> buy
			if (prev <= 0m && derivative > 0m)
			{
				if (Position < 0) BuyMarket();
				if (Position <= 0) BuyMarket();
			}
			// Derivative crossed down through zero -> sell
			else if (prev >= 0m && derivative < 0m)
			{
				if (Position > 0) SellMarket();
				if (Position >= 0) SellMarket();
			}

			_prevDerivative = derivative;
		}).Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

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