Auf GitHub ansehen

Vlado Strategy

Momentum reversal strategy based on the classic Larry Williams %R oscillator. The system waits for the oscillator to reach deep oversold or overbought readings and then reverses the position at the next completed bar. The StockSharp port keeps the discreti tionary flavour of the original MetaTrader implementation while exposing every important setting as a parameter.

Overview

  • Category: Mean-reversion oscillator strategy.
  • Market: Any liquid instrument that delivers stable candle data (forex pairs, index futures, crypto spot pairs).
  • Timeframe: Configurable via CandleType. Defaults to 1-hour candles, matching the original usage example.
  • Direction: Long and short. The engine always holds at most one position and flips when the opposite signal appears.
  • Indicator: Williams %R with configurable lookback length and threshold levels.

How It Works

  1. Subscribes to the selected candle feed and calculates Williams %R on each finished candle.
  2. Uses the default oversold level of -75 and overbought level of -25 (values are negative because of the oscillator scale).
  3. When %R falls below the oversold level the strategy enters or reverses into a long position.
  4. When %R rises above the overbought level the strategy enters or reverses into a short position.
  5. Orders are sized with Volume + Math.Abs(Position) so a reversal closes the existing position and opens the new one in a singl e market order.
  6. No explicit stop-loss or take-profit is used. Risk is controlled by the indicator levels and chosen timeframe.
  7. Every action is logged through LogInfo, making it easy to audit trades in the StockSharp GUI or log files.

Parameters

  • WilliamsPeriod: Number of candles used to compute the oscillator. Higher values smooth the signal, lower values react faster.
  • OverboughtLevel: Threshold that defines when the market is considered overbought (default -25). Can be optimised.
  • OversoldLevel: Threshold that defines when the market is considered oversold (default -75). Can be optimised.
  • CandleType: Candle type and timeframe applied to all calculations. Works with time frames, volume candles, or range bars.
  • Volume (inherited from Strategy): Defines the base order size. Adjust to match account size and risk appetite.

Trading Rules

  • Long Entry: Triggered when %R <= OversoldLevel and the current position is flat or short.
  • Short Entry: Triggered when %R >= OverboughtLevel and the current position is flat or long.
  • Exit: Performed implicitly by the reverse order when an opposite signal appears.
  • Position Management: Always a single open position. The algorithm does not pyramid or scale out.

Additional Notes

  • Works best in range-bound or slow-trending markets where oscillators can cycle between extremes.
  • Combining the strategy with external risk controls (equity stops, session filters) is recommended for live trading.
  • The implementation includes chart rendering: the main area shows candles and trades, while a secondary pane plots Williams %R.
  • Designed for further research: each parameter supports optimisation within StockSharp optimisers.
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>
/// Vlado momentum strategy using EMA crossover.
/// Buys when fast EMA crosses above slow EMA, sells on reverse.
/// </summary>
public class VladoStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	/// <summary>
	/// Fast EMA period.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow EMA period.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

	/// <summary>
	/// Stop-loss distance in price steps.
	/// </summary>
	public int StopLossPoints
	{
		get => _stopLossPoints.Value;
		set => _stopLossPoints.Value = value;
	}

	/// <summary>
	/// Take-profit distance in price steps.
	/// </summary>
	public int TakeProfitPoints
	{
		get => _takeProfitPoints.Value;
		set => _takeProfitPoints.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="VladoStrategy"/> class.
	/// </summary>
	public VladoStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicator");

		_slowPeriod = Param(nameof(SlowPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicator");

		_stopLossPoints = Param(nameof(StopLossPoints), 200)
			.SetNotNegative()
			.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");

		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400)
			.SetNotNegative()
			.SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_fast = null;
		_slow = null;
		_prevFast = 0;
		_prevSlow = 0;
		_entryPrice = 0;
		_cooldown = 0;
	}

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

		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

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

		if (!_fast.IsFormed || !_slow.IsFormed)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevFast = fastValue;
			_prevSlow = slowValue;
			return;
		}

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		// Check SL/TP
		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step)
			{
				SellMarket();
				_entryPrice = 0;
				_cooldown = 80;
				_prevFast = fastValue;
				_prevSlow = slowValue;
				return;
			}

			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step)
			{
				SellMarket();
				_entryPrice = 0;
				_cooldown = 80;
				_prevFast = fastValue;
				_prevSlow = slowValue;
				return;
			}
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step)
			{
				BuyMarket();
				_entryPrice = 0;
				_cooldown = 80;
				_prevFast = fastValue;
				_prevSlow = slowValue;
				return;
			}

			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step)
			{
				BuyMarket();
				_entryPrice = 0;
				_cooldown = 80;
				_prevFast = fastValue;
				_prevSlow = slowValue;
				return;
			}
		}

		// EMA crossover
		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();

			BuyMarket();
			_entryPrice = close;
			_cooldown = 80;
		}
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{
			if (Position > 0)
				SellMarket();

			SellMarket();
			_entryPrice = close;
			_cooldown = 80;
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}