Auf GitHub ansehen

Mean Reversion Pro-Strategie

Mean Reversion Pro ist ein Mean-Reversion-System für wichtige Indizes. Es verwendet zwei gleitende Durchschnitte und Intrabar-Bereiche, um Rücksetzer zu erkennen. Long-Trades werden bevorzugt, da Indizes tendenziell nach oben tendieren.

Details

  • Einstiegskriterien:
    • Long: Schlusskurs unter schnellem SMA, Schlusskurs unter 20%-Bereichsebene, Schlusskurs über langsamem SMA, keine Position.
    • Short: Schlusskurs über schnellem SMA, Schlusskurs über 80%-Bereichsebene, Schlusskurs unter langsamem SMA, keine Position.
  • Long/Short: Beide (Long empfohlen).
  • Ausstiegskriterien:
    • Long: Schlusskurs kreuzt schnellen SMA nach oben.
    • Short: Schlusskurs kreuzt schnellen SMA nach unten.
  • Stops: Keine.
  • Standardwerte:
    • Fast SMA = 5
    • Slow SMA = 100
    • Direction = Nur Long
  • Filter:
    • Kategorie: Mean Reversion
    • Richtung: Konfigurierbar
    • Indikatoren: SMA
    • Stops: Keine
    • Komplexität: Einfach
    • Zeitrahmen: Täglich
using System;
using System.Linq;
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;

public class MeanReversionProStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _fastSma;
	private SimpleMovingAverage _slowSma;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MeanReversionProStrategy()
	{
		_fastLength = Param(nameof(FastLength), 5);
		_slowLength = Param(nameof(SlowLength), 50);
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

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

		_fastSma = new SimpleMovingAverage { Length = FastLength };
		_slowSma = new SimpleMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_fastSma, _slowSma, ProcessCandle)
			.Start();
	}

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

		if (!_fastSma.IsFormed || !_slowSma.IsFormed)
			return;

		var range = candle.HighPrice - candle.LowPrice;
		var longThreshold = candle.LowPrice + 0.2m * range;
		var shortThreshold = candle.HighPrice - 0.2m * range;

		var longSignal = candle.ClosePrice < fast &&
			candle.ClosePrice < longThreshold &&
			candle.ClosePrice > slow;

		var shortSignal = candle.ClosePrice > fast &&
			candle.ClosePrice > shortThreshold &&
			candle.ClosePrice < slow;

		var exitLong = candle.ClosePrice > fast && Position > 0;
		var exitShort = candle.ClosePrice < fast && Position < 0;

		if (longSignal && Position <= 0)
			BuyMarket();
		else if (shortSignal && Position >= 0)
			SellMarket();
		else if (exitLong)
			SellMarket();
		else if (exitShort)
			BuyMarket();
	}
}