在 GitHub 上查看

Spectral RVI Crossover 策略

Spectral RVI Crossover 策略对相对活力指数及其信号线进行平滑处理,并在平滑后的线条交叉时交易。 当平滑后的 RVI 上穿平滑后的信号线时做多,反向交叉时做空。

细节

  • 入场条件:平滑 RVI 上穿其平滑信号线
  • 多空方向:双向
  • 出场条件:反向交叉
  • 止损:无
  • 默认参数
    • RviLength = 14
    • SignalLength = 4
    • SmoothLength = 20
  • 筛选器
    • 类别:振荡指标
    • 方向:双向
    • 指标:RVI、SMA
    • 止损:无
    • 复杂度:基础
    • 时间框架:4小时
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中
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>
/// Spectral RVI crossover strategy.
/// Applies smoothing to RVI average and signal and trades on their crossovers.
/// </summary>
public class SpectralRviStrategy : Strategy
{
	private readonly StrategyParam<int> _rviLength;
	private readonly StrategyParam<int> _smoothLength;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _smoothRvi;
	private SimpleMovingAverage _smoothSig;

	private decimal? _prevSmRvi;
	private decimal? _prevSmSig;

	public int RviLength { get => _rviLength.Value; set => _rviLength.Value = value; }
	public int SmoothLength { get => _smoothLength.Value; set => _smoothLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SpectralRviStrategy()
	{
		_rviLength = Param(nameof(RviLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("RVI Length", "Length for RVI", "General");

		_smoothLength = Param(nameof(SmoothLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Smooth Length", "Smoothing length", "General");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_smoothRvi = null;
		_smoothSig = null;
		_prevSmRvi = null;
		_prevSmSig = null;
	}

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

		_prevSmRvi = null;
		_prevSmSig = null;
		_smoothRvi = new SimpleMovingAverage { Length = SmoothLength };
		_smoothSig = new SimpleMovingAverage { Length = SmoothLength };

		var rvi = new RelativeVigorIndex();

		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 rviVal)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (rviVal is not IRelativeVigorIndexValue rviTyped)
			return;

		if (rviTyped.Average is not decimal avg || rviTyped.Signal is not decimal sig)
			return;

		var t = candle.CloseTime;
		var smRviResult = _smoothRvi.Process(avg, t, true);
		var smSigResult = _smoothSig.Process(sig, t, true);

		if (!_smoothRvi.IsFormed || !_smoothSig.IsFormed)
			return;

		var smRvi = smRviResult.ToDecimal();
		var smSig = smSigResult.ToDecimal();

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevSmRvi = smRvi;
			_prevSmSig = smSig;
			return;
		}

		if (_prevSmRvi is decimal prevR && _prevSmSig is decimal prevS)
		{
			if (prevR <= prevS && smRvi > smSig && Position <= 0)
				BuyMarket();
			else if (prevR >= prevS && smRvi < smSig && Position >= 0)
				SellMarket();
		}

		_prevSmRvi = smRvi;
		_prevSmSig = smSig;
	}
}