Ver en GitHub

Estrategia de Ciclo de Tendencia Color Schaff JCCX

Esta estrategia es una conversión en C# del experto MQL5 Exp_ColorSchaffJCCXTrendCycle. Emplea el oscilador Schaff Trend Cycle (STC) construido sobre el algoritmo JCCX.

Lógica de trading

  • Calcular el Schaff Trend Cycle en cada vela terminada.
  • Cuando el oscilador cae por debajo del High Level tras haber estado por encima, se abre una posición larga y se cierran las posiciones cortas.
  • Cuando el oscilador sube por encima del Low Level tras haber estado por debajo, se abre una posición corta y se cierran las posiciones largas.

Parámetros

Nombre Descripción
Fast JCCX Período JCCX rápido utilizado en el indicador.
Slow JCCX Período JCCX lento utilizado en el indicador.
Smoothing Factor de suavizado JJMA para JCCX.
Phase Valor de fase JJMA.
Cycle Longitud del ciclo para el cálculo del Schaff Trend.
High Level Nivel de disparo superior del oscilador.
Low Level Nivel de disparo inferior del oscilador.
Open Long Permitir apertura de posiciones largas.
Open Short Permitir apertura de posiciones cortas.
Close Long Permitir cierre de posiciones largas existentes.
Close Short Permitir cierre de posiciones cortas existentes.

Notas

La estrategia utiliza la API de alto nivel de StockSharp y se suscribe a datos de velas. Reacciona únicamente a velas terminadas. La gestión del dinero y el control de riesgos se mantienen simples con fines demostrativos.

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 the Schaff Trend Cycle indicator level crossovers.
/// </summary>
public class ColorSchaffJccxTrendCycleStrategy : Strategy
{
	private readonly StrategyParam<decimal> _highLevel;
	private readonly StrategyParam<decimal> _lowLevel;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prev;

	public decimal HighLevel { get => _highLevel.Value; set => _highLevel.Value = value; }
	public decimal LowLevel { get => _lowLevel.Value; set => _lowLevel.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ColorSchaffJccxTrendCycleStrategy()
	{
		_highLevel = Param(nameof(HighLevel), 75m)
			.SetDisplay("High Level", "Upper trigger level", "Signal");

		_lowLevel = Param(nameof(LowLevel), 25m)
			.SetDisplay("Low Level", "Lower trigger level", "Signal");

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

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

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

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

		var stc = new SchaffTrendCycle();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(stc, ProcessCandle)
			.Start();

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

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

		if (_prev is null)
		{
			_prev = stc;
			return;
		}

		if (_prev > HighLevel && stc <= HighLevel && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (_prev < LowLevel && stc >= LowLevel && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prev = stc;
	}
}