在 GitHub 上查看

Kalman Filter Signal 策略

该策略使用卡尔曼滤波器来识别方向变化。根据选择的信号模式,信号由价格与滤波值的关系或滤波器斜率决定。当信号变为看涨时开多单,信号变为看跌时开空单。出现反向信号时反转仓位。止损和止盈使用绝对数值。

详情

  • 入场条件:
    • 多单:信号转为看涨
    • 空单:信号转为看跌
  • 多空方向: 都可以
  • 出场条件: 反向信号
  • 止损: 绝对止损和止盈
  • 默认值:
    • ProcessNoise = 1.0
    • MeasurementNoise = 1.0
    • CandleType = TimeSpan.FromHours(3).TimeFrame()
    • Mode = SignalModes.Kalman
    • StopLoss = 1000m
    • TakeProfit = 2000m
  • 过滤器:
    • 类别: Trend following
    • 方向: 双向
    • 指标: Kalman Filter
    • 止损: 是
    • 复杂度: 中等
    • 周期: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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 Kalman Filter direction signals.
/// Opens long when price crosses above filter, short when below.
/// </summary>
public class KalmanFilterSignalStrategy : Strategy
{
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFilter;
	private decimal? _prevSignal;

	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 KalmanFilterSignalStrategy()
	{
		_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", "Timeframe for calculations", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFilter = null;
		_prevSignal = null;
	}

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

		var kalman = new KalmanFilter();

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

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

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

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

		var signal = candle.ClosePrice > filterValue ? 1m : 0m;

		if (_prevSignal.HasValue && signal != _prevSignal.Value)
		{
			if (signal > 0 && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (signal == 0 && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prevFilter = filterValue;
		_prevSignal = signal;
	}
}