GitHub で見る

RVI Histogram Reversal Strategy

Overview

This strategy trades against extreme RVI values. It operates on the Relative Vigor Index (RVI) and opens positions when the indicator leaves overbought or oversold zones or when the RVI crosses its signal line. Two signal modes are supported:

  • Levels – reacts to RVI crossing predefined upper or lower thresholds.
  • Cross – reacts to RVI crossing its signal line.

The logic is contrarian: if RVI was above the high level and then falls back, a long position is opened. If RVI was below the low level and then rises back, a short position is opened.

Parameters

Name Description
RviPeriod RVI calculation period.
HighLevel Upper threshold for the RVI.
LowLevel Lower threshold for the RVI.
Mode Signal generation mode (Levels or Cross).
EnableBuyOpen Allow opening long positions.
EnableSellOpen Allow opening short positions.
EnableBuyClose Allow closing long positions.
EnableSellClose Allow closing short positions.
CandleType Candle time frame.

How It Works

  1. RVI and its simple moving average are calculated on every finished candle.
  2. Depending on the selected mode, the strategy checks for:
    • the RVI leaving an extreme level, or
    • the RVI crossing its signal line.
  3. On a long signal the strategy closes short positions and opens a long one. On a short signal it closes long positions and opens a short one.

The default time frame is four hours.

Notes

  • Orders are executed with market orders.
  • Stop-loss and take-profit management can be added separately if required.
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>
/// RVI histogram reversal strategy.
/// Opens long when RVI Average crosses above Signal.
/// Opens short when RVI Average crosses below Signal.
/// </summary>
public class RviHistogramReversalStrategy : Strategy
{
	private readonly StrategyParam<int> _rviPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevAvg;
	private decimal? _prevSig;

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

	public RviHistogramReversalStrategy()
	{
		_rviPeriod = Param(nameof(RviPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RVI Period", "Period of RVI indicator", "General");

		_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 OnReseted()
	{
		base.OnReseted();
		_prevAvg = null;
		_prevSig = null;
	}

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

		var rvi = new RelativeVigorIndex();
		rvi.Average.Length = RviPeriod;
		rvi.Signal.Length = RviPeriod;

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(rvi, ProcessCandle)
			.Start();

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

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

		var rviVal = value as IRelativeVigorIndexValue;
		if (rviVal?.Average is not decimal avg || rviVal?.Signal is not decimal sig)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevAvg = avg;
			_prevSig = sig;
			return;
		}

		if (_prevAvg is decimal pa && _prevSig is decimal ps)
		{
			// Average crosses above Signal - buy
			if (pa <= ps && avg > sig && Position <= 0)
				BuyMarket();
			// Average crosses below Signal - sell
			else if (pa >= ps && avg < sig && Position >= 0)
				SellMarket();
		}

		_prevAvg = avg;
		_prevSig = sig;
	}
}