Liquidity Sweep Filter 策略
该趋势策略使用布林带判断市场方向,并结合成交量监测潜在的流动性扫单。当趋势从空转多或从多转空时,根据交易模式开仓。
详情
- 入场条件:
- 多头: 趋势转为看涨且模式允许做多。
- 空头: 趋势转为看跌且模式允许做空。
- 多/空: 由交易模式决定。
- 出场条件:
- 多头: 趋势转为看跌或模式禁止做多。
- 空头: 趋势转为看涨或模式禁止做空。
- 止损: 无。
- 默认值:
Length= 12。Multiplier= 2.0。Major Sweep Threshold= 50。
- 筛选:
- 分类: 趋势
- 方向: 双向
- 指标: 多个
- 止损: 否
- 复杂度: 中等
- 时间框架: 任意
- 季节性: 否
- 神经网络: 否
- 背离: 否
- 风险等级: 中等
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Liquidity Sweep Filter strategy based on Bollinger bands.
/// </summary>
public class LiquiditySweepFilterStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _length;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<int> _cooldownBars;
private SimpleMovingAverage _sma;
private StandardDeviation _stdDev;
private int _trend;
private int _barsSinceSignal;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int Length
{
get => _length.Value;
set => _length.Value = value;
}
public decimal Multiplier
{
get => _multiplier.Value;
set => _multiplier.Value = value;
}
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
public LiquiditySweepFilterStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
_length = Param(nameof(Length), 20)
.SetGreaterThanZero()
.SetDisplay("Length", "Base period", "Trend");
_multiplier = Param(nameof(Multiplier), 0.3m)
.SetGreaterThanZero()
.SetDisplay("Multiplier", "Band width multiplier", "Trend");
_cooldownBars = Param(nameof(CooldownBars), 20)
.SetDisplay("Cooldown Bars", "Min bars between signals", "General");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_trend = 0;
_barsSinceSignal = 0;
_sma = new SimpleMovingAverage { Length = Length };
_stdDev = new StandardDeviation { Length = Length };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_sma, _stdDev, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_sma = null;
_stdDev = null;
_trend = 0;
_barsSinceSignal = 0;
}
private void ProcessCandle(ICandleMessage candle, decimal smaVal, decimal stdVal)
{
if (candle.State != CandleStates.Finished)
return;
_barsSinceSignal++;
if (!_sma.IsFormed || !_stdDev.IsFormed)
return;
var upper = smaVal + Multiplier * stdVal;
var lower = smaVal - Multiplier * stdVal;
var prevTrend = _trend;
// Determine trend based on band crossover with reset at SMA
if (candle.ClosePrice > upper)
_trend = 1;
else if (candle.ClosePrice < lower)
_trend = -1;
else
_trend = 0;
if (_barsSinceSignal < CooldownBars)
return;
if (prevTrend != 1 && _trend == 1 && Position <= 0)
{
BuyMarket(Volume + Math.Abs(Position));
_barsSinceSignal = 0;
}
else if (prevTrend != -1 && _trend == -1 && Position >= 0)
{
SellMarket(Volume + Math.Abs(Position));
_barsSinceSignal = 0;
}
}
}
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
from StockSharp.Algo.Indicators import SimpleMovingAverage, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class liquidity_sweep_filter_strategy(Strategy):
"""
Liquidity sweep filter based on SMA +/- StdDev bands.
Buys on breakout above upper band, sells on break below lower.
"""
def __init__(self):
super(liquidity_sweep_filter_strategy, self).__init__()
self._length = self.Param("Length", 20) \
.SetDisplay("Length", "Base period", "Trend")
self._multiplier = self.Param("Multiplier", 0.3) \
.SetDisplay("Multiplier", "Band width multiplier", "Trend")
self._cooldown_bars = self.Param("CooldownBars", 20) \
.SetDisplay("Cooldown Bars", "Min bars between signals", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._trend = 0
self._bars_since_signal = 0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(liquidity_sweep_filter_strategy, self).OnReseted()
self._trend = 0
self._bars_since_signal = 0
def OnStarted2(self, time):
super(liquidity_sweep_filter_strategy, self).OnStarted2(time)
self._sma = SimpleMovingAverage()
self._sma.Length = self._length.Value
self._std_dev = StandardDeviation()
self._std_dev.Length = self._length.Value
self._trend = 0
self._bars_since_signal = 0
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._sma, self._std_dev, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle, sma_val, std_val):
if candle.State != CandleStates.Finished:
return
self._bars_since_signal += 1
if not self._sma.IsFormed or not self._std_dev.IsFormed:
return
sma = float(sma_val)
std = float(std_val)
mult = self._multiplier.Value
upper = sma + mult * std
lower = sma - mult * std
close = float(candle.ClosePrice)
prev_trend = self._trend
if close > upper:
self._trend = 1
elif close < lower:
self._trend = -1
else:
self._trend = 0
if self._bars_since_signal < self._cooldown_bars.Value:
return
if prev_trend != 1 and self._trend == 1 and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
self._bars_since_signal = 0
elif prev_trend != -1 and self._trend == -1 and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
self._bars_since_signal = 0
def CreateClone(self):
return liquidity_sweep_filter_strategy()