Auf GitHub ansehen

Intraday v2 Strategie

Diese Strategie implementiert einen Intraday-Mean-Reversion-Ansatz mit zwei Sätzen von Bollinger Bändern. Die äußeren Bänder (Abweichung 2.4) definieren Einstiegszonen, während die inneren Bänder (Abweichung 1) die Ausstiege verwalten. Optionale Stop-Loss- und Take-Profit-Levels schließen Positionen, wenn sich der Preis um einen konfigurierbaren Betrag gegen den Trade bewegt.

Details

  • Einstiegskriterien:
    • Long: Der Schlusskurs fällt unter das untere äußere Band.
    • Short: Der Schlusskurs steigt über das obere äußere Band.
  • Long/Short: Beide.
  • Ausstiegskriterien:
    • Long: Der Preis kreuzt das untere innere Band nach oben oder trifft Stop-Loss/Take-Profit.
    • Short: Der Preis kreuzt das obere innere Band nach unten oder trifft Stop-Loss/Take-Profit.
  • Stops: Konfigurierbare absolute Stop-Loss- und Take-Profit-Werte.
  • Filter: Keine.
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>
/// Intraday mean reversion strategy using SMA and standard deviation bands.
/// Buys when price touches lower band, sells when touching upper band.
/// </summary>
public class IntradayV2Strategy : Strategy
{
	private readonly StrategyParam<int> _bandLength;
	private readonly StrategyParam<DataType> _candleType;

	private readonly List<decimal> _closes = new();

	public int BandLength { get => _bandLength.Value; set => _bandLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public IntradayV2Strategy()
	{
		_bandLength = Param(nameof(BandLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("Band Length", "Band period", "Indicators");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_closes.Clear();
	}

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

		var sma = new SimpleMovingAverage { Length = BandLength };
		var stdev = new StandardDeviation { Length = BandLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, stdev, ProcessCandle)
			.Start();
	}

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

		if (stdevVal <= 0)
			return;

		var close = candle.ClosePrice;
		var upper = smaVal + 2m * stdevVal;
		var lower = smaVal - 2m * stdevVal;

		// Mean reversion: buy at lower band
		if (close < lower && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Mean reversion: sell at upper band
		else if (close > upper && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		// Exit long at middle (SMA)
		if (Position > 0 && close > smaVal)
		{
			SellMarket();
		}
		// Exit short at middle (SMA)
		else if (Position < 0 && close < smaVal)
		{
			BuyMarket();
		}
	}
}