Ver no GitHub

CCI COMA

Uses Commodity Channel Index and multi-timeframe moving averages to follow the prevailing trend.

Details

  • Data: Price candles from multiple timeframes.
  • Entry: Long when CCI is above zero, RSI above 50, candle closes above open, and all monitored timeframes show an uptrend; short when opposite.
  • Exit: Position closes on the opposite signal.
  • Instruments: Any instruments.
  • Risk: No explicit stop loss or take profit.
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>
/// CCI and RSI crossover strategy with EMA trend filter.
/// </summary>
public class CciComaStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevCci;
	private bool _hasPrev;

	public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public CciComaStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("CCI Period", "CCI length", "Indicators");
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI length", "Indicators");
		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "Trend EMA period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevCci = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var cci = new CommodityChannelIndex { Length = CciPeriod };
		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var ema = new ExponentialMovingAverage { Length = EmaPeriod };

		SubscribeCandles(CandleType)
			.Bind(cci, rsi, ema, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevCci = cciVal;
			_hasPrev = true;
			return;
		}

		var bullish = cciVal > 0 && rsiVal > 50m && candle.ClosePrice > emaVal;
		var bearish = cciVal < 0 && rsiVal < 50m && candle.ClosePrice < emaVal;

		// CCI crossing zero
		var cciCrossUp = _prevCci <= 0 && cciVal > 0;
		var cciCrossDown = _prevCci >= 0 && cciVal < 0;

		if (cciCrossUp && bullish && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (cciCrossDown && bearish && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevCci = cciVal;
	}
}