Открыть на GitHub

Пробой по CCI

Стратегия основана на пробое индекса товарного канала (CCI).

Тестирование показывает среднегодичную доходность около 94%. Стратегию лучше запускать на фондовом рынке.

CCI Breakout использует индикатор Commodity Channel Index для выявления сильных движений. Всплески выше положительного или ниже отрицательного порога CCI формируют входы. Выход происходит, когда CCI возвращается к нулю или появляется противоположный сигнал.

Так как CCI измеряет отклонение от скользящей средней, экстремальные значения подразумевают неустойчивые цены. Система ждёт этих экстремумов, пытаясь заработать на продолжении движения.

Подробности

  • Критерии входа: сигналы на основе CCI и Momentum.
  • Длинные/короткие: оба направления.
  • Критерии выхода: противоположный сигнал или стоп.
  • Стопы: да.
  • Параметры по умолчанию:
    • CciPeriod = 20
    • StopLossPercent = 2м
    • CandleType = TimeSpan.FromMinutes(5)
  • Фильтры:
    • Категория: Пробой
    • Направление: Оба
    • Индикаторы: CCI, Momentum
    • Стопы: Да
    • Сложность: Базовая
    • Таймфрейм: Внутридневной (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 CCI (Commodity Channel Index) breakout.
/// Buys when CCI crosses above +100, sells when CCI crosses below -100.
/// Exits at zero and uses native actual-fill percent protection.
/// </summary>
public class CciBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _stopLossPercent;

	private decimal _prevCci;
	private bool _hasPrevValues;
	private Order _pendingOrder;

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

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

	public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }

	/// <summary>
	/// Initializes a new instance of the <see cref="CciBreakoutStrategy"/>.
	/// </summary>
	public CciBreakoutStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 20)
			.SetDisplay("CCI Period", "Period for CCI calculation", "Indicators")
			.SetOptimize(14, 30, 4);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
		_stopLossPercent = Param(nameof(StopLossPercent), 2m).SetNotNegative()
			.SetDisplay("Stop Loss (%)", "Actual-fill percent stop; zero disables it.", "Protection");
		OrderRegistering += order => _pendingOrder = order;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevCci = default;
		_hasPrevValues = default;
		_pendingOrder = null;
	}

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

		var cci = new CommodityChannelIndex { Length = CciPeriod };
		StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);
		foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
		{
			var quotes = new Subscription(DataType.Level1, Security);
			quotes.MarketData.BuildField = field;
			SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
		}

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

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

	private void ObserveProtectionQuote(Level1ChangeMessage quote)
	{
		// The high-level handler activates native protection before the callback, including between bars.
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (!_hasPrevValues)
		{
			_hasPrevValues = true;
			_prevCci = cciValue;
			return;
		}

		if (_pendingOrder is not null && _pendingOrder.State is not (OrderStates.Done or OrderStates.Failed))
		{
			_prevCci = cciValue;
			return;
		}

		// CCI crosses above +100 - buy signal
		if (_prevCci <= 100 && cciValue > 100 && Position <= 0)
		{
			var volume = Volume + Math.Abs(Position);
			BuyMarket(volume);
		}
		// CCI crosses below -100 - sell signal
		else if (_prevCci >= -100 && cciValue < -100 && Position >= 0)
		{
			var volume = Volume + Math.Abs(Position);
			SellMarket(volume);
		}
		else if (Position > 0m && cciValue <= 0m)
			SellMarket(Position);
		else if (Position < 0m && cciValue >= 0m)
			BuyMarket(Math.Abs(Position));

		_prevCci = cciValue;
	}
}