Auf GitHub ansehen

Williams %R-Strategie

Die Strategie kauft, wenn Williams %R in tief überverkauftes Terrain fällt, und steigt beim Ausbruch nach oben oder bei überkauften Niveaus aus.

Details

  • Einstiegskriterien: %R < -90
  • Long/Short: Nur Long
  • Ausstiegskriterien: Schlusskurs > vorheriges Hoch oder %R > -30
  • Stops: Nein
  • Standardwerte:
    • LookbackPeriod = 2
  • Filter:
    • Kategorie: Oszillator
    • Richtung: Long
    • Indikatoren: WilliamsR
    • Stops: Nein
    • Komplexität: Grundlegend
    • Zeitrahmen: Intraday
    • Saisonalität: Nein
    • Neuronale Netze: Nein
    • Divergenz: Nein
    • Risikolevel: Mittel
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>
/// Williams %R strategy using RSI as proxy.
/// Enters long on oversold RSI, exits on breakout above previous high or RSI overbought.
/// </summary>
public class WilliamsRStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<decimal> _oversold;
	private readonly StrategyParam<decimal> _overbought;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHigh;

	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public decimal Oversold { get => _oversold.Value; set => _oversold.Value = value; }
	public decimal Overbought { get => _overbought.Value; set => _overbought.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public WilliamsRStrategy()
	{
		_rsiLength = Param(nameof(RsiLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Length", "RSI period", "General");

		_oversold = Param(nameof(Oversold), 25m)
			.SetDisplay("Oversold", "Oversold level", "General");

		_overbought = Param(nameof(Overbought), 75m)
			.SetDisplay("Overbought", "Overbought/exit level", "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 OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiLength };

		_prevHigh = 0;

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

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

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

		var longSignal = rsiVal < Oversold;
		var exitSignal = (_prevHigh > 0 && candle.ClosePrice > _prevHigh) || rsiVal > Overbought;

		if (longSignal && Position <= 0)
			BuyMarket();
		else if (exitSignal && Position > 0)
			SellMarket();

		_prevHigh = candle.HighPrice;
	}
}