在 GitHub 上查看

Super Woodies CCI 策略

该策略是原始 MQL5 Exp_SuperWoodiesCCI 专家的移植版本。它基于较高周期上的商品通道指数(CCI)方向进行交易。

逻辑

  • 计算具有可配置周期的 CCI。
  • 当 CCI 上穿零轴时:
    • 可选择平掉空头头寸。
    • 可选择开立多头头寸。
  • 当 CCI 下穿零轴时:
    • 可选择平掉多头头寸。
    • 可选择开立空头头寸。

策略只处理已完成的K线,并在指定的 K 线类型上运行。

参数

  • CciPeriod – CCI 的计算周期。
  • CandleType – 要分析的 K 线时间框架。
  • AllowLongEntry – 是否允许开多。
  • AllowShortEntry – 是否允许开空。
  • AllowLongExit – 当 CCI 为负时是否允许平多。
  • AllowShortExit – 当 CCI 为正时是否允许平空。

说明

该策略使用 StockSharp 高级 API,通过 SubscribeCandles 订阅数据并绑定指标,交易使用 BuyMarketSellMarket 方法管理头寸。

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>
/// Super Woodies CCI strategy.
/// Opens long when CCI crosses above zero and short when crosses below.
/// </summary>
public class SuperWoodiesCciStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<bool> _allowLongEntry;
	private readonly StrategyParam<bool> _allowShortEntry;
	private readonly StrategyParam<bool> _allowLongExit;
	private readonly StrategyParam<bool> _allowShortExit;

	private CommodityChannelIndex _cci = null!;
	private bool _hasPrev;
	private bool _wasPositive;

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

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

	/// <summary>
	/// Allow opening long positions.
	/// </summary>
	public bool AllowLongEntry
	{
		get => _allowLongEntry.Value;
		set => _allowLongEntry.Value = value;
	}

	/// <summary>
	/// Allow opening short positions.
	/// </summary>
	public bool AllowShortEntry
	{
		get => _allowShortEntry.Value;
		set => _allowShortEntry.Value = value;
	}

	/// <summary>
	/// Allow closing long positions when CCI goes below zero.
	/// </summary>
	public bool AllowLongExit
	{
		get => _allowLongExit.Value;
		set => _allowLongExit.Value = value;
	}

	/// <summary>
	/// Allow closing short positions when CCI goes above zero.
	/// </summary>
	public bool AllowShortExit
	{
		get => _allowShortExit.Value;
		set => _allowShortExit.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="SuperWoodiesCciStrategy"/>.
	/// </summary>
	public SuperWoodiesCciStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("CCI Period", "CCI lookback length", "General")
			
			.SetOptimize(20, 100, 10);

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

		_allowLongEntry = Param(nameof(AllowLongEntry), true)
			.SetDisplay("Allow Long Entry", "Enable opening long positions", "Trading");

		_allowShortEntry = Param(nameof(AllowShortEntry), true)
			.SetDisplay("Allow Short Entry", "Enable opening short positions", "Trading");

		_allowLongExit = Param(nameof(AllowLongExit), true)
			.SetDisplay("Allow Long Exit", "Enable closing long positions", "Trading");

		_allowShortExit = Param(nameof(AllowShortExit), true)
			.SetDisplay("Allow Short Exit", "Enable closing short positions", "Trading");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_hasPrev = false;
		_wasPositive = false;
		_cci = null!;
	}

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

		_cci = new CommodityChannelIndex { Length = CciPeriod };

		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 ProcessCandle(ICandleMessage candle, decimal cciValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_cci.IsFormed || !IsFormedAndOnlineAndAllowTrading())
			return;

		var isPositive = cciValue > 0m;

		if (_hasPrev && isPositive != _wasPositive)
		{
			if (isPositive)
			{
				if (AllowLongEntry && Position <= 0)
				{
					if (Position < 0)
						BuyMarket();
					BuyMarket();
				}
			}
			else
			{
				if (AllowShortEntry && Position >= 0)
				{
					if (Position > 0)
						SellMarket();
					SellMarket();
				}
			}
		}
		else
		{
			if (isPositive)
			{
				if (AllowShortExit && Position < 0)
					BuyMarket();
			}
			else
			{
				if (AllowLongExit && Position > 0)
					SellMarket();
			}
		}

		_wasPositive = isPositive;
		_hasPrev = true;
	}
}