Auf GitHub ansehen

Gemittelte Stoch & WPR-Strategie

Diese Strategie kombiniert den Stochastic-Oszillator mit Williams %R, um extreme Marktbedingungen zu erkennen. Eine Long-Position wird eröffnet, wenn der Stochastic-Wert unter 0.1 fällt und Williams %R unter -90 liegt, was starken Überverkaufsdruck signalisiert. Eine Short-Position wird eröffnet, wenn der Stochastic über 99.9 steigt und Williams %R -5 überschreitet, was starke Überkauftbedingungen anzeigt.

Die Strategie funktioniert mit jedem Instrument und Zeitrahmen, der vom gewählten Kerzentyp unterstützt wird. Sie kann sowohl Long- als auch Short-Positionen handeln und bietet einen optionalen prozentualen Stop-Loss für das Risikomanagement.

Details

  • Einstiegskriterien:
    • Long: Stochastic < 0.1 und Williams %R < -90.
    • Short: Stochastic > 99.9 und Williams %R > -5.
  • Long/Short: Beide.
  • Ausstiegskriterien: Gegensignal oder ausgelöster Stop-Loss.
  • Stops: Optionaler prozentualer Stop-Loss.
  • Indikatoren:
    • Stochastic-Oszillator (Standardperiode 26).
    • Williams %R (Standardperiode 26).

Parameter

  • StochPeriod – Berechnungsperiode des Stochastic.
  • WprPeriod – Berechnungsperiode von Williams %R.
  • StopLossPercent – Größe des prozentualen Stop-Loss.
  • CandleType – Kerzentyp für Indikatorberechnungen.
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>
/// Strategy using RSI and Williams %R for oversold/overbought entries.
/// </summary>
public class AveragedStochWprStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _wprPeriod;
	private readonly StrategyParam<DataType> _candleType;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public int WprPeriod { get => _wprPeriod.Value; set => _wprPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AveragedStochWprStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI period", "Indicators");
		_wprPeriod = Param(nameof(WprPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("WPR Period", "Williams %R period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var wpr = new WilliamsR { Length = WprPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(rsi, wpr, ProcessCandle)
			.Start();
	}

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

		// Buy: RSI oversold + WPR oversold
		if (rsi < 30 && wpr < -80 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Sell: RSI overbought + WPR overbought
		else if (rsi > 70 && wpr > -20 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
		// Exit
		else if (Position > 0 && rsi > 65)
			SellMarket();
		else if (Position < 0 && rsi < 35)
			BuyMarket();
	}
}