View on GitHub

4153 Vortex Oscillator System

Overview

The strategy reproduces the MetaTrader 4 "Vortex Oscillator System" expert using StockSharp's high level strategy API. It derives a normalized Vortex oscillator by combining the standard Vortex indicator components and reacts whenever momentum escapes a configurable neutral band. The algorithm trades a single symbol and always works with fully closed or reversed positions.

Trading rules

  • A candle subscription defined by CandleType feeds a Vortex indicator with period VortexLength. The oscillator is calculated as (VI+ - VI-) / (VI+ + VI-), which keeps readings in the [-1, 1] range.
  • A long setup is armed when the oscillator falls below BuyThreshold and, if UseBuyStopLoss is enabled, remains above BuyStopLossLevel. A short setup is armed when the oscillator rises above SellThreshold and, if UseSellStopLoss is enabled, remains below SellStopLossLevel.
  • Whenever the oscillator moves back inside the neutral band bounded by BuyThreshold and SellThreshold, both setups are cleared so the next break must happen from a neutral state.
  • If a long setup is active while the current position is flat or short, the strategy sends a market buy for Volume lots plus any quantity required to cover an existing short. Short setups mirror that behaviour by selling Volume lots plus the outstanding long quantity.
  • Open positions may be closed without an opposite setup: if UseBuyStopLoss is enabled and the oscillator touches BuyStopLossLevel the long trade is liquidated; UseBuyTakeProfit exits a long once the oscillator exceeds BuyTakeProfitLevel. Equivalent checks using SellStopLossLevel and SellTakeProfitLevel manage short positions when their respective toggles are enabled.

Parameters

  • VortexLength – number of candles used to compute VI+ and VI- values.
  • CandleType – timeframe or data type requested from the market data source.
  • Volume – base order size for new entries; reversal orders automatically add the quantity needed to flatten the current position.
  • BuyThreshold – oscillator level that arms a long setup once breached.
  • UseBuyStopLoss – requires the oscillator to stay above BuyStopLossLevel before a long entry can be armed.
  • BuyStopLossLevel – oscillator level that immediately closes a long position when the stop filter is enabled.
  • UseBuyTakeProfit – toggles the oscillator based take-profit for long trades.
  • BuyTakeProfitLevel – oscillator level that realizes profits on long positions when the take-profit filter is active.
  • SellThreshold – oscillator level that arms a short setup once breached.
  • UseSellStopLoss – requires the oscillator to stay below SellStopLossLevel before a short entry can be armed.
  • SellStopLossLevel – oscillator level that immediately closes a short position when the stop filter is enabled.
  • UseSellTakeProfit – toggles the oscillator based take-profit for short trades.
  • SellTakeProfitLevel – oscillator level that realizes profits on short positions when the take-profit filter is active.

Additional notes

  • The strategy draws candles and executed trades on the chart automatically; the internal oscillator logic does not add custom panes.
  • Because the oscillator is normalized, the default thresholds -0.75, 0.75, -1.00, and 1.00 translate directly from the original expert advisor and can be optimized using StockSharp's parameter system.
  • The logic never keeps simultaneous long and short positions; every reversal closes the current exposure first and then opens the opposite side in a single market order.
using System;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Vortex Oscillator System: RSI momentum breakout with EMA filter and ATR stops.
/// </summary>
public class VortexOscillatorSystemMomentumStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _atrLength;

	private decimal _prevRsi;
	private decimal _entryPrice;

	public VortexOscillatorSystemMomentumStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe.", "General");

		_rsiLength = Param(nameof(RsiLength), 14)
			.SetDisplay("RSI Length", "RSI period.", "Indicators");

		_emaLength = Param(nameof(EmaLength), 50)
			.SetDisplay("EMA Length", "Trend filter.", "Indicators");

		_atrLength = Param(nameof(AtrLength), 14)
			.SetDisplay("ATR Length", "ATR period.", "Indicators");
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public int RsiLength
	{
		get => _rsiLength.Value;
		set => _rsiLength.Value = value;
	}

	public int EmaLength
	{
		get => _emaLength.Value;
		set => _emaLength.Value = value;
	}

	public int AtrLength
	{
		get => _atrLength.Value;
		set => _atrLength.Value = value;
	}

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

		_prevRsi = 0;
		_entryPrice = 0;
	}

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

		_prevRsi = 0;
		_entryPrice = 0;

		var rsi = new RelativeStrengthIndex { Length = RsiLength };
		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var atr = new AverageTrueRange { Length = AtrLength };

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

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

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

		if (_prevRsi == 0 || atrVal <= 0)
		{
			_prevRsi = rsiVal;
			return;
		}

		var close = candle.ClosePrice;

		if (Position > 0)
		{
			if (close >= _entryPrice + atrVal * 2.5m || close <= _entryPrice - atrVal * 1.5m || rsiVal < 40)
			{
				SellMarket();
				_entryPrice = 0;
			}
		}
		else if (Position < 0)
		{
			if (close <= _entryPrice - atrVal * 2.5m || close >= _entryPrice + atrVal * 1.5m || rsiVal > 60)
			{
				BuyMarket();
				_entryPrice = 0;
			}
		}

		if (Position == 0)
		{
			if (rsiVal > 55 && _prevRsi <= 55 && close > emaVal)
			{
				_entryPrice = close;
				BuyMarket();
			}
			else if (rsiVal < 45 && _prevRsi >= 45 && close < emaVal)
			{
				_entryPrice = close;
				SellMarket();
			}
		}

		_prevRsi = rsiVal;
	}
}