Auf GitHub ansehen

Geplante Zeithandel-Strategie

Diese Strategie gibt Marktorders zu einer vordefinierten Zeit ab und schützt sie mit festen Stop-Loss- und Take-Profit-Niveaus.

Handelsregeln

  • Wenn die aktuelle Zeit Trade Hour:Trade Minute:Trade Second erreicht, wird die Strategie einmal pro Sitzung ausgelöst.
  • Wenn Allow Buy aktiviert ist, wird eine Long-Position mit dem angegebenen Volume eröffnet.
  • Wenn Allow Sell aktiviert ist, wird eine Short-Position mit demselben Volume eröffnet.
  • Schutzorders werden über StartProtection mit Punktwerten für Stop-Loss und Take-Profit verwaltet.

Parameter

Name Beschreibung
Volume Ordergröße.
Take Profit (ticks) Take-Profit-Abstand vom Einstieg in Ticks.
Stop Loss (ticks) Stop-Loss-Abstand vom Einstieg in Ticks.
Allow Buy Long-Trades aktivieren.
Allow Sell Short-Trades aktivieren.
Trade Hour Handelsstunde des Tages (0-23).
Trade Minute Handelsminute der Stunde (0-59).
Trade Second Handelssekunde der Minute (0-59).
Candle Type Kerzenserie zur Zeitverfolgung, Standard sind 1-Sekunden-Kerzen.

Hinweise

Die Strategie eröffnet Trades nur einmal pro Ausführung. Um erneut zu handeln, starten Sie die Strategie neu oder passen Sie die Handelszeit an.

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>
/// EMA trend-following strategy with RSI filter.
/// </summary>
public class ScheduledTimeTraderStrategy : Strategy
{
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevEma;
	private decimal _prevClose;
	private bool _hasPrev;

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

	public ScheduledTimeTraderStrategy()
	{
		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "EMA period", "Indicators");
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI 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();
		_prevEma = 0;
		_prevClose = 0;
		_hasPrev = false;
	}

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

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

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

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

		var close = candle.ClosePrice;

		if (!_hasPrev) { _prevEma = emaValue; _prevClose = close; _hasPrev = true; return; }

		// Buy: close crosses above EMA and RSI confirms
		if (_prevClose <= _prevEma && close > emaValue && rsiValue < 65 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Sell: close crosses below EMA and RSI confirms
		else if (_prevClose >= _prevEma && close < emaValue && rsiValue > 35 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevEma = emaValue;
		_prevClose = close;
	}
}