在 GitHub 上查看

Exp RSIOMA 策略

Exp RSIOMA 策略使用 RSI 移动平均线 (RSIOMA) 指标来捕捉趋势反转与突破。RSI 数值通过额外的移动平均线进行平滑,形成信号线和柱状图。策略支持四种模式:

  1. Breakdown – 当 RSI 穿越设定的高/低阈值时交易。
  2. HistTwist – 当柱状图方向发生变化时交易。
  3. SignalTwist – 当信号线方向发生变化时交易。
  4. HistDisposition – 当柱状图与信号线交叉时交易。

多头与空头的开仓和平仓可以独立控制。

细节

  • 入场条件:取决于 Mode
  • 多/空:双向
  • 退出条件:相反信号
  • 止损:无
  • 默认值
    • CandleType = 4 小时
    • RsiPeriod = 14
    • SignalPeriod = 21
    • HighLevel = 20
    • LowLevel = -20
  • 过滤条件
    • 类别: 趋势
    • 方向: 双向
    • 指标: RSI
    • 止损: 无
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险级别: 中等
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 based on the RSI crossover with an EMA of RSI (RSIOMA style).
/// Buys when RSI crosses above its EMA and sells when RSI crosses below.
/// </summary>
public class ExpRsiomaStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private decimal _prevEma;
	private bool _hasPrev;

	/// <summary>RSI calculation length.</summary>
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }

	/// <summary>EMA smoothing period.</summary>
	public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }

	/// <summary>Candle type.</summary>
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ExpRsiomaStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 21)
			.SetDisplay("RSI Period", "RSI calculation length", "Parameters")
			.SetGreaterThanZero();

		_emaPeriod = Param(nameof(EmaPeriod), 14)
			.SetDisplay("EMA Period", "EMA smoothing period", "Parameters")
			.SetGreaterThanZero();

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for candles", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = 0m;
		_prevEma = 0m;
		_hasPrev = false;
	}

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

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

		_hasPrev = false;

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var rsiEma = new ExponentialMovingAverage { Length = EmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(rsi, (candle, rsiValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				var emaResult = rsiEma.Process(new DecimalIndicatorValue(rsiEma, rsiValue, candle.ServerTime) { IsFinal = true });

				if (!rsiEma.IsFormed)
					return;

				var emaValue = emaResult.ToDecimal();

				if (_hasPrev)
				{
					var crossUp = _prevRsi <= _prevEma && rsiValue > emaValue;
					var crossDown = _prevRsi >= _prevEma && rsiValue < emaValue;

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

				_prevRsi = rsiValue;
				_prevEma = emaValue;
				_hasPrev = true;
			})
			.Start();

		StartProtection(
			takeProfit: new Unit(2, UnitTypes.Percent),
			stopLoss: new Unit(1, UnitTypes.Percent)
		);

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