GitHub で見る

Kalman Filter トレンド戦略

このトレンドフォロー手法は、Kalman Filterを使用して価格変動を平滑化し、基本的な方向を推定します。フィルターは市場ノイズに動的に適応し、標準的な移動平均と比較してトレンドの強さをより精緻に把握できます。

テストによると、平均年間リターンは約112%です。外国為替市場で最も高いパフォーマンスを発揮します。

終値がKalman Filterの推定値を上回ると、ロングポジションが開かれます。逆に、終値がフィルター値を下回ると、ショートポジションが取られます。フィルターはすべてのバーで更新されるため、価格がラインを交差するたびにトレードが切り替わり、トレンド市場への継続的な参加が可能になります。

体系的なアプローチを好むトレーダーは、Kalman Filterがダマシを減らすのに役立つと感じるかもしれません。ATRベースの保護ストップにより、トレンドが急速に反転した場合のリスクを制限します。

詳細

  • エントリー条件:
    • ロング: 終値 > Kalman Filter
    • ショート: 終値 < Kalman Filter
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 終値 < Kalman Filter の時に決済
    • ショート: 終値 > Kalman Filter の時に決済
  • ストップ: あり、ATRベースのストップロス。
  • デフォルト値:
    • ProcessNoise = 0.01m
    • MeasurementNoise = 0.1m
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: トレンド
    • 方向: 両方
    • インジケーター: Kalman Filter
    • ストップ: あり
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Kalman Filter Trend strategy.
/// Uses a custom Kalman Filter indicator to track price trend.
/// </summary>
public class KalmanFilterTrendStrategy : Strategy
{
	private readonly StrategyParam<decimal> _processNoiseParam;
	private readonly StrategyParam<decimal> _measurementNoiseParam;
	private readonly StrategyParam<DataType> _candleTypeParam;

	private KalmanFilter _kalmanFilter;
	private AverageTrueRange _atr;

	/// <summary>
	/// Process noise coefficient for Kalman filter.
	/// </summary>
	public decimal ProcessNoise
	{
		get => _processNoiseParam.Value;
		set => _processNoiseParam.Value = value;
	}

	/// <summary>
	/// Measurement noise coefficient for Kalman filter.
	/// </summary>
	public decimal MeasurementNoise
	{
		get => _measurementNoiseParam.Value;
		set => _measurementNoiseParam.Value = value;
	}

	/// <summary>
	/// Candle type for strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleTypeParam.Value;
		set => _candleTypeParam.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public KalmanFilterTrendStrategy()
	{
		_processNoiseParam = Param(nameof(ProcessNoise), 0.01m)
			.SetRange(0.0001m, 1)
			.SetDisplay("Process Noise", "Process noise coefficient for Kalman filter", "Parameters")
			
			.SetOptimize(0.001m, 0.1m, 0.005m);

		_measurementNoiseParam = Param(nameof(MeasurementNoise), 0.1m)
			.SetRange(0.0001m, 1)
			.SetDisplay("Measurement Noise", "Measurement noise coefficient for Kalman filter", "Parameters")
			
			.SetOptimize(0.01m, 1.0m, 0.1m);

		_candleTypeParam = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for strategy", "Common");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_kalmanFilter = null;
		_atr = null;
	}

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

		// Create indicators
		_kalmanFilter = new KalmanFilter 
		{ 
			ProcessNoise = ProcessNoise,
			MeasurementNoise = MeasurementNoise 
		};
		
		_atr = new AverageTrueRange { Length = 14 };

		// Create subscription and bind indicators
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_kalmanFilter, _atr, ProcessCandle)
			.Start();

		// Setup chart visualization if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _kalmanFilter);
			DrawOwnTrades(area);
		}
		
		// Enable position protection
		StartProtection(
			takeProfit: new Unit(0, UnitTypes.Absolute), // No take profit
			stopLoss: new Unit(2, UnitTypes.Absolute) // Stop loss at 2*ATR
		);
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;
		
		// Calculate trend direction
		var trend = candle.ClosePrice > kalmanValue ? 1 : -1;
		
		// Trading logic based on price position relative to Kalman filter
		if (trend > 0 && Position <= 0)
		{
			// Buy when price is above Kalman filter (uptrend)
			BuyMarket(Volume + Math.Abs(Position));
		}
		else if (trend < 0 && Position >= 0)
		{
			// Sell when price is below Kalman filter (downtrend)
			SellMarket(Volume + Math.Abs(Position));
		}
	}
}