在 GitHub 上查看

随机指标超买/超卖反转

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

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

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

当 %K 线重新穿过 50 水平时仓位平掉,表明动能转向另一方。

细节

  • 入场条件:
    • 多头:%K < 20 后转为上行。
    • 空头:%K > 80 后转为下行。
  • 多/空:双向。
  • 退出条件:%K 穿越 50 或止损。
  • 止损:有,距离为 2%。
  • 默认值:
    • StochPeriod = 14
    • KPeriod = 3
    • DPeriod = 3
    • CandleType = 5 分钟
  • 过滤条件:
    • 类别: 振荡指标
    • 方向: 双向
    • 指标: 随机指标
    • 止损: 有
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险级别: 中等
using System;
using System.Collections.Generic;
using System.Linq;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Fades confirmed stochastic extremes and exits on the return to neutral.
/// The native oscillator's D is the smoothed %K; a second SMA forms %D.
/// </summary>
public class StochasticOverboughtOversoldStrategy : Strategy
{
	private const decimal ComparisonTolerance = 0.00000001m;
	private readonly StrategyParam<int> _stochPeriod;
	private readonly StrategyParam<int> _kPeriod;
	private readonly StrategyParam<int> _dPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _stopLossPercent;

	private readonly Queue<decimal> _dWindow = new();
	private decimal? _previousK;
	private Order _pendingOrder;

	public int StochPeriod { get => _stochPeriod.Value; set => _stochPeriod.Value = value; }
	public int KPeriod { get => _kPeriod.Value; set => _kPeriod.Value = value; }
	public int DPeriod { get => _dPeriod.Value; set => _dPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }

	public StochasticOverboughtOversoldStrategy()
	{
		_stochPeriod = Param(nameof(StochPeriod), 14).SetGreaterThanZero()
			.SetDisplay("Stochastic Period", "Raw high/low lookback", "Indicators");
		_kPeriod = Param(nameof(KPeriod), 3).SetGreaterThanZero()
			.SetDisplay("K Period", "SMA length of raw %K", "Indicators");
		_dPeriod = Param(nameof(DPeriod), 3).SetGreaterThanZero()
			.SetDisplay("D Period", "SMA length of smoothed %K", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Stochastic timeframe", "General");
		_stopLossPercent = Param(nameof(StopLossPercent), 2m).SetNotNegative()
			.SetDisplay("Stop Loss (%)", "Actual-fill percent stop; zero disables it.", "Protection");
		OrderRegistering += order => _pendingOrder = order;
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType), (Security, DataType.Level1)];

	protected override void OnReseted()
	{
		base.OnReseted();
		ClearState();
	}

	private void ClearState()
	{
		_dWindow.Clear();
		_previousK = null;
		_pendingOrder = null;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		ClearState();
		StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);
		foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
		{
			var quotes = new Subscription(DataType.Level1, Security);
			quotes.MarketData.BuildField = field;
			SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
		}
		var stochastic = new StochasticOscillator
		{
			K = { Length = StochPeriod },
			D = { Length = KPeriod },
		};
		var candles = SubscribeCandles(CandleType);
		candles.BindEx(stochastic, ProcessCandle).Start();
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, candles);
			DrawIndicator(area, stochastic);
			DrawOwnTrades(area);
		}
	}

	private void ObserveProtectionQuote(Level1ChangeMessage quote)
	{
		// Native actual-fill protection evaluates executable quotes between signal candles.
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue output)
	{
		if (candle.State != CandleStates.Finished || !output.Indicator.IsFormed ||
			output is not StochasticOscillatorValue value || value.D is not decimal k)
			return;

		_dWindow.Enqueue(k);
		if (_dWindow.Count > DPeriod)
			_dWindow.Dequeue();
		if (_dWindow.Count < DPeriod)
			return;

		var d = _dWindow.Average();
		var previous = _previousK;
		_previousK = k;
		if (previous is not decimal prior || !IsFormedAndOnlineAndAllowTrading() ||
			_pendingOrder is not null && _pendingOrder.State is not (OrderStates.Done or OrderStates.Failed))
			return;

		// The native rolling SMA can leave a sub-ulp remainder at exact levels (e.g. 50).
		if (Position == 0m && prior < 20m - ComparisonTolerance &&
			k > prior + ComparisonTolerance && k > d + ComparisonTolerance)
			BuyMarket(Volume);
		else if (Position == 0m && prior > 80m + ComparisonTolerance &&
			k < prior - ComparisonTolerance && k < d - ComparisonTolerance)
			SellMarket(Volume);
		// %K crossing 50 either way closes, so a position whose entry bar already passed 50 exits on the cross back.
		else if (Position > 0m && (prior < 50m - ComparisonTolerance) != (k < 50m - ComparisonTolerance))
			SellMarket(Position);
		else if (Position < 0m && (prior > 50m + ComparisonTolerance) != (k > 50m + ComparisonTolerance))
			BuyMarket(Math.Abs(Position));
	}
}