在 GitHub 上查看

Bezier标准差策略

该策略使用标准差指标捕捉波动率的转折点。指标出现局部低点时,意味着波动率可能向上扩张,系统买入做多;出现局部高点时,预期波动率收缩而卖出做空。

默认使用四小时K线,可同时进行多头和空头交易。策略不设置止损,完全依靠反向信号退出。

详细信息

  • 入场条件
    • 做多:上一根K线的标准差值低于相邻两值(局部最低)。
    • 做空:上一根K线的标准差值高于相邻两值(局部最高)。
  • 多/空:双向。
  • 出场条件
    • 反向信号触发反手。
  • 止损:无。
  • 默认参数
    • StdDev Period = 9。
    • Candle Type = 四小时K线。
  • 过滤器
    • 类型:均值回归
    • 方向:双向
    • 指标:标准差
    • 止损:无
    • 复杂度:简单
    • 时间框架:中期
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// Strategy based on standard deviation turning points.
/// Opens long at local minima and short at local maxima of the indicator.
/// </summary>
public class BezierStDevStrategy : Strategy
{
	private readonly StrategyParam<int> _stdDevPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevValue1;
	private decimal _prevValue2;

	/// <summary>
	/// Standard deviation calculation period.
	/// </summary>
	public int StdDevPeriod
	{
		get => _stdDevPeriod.Value;
		set => _stdDevPeriod.Value = value;
	}

	/// <summary>
	/// Type of candles used for calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public BezierStDevStrategy()
	{
		_stdDevPeriod = Param(nameof(StdDevPeriod), 9)
			.SetGreaterThanZero()
			.SetDisplay("StdDev Period", "Period for standard deviation calculation", "General")
			
			.SetOptimize(5, 20, 1);

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

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

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

		_prevValue1 = 0m;
		_prevValue2 = 0m;
	}

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

		var stdDev = new StandardDeviation { Length = StdDevPeriod };

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

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

	private void ProcessCandle(ICandleMessage candle, decimal stdDevValue)
	{
		// We only work with finished candles.
		if (candle.State != CandleStates.Finished)
			return;

		// Ensure the strategy is ready for trading.
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		// Check for local minima and maxima at the previous value.
		if (_prevValue2 != 0m)
		{
			var isLocalMin = _prevValue1 < _prevValue2 && _prevValue1 < stdDevValue;
			var isLocalMax = _prevValue1 > _prevValue2 && _prevValue1 > stdDevValue;

			if (isLocalMin)
			{
				if (Position <= 0)
					BuyMarket();
			}
			else if (isLocalMax)
			{
				if (Position >= 0)
					SellMarket();
			}
		}

		// Shift stored values for next calculation.
		_prevValue2 = _prevValue1;
		_prevValue1 = stdDevValue;
	}
}