在 GitHub 上查看

Laguerre CCI MA

该策略结合了 Laguerre 滤波器、商品通道指数(CCI)和指数移动平均线。

概述

  • Laguerre 滤波器在 0-1 范围内标识超买和超卖区域。
  • CCI 用于确认价格动量。
  • EMA 的斜率保证交易顺应主要趋势。

入场规则

  • 当 Laguerre 值为 0、EMA 上升且 CCI 低于负的 CciLevel 阈值时做多。
  • 当 Laguerre 值为 1、EMA 下降且 CCI 高于正的 CciLevel 阈值时做空。

出场规则

  • 当 Laguerre 值高于 0.9 时平仓多头。
  • 当 Laguerre 值低于 0.1 时平仓空头。

参数

  • LagGamma – Laguerre 滤波器的 gamma 参数。
  • CciPeriod – CCI 的周期。
  • CciLevel – 用于入场的 CCI 绝对阈值。
  • MaPeriod – 移动平均线的周期。
  • TakeProfit – 以绝对价格单位表示的止盈(可选)。
  • StopLoss – 以绝对价格单位表示的止损(可选)。
  • CandleType – 用于计算的蜡烛类型。

该策略仅处理已完成的蜡烛,并使用 StockSharp 的高级 API 绑定来获取指标数据。

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 combining CCI crossover with EMA trend filter.
/// </summary>
public class LaguerreCciMaStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevCci;
	private bool _hasPrev;

	public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LaguerreCciMaStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators");

		_maPeriod = Param(nameof(MaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Period for moving average", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "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 ma = new ExponentialMovingAverage { Length = MaPeriod };

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

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

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

		var close = candle.ClosePrice;

		// Buy: CCI crosses above 0 and price above MA
		if (_prevCci <= 0 && cciValue > 0 && close > maValue && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Sell: CCI crosses below 0 and price below MA
		else if (_prevCci >= 0 && cciValue < 0 && close < maValue && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevCci = cciValue;
	}
}