GitHub で見る

XRVIクロスオーバー戦略

XRVIクロスオーバー戦略はExtended Relative Vigor Index(XRVI)に基づいています。 XRVIはRelative Vigor Indexを平滑化し、次にシグナルラインを生成するために第2の移動平均を適用することで計算されます。 戦略はXRVIがシグナルラインを上抜けするとロングに入り、下抜けするとショートに入ります。 既存のポジションは反対シグナルで反転されます。

詳細

  • エントリー条件: XRVIとシグナルラインのクロス
  • ロング/ショート: 両方
  • エグジット条件: 反対のクロスオーバー
  • ストップ: なし
  • デフォルト値:
    • RviLength = 10
    • SignalLength = 5
    • CandleType = H4時間軸
  • フィルター:
    • カテゴリ: オシレーター
    • 方向: 両方
    • インジケーター: Relative Vigor Index, Simple Moving Average
    • ストップ: なし
    • 複雑さ: 基本
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// XRVI crossover strategy.
/// Buys when RVI Average crosses above Signal, sells when it crosses below.
/// </summary>
public class XrviCrossoverStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevAvg;
	private decimal? _prevSig;

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

	public XrviCrossoverStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevAvg = null;
		_prevSig = null;
	}

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

		var rvi = new RelativeVigorIndex();

		SubscribeCandles(CandleType)
			.BindEx(rvi, ProcessCandle)
			.Start();
	}

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

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

		if (_prevAvg is not null && _prevSig is not null)
		{
			var crossUp = _prevAvg <= _prevSig && avg > sig;
			var crossDown = _prevAvg >= _prevSig && avg < sig;

			if (crossUp && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (crossDown && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prevAvg = avg;
		_prevSig = sig;
	}
}