Ver no GitHub

Estratégia de Ciclo de Tendência Color Schaff JCCX

Esta estratégia é uma conversão em C# do especialista MQL5 Exp_ColorSchaffJCCXTrendCycle. Emprega o oscilador Schaff Trend Cycle (STC) construído sobre o algoritmo JCCX.

Lógica de negociação

  • Calcular o Schaff Trend Cycle em cada vela concluída.
  • Quando o oscilador cai abaixo do High Level após ter estado acima dele, uma posição comprada é aberta e as posições vendidas são fechadas.
  • Quando o oscilador sobe acima do Low Level após ter estado abaixo dele, uma posição vendida é aberta e as posições compradas são fechadas.

Parâmetros

Nome Descrição
Fast JCCX Período JCCX rápido usado no indicador.
Slow JCCX Período JCCX lento usado no indicador.
Smoothing Fator de suavização JJMA para JCCX.
Phase Valor de fase JJMA.
Cycle Comprimento do ciclo para o cálculo do Schaff Trend.
High Level Nível de gatilho superior do oscilador.
Low Level Nível de gatilho inferior do oscilador.
Open Long Permitir abertura de posições compradas.
Open Short Permitir abertura de posições vendidas.
Close Long Permitir fechamento de posições compradas existentes.
Close Short Permitir fechamento de posições vendidas existentes.

Notas

A estratégia usa a API de alto nível do StockSharp e subscreve dados de velas. Reage apenas a velas concluídas. O gerenciamento de dinheiro e o controle de risco são mantidos simples para fins de demonstração.

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;
	}
}