在 GitHub 上查看

PresentTrend RMI Synergy

原生Relative Momentum Index配合收盘SMA入场,随后用仅向保护方向收紧的SMA/ATR价格止损。不声称盈利表现。

指标和入场

默认使用已完成的五分钟蜡烛。原生RMI:d[t]=close[t]-close[t-RmiMomentum],lag=3。最后RmiPeriod=21个动量样本中,U为正d的均值(其余计零),D为负d绝对值的均值(其余计零),RMI=100*U/(U+D)。分母为零时无入场值。这是原生滚动均值相对动量计算。

收盘SMA和原生Wilder ATR共享SuperTrendLength=5;ATR包含相对前一close的跳空,以增长均值开始,然后Wilder平滑。三个指标形成后,仅从空仓入场:RMI>60且close>SMA做多;RMI<40且close<SMA做空。按策略Volume发市价单。持仓期间忽略新的入场信号。

Direction是可选的类型化Sides过滤器:C#的null或Python的None表示双向,Sides.Buy仅多,Sides.Sell仅空。只限制入场,止损始终平掉实际持仓;不使用字符串或数字方向代码。

跟踪止损

width=SuperTrendMultiplier*ATR,默认倍数4。多头入场的stop=SMA-width,后续已完成蜡烛取max(previousStop,SMA-width);空头入场stop=SMA+width,随后取min(previousStop,SMA+width)。持仓期间从不放松。初始止损来自信号蜡烛的SMA/ATR,不按成交价重算;保留的SuperTrend参数名配置这些以SMA为中心的带。

正bid≤多头stop或正ask≥空头stop时,市价平掉全部实际数量。更新stop后也检查已完成close,当前close到达新stop可立即退出。蜡烛之间的真实报价可触发退出。新stop只用于当前及后续观测价格,不回看该蜡烛此前的high/low。当前RMI为空时,SMA/ATR仍可更新保护。

这是本地价格监控;按报价进行盘中保护需Level1 bid/ask,无报价时仍检查close。不提交交易所原生止损单,成交价可偏离阈值。未完成订单阻止重复,指标和stop继续更新。止损退出的蜡烛不再次入场,稍后的合格蜡烛可重入。没有EMA、六小时冷却、相反信号立即反转或独立止盈。

参数 默认值
RmiPeriod 21,正
RmiMomentum 3,正的close lag
SuperTrendLength 5,正的共享SMA/ATR周期
SuperTrendMultiplier 4,正的ATR宽度
Direction 未设置,双向
CandleType 5分钟

C#和Python在现有BTC/TON历史上独立计算动量和、SMA、Wilder ATR及收紧止损。验证每个入场、可执行价格退出和实际持仓、两方向及过滤、持仓中忽略相反入场、紧止损的蜡烛间触发和小于六小时的入场间隔。

namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

/// <summary>
/// Opens flat-position trades on native RMI momentum and SMA alignment,
/// then exits only at a ratcheted SMA/Wilder ATR price stop.
/// </summary>
public class PresentTrendRmiSynergyStrategy : Strategy
{
	private readonly StrategyParam<int> _rmiPeriod;
	private readonly StrategyParam<int> _rmiMomentum;
	private readonly StrategyParam<int> _superTrendLength;
	private readonly StrategyParam<decimal> _superTrendMultiplier;
	private readonly StrategyParam<Sides?> _direction;
	private readonly StrategyParam<DataType> _candleType;
	private decimal? _stopPrice;
	private Order _pendingOrder;

	/// <summary>Initializes RMI21 with momentum3, SMA/ATR5, multiplier4 and both directions on five-minute candles.</summary>
	public PresentTrendRmiSynergyStrategy()
	{
		_rmiPeriod = Param(nameof(RmiPeriod), 21).SetGreaterThanZero()
			.SetDisplay("RMI Period", "Native rolling mean period of positive/negative momentum", "Indicators");
		_rmiMomentum = Param(nameof(RmiMomentum), 3).SetGreaterThanZero()
			.SetDisplay("RMI Momentum", "Closed-bar lag of each close-price momentum sample", "Indicators");
		_superTrendLength = Param(nameof(SuperTrendLength), 5).SetGreaterThanZero()
			.SetDisplay("SuperTrend Length", "Shared close SMA and Wilder ATR period for trailing bands", "Protection");
		_superTrendMultiplier = Param(nameof(SuperTrendMultiplier), 4m).SetGreaterThanZero()
			.SetDisplay("SuperTrend Multiplier", "ATR width of the ratcheted SMA price stop", "Protection");
		_direction = Param<Sides?>(nameof(Direction), null)
			.SetDisplay("Direction", "Entry side filter: Buy for long, Sell for short, unset for both", "Trading");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Finished candles supplying RMI, SMA and ATR", "General");
		OrderRegistering += order => _pendingOrder = order;
	}

	/// <summary>Positive native RMI rolling mean period.</summary>
	public int RmiPeriod { get => _rmiPeriod.Value; set => _rmiPeriod.Value = value; }

	/// <summary>Positive close-price momentum lag, in finished candles.</summary>
	public int RmiMomentum { get => _rmiMomentum.Value; set => _rmiMomentum.Value = value; }

	/// <summary>Positive shared SMA and Wilder ATR period.</summary>
	public int SuperTrendLength { get => _superTrendLength.Value; set => _superTrendLength.Value = value; }

	/// <summary>Positive ATR multiplier of the trailing stop.</summary>
	public decimal SuperTrendMultiplier { get => _superTrendMultiplier.Value; set => _superTrendMultiplier.Value = value; }

	/// <summary>Allowed entry side; null permits both. Stops always close the held side.</summary>
	public Sides? Direction { get => _direction.Value; set => _direction.Value = value; }

	/// <summary>Finished candle type; five minutes by default.</summary>
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

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

	private void ResetState()
	{
		_stopPrice = null;
		_pendingOrder = null;
	}

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

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		if (RmiPeriod <= 0 || RmiMomentum <= 0 || SuperTrendLength <= 0 || SuperTrendMultiplier <= 0m
			|| Direction is Sides direction && direction is not (Sides.Buy or Sides.Sell))
			throw new ArgumentException("Periods and ATR multiplier must be positive; direction must be Buy, Sell or unset for both.");
		ResetState();
		foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
		{
			var quotes = new Subscription(DataType.Level1, Security);
			quotes.MarketData.BuildField = field;
			SubscribeLevel1(quotes).Bind(ObserveQuote).Start();
		}
		var rmi = new RelativeMomentumIndex { Length = RmiPeriod, MomentumPeriod = RmiMomentum };
		var mean = new SimpleMovingAverage { Length = SuperTrendLength };
		var atr = new AverageTrueRange { Length = SuperTrendLength };
		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx([rmi, mean, atr], ProcessCandle, allowEmpty: true).Start();
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, mean);
			DrawOwnTrades(area);
		}
	}

	private bool IsPending => _pendingOrder is not null && _pendingOrder.State is not (OrderStates.Done or OrderStates.Failed);

	private void Submit(Sides side, decimal volume, string comment)
		=> RegisterOrder(new Order
		{
			Security = Security,
			Portfolio = Portfolio,
			Type = OrderTypes.Market,
			Side = side,
			Volume = volume,
			Comment = comment,
		});

	private bool TryStopExit(decimal price)
	{
		if (Position == 0m || _stopPrice is not decimal stop || IsPending || !IsFormedAndOnlineAndAllowTrading())
			return false;
		if (Position > 0m ? price > stop : price < stop)
			return false;
		Submit(Position > 0m ? Sides.Sell : Sides.Buy, Math.Abs(Position), "RMI ATR trailing stop");
		return true;
	}

	private void ObserveQuote(Level1ChangeMessage quote)
	{
		var field = Position > 0m ? Level1Fields.BestBidPrice : Level1Fields.BestAskPrice;
		if (quote.TryGetDecimal(field) is decimal price && price > 0m)
			TryStopExit(price);
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue[] values)
	{
		if (candle.State != CandleStates.Finished || values[1].IsEmpty || !values[1].IsFormed
			|| values[2].IsEmpty || !values[2].IsFormed)
			return;
		var mean = values[1].ToDecimal();
		var width = values[2].ToDecimal() * SuperTrendMultiplier;
		if (Position != 0m)
		{
			var candidate = Position > 0m ? mean - width : mean + width;
			_stopPrice = _stopPrice is decimal previous ? Position > 0m ? Math.Max(previous, candidate) : Math.Min(previous, candidate) : candidate;
			TryStopExit(candle.ClosePrice);
			return;
		}
		if (values[0].IsEmpty || !values[0].IsFormed || IsPending || !IsFormedAndOnlineAndAllowTrading())
			return;
		var rmi = values[0].ToDecimal();
		Sides? side = rmi > 60m && candle.ClosePrice > mean ? Sides.Buy
			: rmi < 40m && candle.ClosePrice < mean ? Sides.Sell : null;
		if (side is not Sides entry || Direction is Sides allowed && entry != allowed)
			return;
		_stopPrice = entry == Sides.Buy ? mean - width : mean + width;
		Submit(entry, Volume, "RMI SMA aligned entry");
	}
}