在 GitHub 上查看

Laguerre Filter

该策略交易 Laguerre 滤波线与短期 FIR 滤波线(对最近中间价的加权移动平均)之间的交叉。

  • Laguerre 滤波使用 Gamma 参数对价格进行平滑以减少噪声。
  • FIR 线是一个长度为 4 的对称加权移动平均。
  • 当 FIR 线曾位于 Laguerre 线上方并下穿时,策略开多单。
  • 当 FIR 线曾位于 Laguerre 线下方并上穿时,策略开空单。
  • 当两条线的相对位置反转时,关闭相反方向的仓位。
  • 每笔交易都使用按入场价百分比计算的止损。

这种均值回归方法尝试在价格偏离平滑的 Laguerre 曲线时捕捉回调。

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 based on Laguerre filter and WMA (FIR) crossover.
/// </summary>
public class LaguerreFilterStrategy : Strategy
{
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFir;
	private decimal? _prevLaguerre;

	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public LaguerreFilterStrategy()
	{
		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");

		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_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();
		_prevFir = _prevLaguerre = null;
	}

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

		var laguerre = new ExponentialMovingAverage { Length = 10 };
		var fir = new WeightedMovingAverage { Length = 4 };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(laguerre, fir, ProcessCandle)
			.Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

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

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

		if (_prevFir is null || _prevLaguerre is null)
		{
			_prevFir = firValue;
			_prevLaguerre = laguerreValue;
			return;
		}

		var firWasAbove = _prevFir > _prevLaguerre;
		var firIsAbove = firValue > laguerreValue;

		if (!firWasAbove && firIsAbove && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (firWasAbove && !firIsAbove && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFir = firValue;
		_prevLaguerre = laguerreValue;
	}
}