在 GitHub 上查看

彩色零延迟RSI OSMA策略

该策略基于五个不同周期RSI的组合振荡器。将加权后的RSI值进行两次平滑,得到零延迟的OSMA曲线。

工作原理

  1. 计算周期为8、21、34、55、89的五个RSI。
  2. 按权重相乘并求和。
  3. 对结果进行两次平滑得到OSMA值。
  4. 当OSMA转向上(前一值低于两根之前且当前值高于前一值)时,策略平掉空头并可选开多头。
  5. 当OSMA转向下(前一值高于两根之前且当前值低于前一值)时,策略平掉多头并可选开空头。

参数

  • Smoothing 1, Smoothing 2 – 两个平滑阶段的长度。
  • Factor 1..5 – 每个RSI分量的权重。
  • RSI Period 1..5 – RSI指标的周期。
  • Allow Buy / Allow Sell – 允许开多或开空。
  • Close Long / Close Short – 在相反信号出现时平仓。
  • Candle Type – 使用的K线周期,默认4小时。

说明

策略仅在K线收盘后执行信号。启动时自动开启仓位保护。

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>
/// Strategy based on the Color Zerolag RSI OSMA indicator.
/// Uses weighted RSI composite with EMA smoothing for direction changes.
/// </summary>
public class ColorZerolagRsiOsmaStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _smoothing;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevOsma;
	private decimal _prevPrevOsma;
	private int _count;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public int Smoothing { get => _smoothing.Value; set => _smoothing.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ColorZerolagRsiOsmaStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI calculation period", "Indicator");

		_smoothing = Param(nameof(Smoothing), 21)
			.SetGreaterThanZero()
			.SetDisplay("Smoothing", "EMA smoothing period", "Indicator");

		_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();
		_prevOsma = 0;
		_prevPrevOsma = 0;
		_count = 0;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var ema = new ExponentialMovingAverage { Length = Smoothing };

		SubscribeCandles(CandleType)
			.Bind(rsi, ema, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal rsiValue, decimal emaValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		// OSMA = difference between RSI and its smoothed version (EMA of price)
		var osma = rsiValue - 50m;
		_count++;

		if (_count < 3)
		{
			_prevPrevOsma = _prevOsma;
			_prevOsma = osma;
			return;
		}

		// Buy when OSMA turns up
		var turnUp = _prevOsma < _prevPrevOsma && osma > _prevOsma;
		// Sell when OSMA turns down
		var turnDown = _prevOsma > _prevPrevOsma && osma < _prevOsma;

		if (turnUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (turnDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevPrevOsma = _prevOsma;
		_prevOsma = osma;
	}
}