在 GitHub 上查看

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;
		}
	}
}