在 GitHub 上查看

CCI Normalized Reversal 策略

该策略利用商品通道指数(CCI)在指标离开极端区域后寻找反转信号。

概述

指标基于 8 小时K线计算,周期可配置。设置的两个阈值定义了超买和超卖区域。当 CCI 在达到极值后重新回到这些区域内部,策略将在相反方向开仓,期望价格回归均值。

交易规则

  • 做多:两根之前的K线 CCI 高于上阈值,上一根K线跌回阈值以下。
  • 做空:两根之前的K线 CCI 低于下阈值,上一根K线上穿该阈值。
  • 平多:上一根K线的 CCI 低于中间水平。
  • 平空:上一根K线的 CCI 高于中间水平。

参数

  • CciPeriod – CCI 计算周期。
  • HighLevel – 上阈值,表示超买区域。
  • MiddleLevel – 中间水平,用于退出头寸。
  • LowLevel – 下阈值,表示超卖区域。
  • CandleType – 计算所用的K线类型(默认 8 小时)。

备注

策略同一时间只持有一个头寸,并使用市价单。通过 StartProtection 启用默认风险管理。

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>
/// CCI Normalized Reversal Strategy.
/// Enters positions after the indicator leaves extreme zones.
/// </summary>
public class CciNormalizedReversalStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _highLevel;
	private readonly StrategyParam<int> _middleLevel;
	private readonly StrategyParam<int> _lowLevel;
	private readonly StrategyParam<DataType> _candleType;

	private int _prevColor = 2;
	private int _prevPrevColor = 2;

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

	/// <summary>
	/// Upper CCI threshold.
	/// </summary>
	public int HighLevel
	{
		get => _highLevel.Value;
		set => _highLevel.Value = value;
	}

	/// <summary>
	/// Middle CCI threshold.
	/// </summary>
	public int MiddleLevel
	{
		get => _middleLevel.Value;
		set => _middleLevel.Value = value;
	}

	/// <summary>
	/// Lower CCI threshold.
	/// </summary>
	public int LowLevel
	{
		get => _lowLevel.Value;
		set => _lowLevel.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="CciNormalizedReversalStrategy"/>.
	/// </summary>
	public CciNormalizedReversalStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 10)
			.SetDisplay("CCI Period", "Lookback period for CCI", "General")
			.SetRange(5, 50)
			;

		_highLevel = Param(nameof(HighLevel), 100)
			.SetDisplay("High Level", "Upper CCI threshold", "General")
			.SetRange(50, 200)
			;

		_middleLevel = Param(nameof(MiddleLevel), 0)
			.SetDisplay("Middle Level", "Middle CCI threshold", "General")
			.SetRange(-50, 50)
			;

		_lowLevel = Param(nameof(LowLevel), -100)
			.SetDisplay("Low Level", "Lower CCI threshold", "General")
			.SetRange(-200, -50)
			;

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

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

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

		_prevColor = 2;
		_prevPrevColor = 2;
	}

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

		_prevColor = 2;
		_prevPrevColor = 2;

		var cci = new CommodityChannelIndex { Length = CciPeriod };

		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(cci, ProcessCandle)
			.Start();
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var color = GetColorIndex(cciValue);

		// Close short when CCI rises above middle level
		if (_prevColor < 2 && Position < 0)
			BuyMarket();

		// Close long when CCI falls below middle level
		if (_prevColor > 2 && Position > 0)
			SellMarket();

		// Open long after leaving high zone
		if (_prevPrevColor == 0 && _prevColor > 0 && Position <= 0)
			BuyMarket();

		// Open short after leaving low zone
		if (_prevPrevColor == 4 && _prevColor < 4 && Position >= 0)
			SellMarket();

		_prevPrevColor = _prevColor;
		_prevColor = color;
	}

	private int GetColorIndex(decimal cci)
	{
		if (cci > MiddleLevel)
			return cci > HighLevel ? 0 : 1;
		if (cci < MiddleLevel)
			return cci < LowLevel ? 4 : 3;
		return 2;
	}
}