Kalman Filter Signal 策略
该策略使用卡尔曼滤波器来识别方向变化。根据选择的信号模式,信号由价格与滤波值的关系或滤波器斜率决定。当信号变为看涨时开多单,信号变为看跌时开空单。出现反向信号时反转仓位。止损和止盈使用绝对数值。
详情
- 入场条件:
- 多单:信号转为看涨
- 空单:信号转为看跌
- 多空方向: 都可以
- 出场条件: 反向信号
- 止损: 绝对止损和止盈
- 默认值:
ProcessNoise= 1.0MeasurementNoise= 1.0CandleType= TimeSpan.FromHours(3).TimeFrame()Mode= SignalModes.KalmanStopLoss= 1000mTakeProfit= 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;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import KalmanFilter
from StockSharp.Algo.Strategies import Strategy
class kalman_filter_signal_strategy(Strategy):
def __init__(self):
super(kalman_filter_signal_strategy, self).__init__()
self._stop_loss_pct = self.Param("StopLossPct", 2.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._take_profit_pct = self.Param("TakeProfitPct", 3.0) \
.SetDisplay("Take Profit %", "Take profit percentage", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for calculations", "General")
self._prev_filter = None
self._prev_signal = None
@property
def stop_loss_pct(self):
return self._stop_loss_pct.Value
@property
def take_profit_pct(self):
return self._take_profit_pct.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(kalman_filter_signal_strategy, self).OnReseted()
self._prev_filter = None
self._prev_signal = None
def OnStarted2(self, time):
super(kalman_filter_signal_strategy, self).OnStarted2(time)
kalman = KalmanFilter()
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(kalman, self.process_candle).Start()
self.StartProtection(
stopLoss=Unit(self.stop_loss_pct, UnitTypes.Percent),
takeProfit=Unit(self.take_profit_pct, UnitTypes.Percent),
useMarketOrders=True)
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, kalman)
self.DrawOwnTrades(area)
def process_candle(self, candle, filter_value):
if candle.State != CandleStates.Finished:
return
filter_value = float(filter_value)
signal = 1.0 if float(candle.ClosePrice) > filter_value else 0.0
if self._prev_signal is not None and signal != self._prev_signal:
if signal > 0 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif signal == 0 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_filter = filter_value
self._prev_signal = signal
def CreateClone(self):
return kalman_filter_signal_strategy()