在 GitHub 上查看

随机指标超买/超卖反转

该策略针对随机振荡器的极端读数做出反应。当 %K 线进入超卖区域时,系统预期会出现反弹;相反,超买读数可能预示下跌。该方法运行在较短的日内周期上,因此信号生成较快。

测试表明年均收益约为 73%,该策略在加密市场表现最佳。

在选定的时间框架上订阅后,算法监控 %K 和 %D 线。当 %K 跌破 20 后开始回升,形成看涨形态;若 %K 升破 80 后开始下行,则为看跌形态。固定百分比的止损控制双向风险。

当 %K 线重新穿过 50 水平时仓位平掉,表明动能转向另一方。由于止损与最新 ATR 相匹配,头寸规模会随波动性而调整。

细节

  • 入场条件
    • 多头%K < 20 后转为上行。
    • 空头%K > 80 后转为下行。
  • 多/空:双向。
  • 退出条件:%K 穿越 50 或止损。
  • 止损:有,距离为 2%
  • 默认值
    • StochPeriod = 14
    • KPeriod = 3
    • DPeriod = 3
    • CandleType = 5 分钟
  • 过滤条件
    • 类别: 振荡指标
    • 方向: 双向
    • 指标: 随机指标
    • 止损: 有
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险级别: 中等
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>
/// Stochastic Overbought/Oversold strategy.
/// Buys when K is oversold, sells when K is overbought.
/// </summary>
public class StochasticOverboughtOversoldStrategy : Strategy
{
	private readonly StrategyParam<int> _kPeriod;
	private readonly StrategyParam<int> _dPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private int _cooldown;

	/// <summary>
	/// K period.
	/// </summary>
	public int KPeriod
	{
		get => _kPeriod.Value;
		set => _kPeriod.Value = value;
	}

	/// <summary>
	/// D period.
	/// </summary>
	public int DPeriod
	{
		get => _dPeriod.Value;
		set => _dPeriod.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Cooldown bars.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public StochasticOverboughtOversoldStrategy()
	{
		_kPeriod = Param(nameof(KPeriod), 3)
			.SetGreaterThanZero()
			.SetDisplay("K Period", "Smoothing period for %K", "Indicators");

		_dPeriod = Param(nameof(DPeriod), 3)
			.SetGreaterThanZero()
			.SetDisplay("D Period", "Smoothing period for %D", "Indicators");

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

		_cooldownBars = Param(nameof(CooldownBars), 500)
			.SetRange(1, 1000)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_cooldown = default;
	}

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

		_cooldown = 0;

		var stochastic = new StochasticOscillator
		{
			K = { Length = KPeriod },
			D = { Length = DPeriod },
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(stochastic, ProcessCandle)
			.Start();

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

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue stochValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!stochValue.IsFormed)
			return;

		var stochTyped = (StochasticOscillatorValue)stochValue;

		if (stochTyped.K is not decimal kValue)
			return;

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		if (Position == 0 && kValue < 20)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		else if (Position == 0 && kValue > 80)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position > 0 && kValue > 80)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position < 0 && kValue < 20)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
	}
}