Открыть на GitHub

Стратегия Williams %R

Стратегия покупает, когда Williams %R падает в глубокую перепроданность, и выходит при пробое вверх или переходе в перекупленность.

Детали

  • Критерий входа: %R < -90
  • Лонг/Шорт: только лонг
  • Критерий выхода: закрытие выше предыдущего максимума или %R > -30
  • Стопы: нет
  • Значения по умолчанию:
    • LookbackPeriod = 2
  • Фильтры:
    • Категория: Осциллятор
    • Направление: Лонг
    • Индикаторы: WilliamsR
    • Стопы: Нет
    • Сложность: Базовая
    • Таймфрейм: Внутридневной
    • Сезонность: Нет
    • Нейросети: Нет
    • Дивергенция: Нет
    • Уровень риска: Средний
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;
	}
}