Ver en GitHub

Estrategia de Cruce Cero de Derivada

Esta estrategia opera basándose en el cambio de signo de la derivada del precio. La derivada se calcula como el momentum del precio dividido por el período y multiplicado por 100. Cuando la derivada cruza la línea cero, la posición actual se cierra y se abre la posición opuesta.

Parámetros

  • DerivativePeriod - período de suavizado para el cálculo de la derivada.
  • PriceType - precio fuente utilizado para la derivada.
  • BuyEntry - permitir la apertura de posiciones largas.
  • SellEntry - permitir la apertura de posiciones cortas.
  • BuyExit - permitir el cierre de posiciones largas.
  • SellExit - permitir el cierre de posiciones cortas.
  • StopLoss - stop loss en puntos.
  • TakeProfit - take profit en puntos.
  • CandleType - marco temporal de velas.

Lógica

  1. Suscribirse a velas y calcular el momentum del precio seleccionado.
  2. La derivada se obtiene dividiendo el momentum por el período y escalando por 100.
  3. Cuando la derivada pasa de positiva a no positiva, se abre una posición larga y se cierra la corta.
  4. Cuando la derivada pasa de negativa a no negativa, se abre una posición corta y se cierra la larga.
  5. Se aplica protección mediante stop loss y take profit para gestionar el riesgo.
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);
		}
	}
}