在 GitHub 上查看

CCI Woodies 策略

概述

该策略基于 Woodies CCI 方法的两条 CCI 线(快线和慢线)的交叉进行交易。在指定的时间框架上计算两条 CCI。当快线从上向下穿过慢线时,策略开多仓并平掉可能存在的空仓;当快线从下向上穿过慢线时,策略开空仓并平掉可能存在的多仓。

参数

  • FastPeriod:快速 CCI 的周期。
  • SlowPeriod:慢速 CCI 的周期。
  • CandleType:用于计算的 K 线类型(时间框架)。
  • InvertSignals:为 true 时,买卖逻辑反向。
  • TakeProfitPoints:以价格点表示的止盈。
  • StopLossPoints:以价格点表示的止损。

说明

策略使用 StockSharp 的高级 API,通过 SubscribeCandles 订阅 K 线,并通过 Bind 绑定指标。止盈和止损由 StartProtection 管理。

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 Woodies crossover strategy.
/// Buys when the fast CCI crosses below the slow CCI and sells on the opposite crossover.
/// </summary>
public class CciWoodiesStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _isInitialized;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public CciWoodiesStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 6)
			.SetDisplay("Fast CCI Period", "Period for fast CCI", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 14)
			.SetDisplay("Slow CCI Period", "Period for slow CCI", "Indicators");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_isInitialized = false;
	}

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

		_prevFast = 0;
		_prevSlow = 0;
		_isInitialized = false;

		var fastCci = new CommodityChannelIndex { Length = FastPeriod };
		var slowCci = new CommodityChannelIndex { Length = SlowPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (!_isInitialized)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_isInitialized = true;
			return;
		}

		var crossDown = _prevFast > _prevSlow && fast <= slow;
		var crossUp = _prevFast < _prevSlow && fast >= slow;

		if (crossDown && Position <= 0)
			BuyMarket();
		else if (crossUp && Position >= 0)
			SellMarket();

		_prevFast = fast;
		_prevSlow = slow;
	}
}