GitHub で見る

Triple RVI Strategy

This strategy trades using the Relative Vigor Index (RVI) on three different timeframes. The longer-term RVI trends act as filters, while the shortest timeframe is used for entries. A long position is opened when the short-term RVI crosses below its signal line while both higher timeframes remain bullish. A short position is opened when the short-term RVI crosses above its signal line and both higher timeframes are bearish. Positions are closed when any timeframe indicates a trend change against the current position.

Parameters

  • RviPeriod – period for calculating the RVI.
  • CandleType1 – timeframe of the highest RVI filter.
  • CandleType2 – timeframe of the middle RVI filter.
  • CandleType3 – trading timeframe where entry signals are generated.
  • Volume – order size used for market orders.

Notes

  • Only finished candles are processed.
  • The strategy uses the StockSharp high level API.
  • Default timeframes correspond to 30, 15 and 5 minute candles.
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>
/// Strategy that trades using Relative Vigor Index with multiple period confirmations.
/// Uses a long-period RVI for trend, mid-period for confirmation, and short for entry.
/// </summary>
public class TripleRviStrategy : Strategy
{
	private readonly StrategyParam<int> _rviPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private int _trend1;
	private int _trend2;
	private decimal _prevSignal = decimal.MinValue;

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

	public TripleRviStrategy()
	{
		_rviPeriod = Param(nameof(RviPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("RVI Period", "Base period of RVI", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Trading timeframe", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

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

		_trend1 = 0;
		_trend2 = 0;
		_prevSignal = decimal.MinValue;
	}

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

		_trend1 = 0;
		_trend2 = 0;
		_prevSignal = decimal.MinValue;

		var trendRsi = new RelativeStrengthIndex { Length = RviPeriod * 3 };
		var midRsi = new RelativeStrengthIndex { Length = RviPeriod * 2 };
		var signalRsi = new RelativeStrengthIndex { Length = RviPeriod };

		var sub = SubscribeCandles(CandleType);
		sub.Bind(trendRsi, midRsi, signalRsi, ProcessCandle).Start();

		StartProtection(
			new Unit(2000m, UnitTypes.Absolute),
			new Unit(1000m, UnitTypes.Absolute));
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		_trend1 = trendValue > 55m ? 1 : trendValue < 45m ? -1 : 0;
		_trend2 = midValue > 55m ? 1 : midValue < 45m ? -1 : 0;

		if (_prevSignal == decimal.MinValue)
		{
			_prevSignal = signalValue;
			return;
		}

		var crossUp = _prevSignal <= 50m && signalValue > 50m;
		var crossDown = _prevSignal >= 50m && signalValue < 50m;

		if (crossUp && _trend1 > 0 && _trend2 > 0 && Position <= 0)
			BuyMarket();
		else if (crossDown && _trend1 < 0 && _trend2 < 0 && Position >= 0)
			SellMarket();

		if (Position > 0 && (_trend1 < 0 || _trend2 < 0))
			SellMarket();
		else if (Position < 0 && (_trend1 > 0 || _trend2 > 0))
			BuyMarket();

		_prevSignal = signalValue;
	}
}