在 GitHub 上查看

Weighted Harrell-Davis Quantile Estimator with AbsoluteDeviation 策略

该策略使用基于中位数的分位估计和绝对偏差带来检测价格异常。 当收盘价跌破下轨时买入,涨破上轨时卖出。

细节

  • 入场条件: 收盘价低于下轨或高于上轨
  • 多空方向: 双向
  • 出场条件: 穿越相反的轨道
  • 止损: 无
  • 默认值:
    • Length = 39
    • DeviationMultiplier = 1.213
  • 过滤器:
    • 分类: 均值回归
    • 方向: 双向
    • 指标: Median
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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>
/// Quantile estimator strategy with deviation bands.
/// Uses SMA + StdDev as Bollinger-like bands for mean reversion.
/// Buys when price drops below the lower band, sells when above upper band.
/// </summary>
public class WeightedHarrellDavisQuantileEstimatorWithAbsoluteDeviationStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<decimal> _devMult;
	private readonly StrategyParam<DataType> _candleType;

	public int Length { get => _length.Value; set => _length.Value = value; }
	public decimal DevMult { get => _devMult.Value; set => _devMult.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public WeightedHarrellDavisQuantileEstimatorWithAbsoluteDeviationStrategy()
	{
		_length = Param(nameof(Length), 39)
			.SetGreaterThanZero()
			.SetDisplay("Length", "Lookback period", "General");

		_devMult = Param(nameof(DevMult), 1.5m)
			.SetGreaterThanZero()
			.SetDisplay("Deviation Mult", "Band multiplier", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var sma = new SimpleMovingAverage { Length = Length };
		var stdDev = new StandardDeviation { Length = Length };

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

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

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

		if (stdVal <= 0)
			return;

		var upper = smaVal + DevMult * stdVal;
		var lower = smaVal - DevMult * stdVal;

		if (candle.ClosePrice > upper && Position >= 0)
			SellMarket();
		else if (candle.ClosePrice < lower && Position <= 0)
			BuyMarket();
	}
}