Ver en GitHub

Estrategia Cronex CCI

Estrategia basada en el cruce del Índice de Canal de Materias Primas Cronex. El indicador suaviza el CCI a través de dos medias móviles exponenciales para crear una línea rápida y una lenta.

La estrategia abre una posición larga cuando la línea rápida cruza por debajo de la línea lenta y cierra cualquier posición corta. Se abre una posición corta cuando la línea rápida cruza por encima de la línea lenta y cierra cualquier posición larga.

Este enfoque contrario intenta capturar reversiones tras los cambios de momentum. Funciona en marcos temporales más altos, como velas de 4 horas.

Detalles

  • Criterios de entrada: Cruces de las líneas CCI rápida y lenta suavizadas.
  • Largo/Corto: Ambas direcciones.
  • Criterios de salida: Cruce opuesto.
  • Stops: No.
  • Valores predeterminados:
    • CciPeriod = 25
    • FastPeriod = 14
    • SlowPeriod = 25
    • CandleType = TimeSpan.FromHours(4)
    • EnableLongEntry = true
    • EnableShortEntry = true
    • EnableLongExit = true
    • EnableShortExit = true
  • Filtros:
    • Categoría: Reversión a la media
    • Dirección: Ambos
    • Indicadores: CCI, EMA
    • Stops: No
    • Complejidad: Básico
    • Marco temporal: Swing (4h)
    • Estacionalidad: No
    • Redes neuronales: No
    • Divergencia: No
    • Nivel de riesgo: Medio
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>
/// Strategy based on Cronex CCI indicator.
/// </summary>
public class CronexCciStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<bool> _enableLongEntry;
	private readonly StrategyParam<bool> _enableShortEntry;
	private readonly StrategyParam<bool> _enableLongExit;
	private readonly StrategyParam<bool> _enableShortExit;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	/// <summary>
	/// CCI period.
	/// </summary>
	public int CciPeriod
	{
		get => _cciPeriod.Value;
		set => _cciPeriod.Value = value;
	}

	/// <summary>
	/// Fast smoothing period.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow smoothing period.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

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

	/// <summary>
	/// Enable opening long positions.
	/// </summary>
	public bool EnableLongEntry
	{
		get => _enableLongEntry.Value;
		set => _enableLongEntry.Value = value;
	}

	/// <summary>
	/// Enable opening short positions.
	/// </summary>
	public bool EnableShortEntry
	{
		get => _enableShortEntry.Value;
		set => _enableShortEntry.Value = value;
	}

	/// <summary>
	/// Enable closing long positions.
	/// </summary>
	public bool EnableLongExit
	{
		get => _enableLongExit.Value;
		set => _enableLongExit.Value = value;
	}

	/// <summary>
	/// Enable closing short positions.
	/// </summary>
	public bool EnableShortExit
	{
		get => _enableShortExit.Value;
		set => _enableShortExit.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="CronexCciStrategy"/>.
	/// </summary>
	public CronexCciStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 25)
			.SetRange(5, 100)
			.SetDisplay("CCI Period", "CCI calculation length", "Indicators")
			;

		_fastPeriod = Param(nameof(FastPeriod), 14)
			.SetRange(2, 50)
			.SetDisplay("Fast Period", "Fast smoothing period", "Indicators")
			;

		_slowPeriod = Param(nameof(SlowPeriod), 25)
			.SetRange(2, 100)
			.SetDisplay("Slow Period", "Slow smoothing period", "Indicators")
			;

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

		_enableLongEntry = Param(nameof(EnableLongEntry), true)
			.SetDisplay("Enable Long Entry", "Allow opening long positions", "Trading");

		_enableShortEntry = Param(nameof(EnableShortEntry), true)
			.SetDisplay("Enable Short Entry", "Allow opening short positions", "Trading");

		_enableLongExit = Param(nameof(EnableLongExit), true)
			.SetDisplay("Enable Long Exit", "Allow closing long positions", "Trading");

		_enableShortExit = Param(nameof(EnableShortExit), true)
			.SetDisplay("Enable Short Exit", "Allow closing short positions", "Trading");
	}

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

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

		_prevFast = null;
		_prevSlow = null;
	}

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

		// Initialize indicators
		var cci = new CommodityChannelIndex { Length = CciPeriod };
		var fastMa = new EMA { Length = FastPeriod };
		var slowMa = new EMA { Length = SlowPeriod };

		// Subscribe to candles and bind indicators
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(cci, fastMa, slowMa, ProcessCandle)
			.Start();

		// Setup chart visualization
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastMa);
			DrawIndicator(area, slowMa);
			DrawOwnTrades(area);
		}
	}

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

		// Check if strategy is ready to trade
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var prevFast = _prevFast;
		var prevSlow = _prevSlow;

		if (prevFast.HasValue && prevSlow.HasValue)
		{
			if (prevFast > prevSlow)
			{
				if (EnableShortExit && Position < 0)
					BuyMarket();

				if (EnableLongEntry && fastValue <= slowValue && Position <= 0)
					BuyMarket();
			}
			else if (prevFast < prevSlow)
			{
				if (EnableLongExit && Position > 0)
					SellMarket();

				if (EnableShortEntry && fastValue >= slowValue && Position >= 0)
					SellMarket();
			}
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}