在 GitHub 上查看

Scheduled Time Trader 策略

该策略在预设时间发送市价单,并使用固定的止损和止盈进行保护。

交易规则

  • 当当前时间达到 Trade Hour:Trade Minute:Trade Second 时,策略在本次运行中仅触发一次。
  • 若启用 Allow Buy,则按 Volume 开多单。
  • 若启用 Allow Sell,则按相同 Volume 开空单。
  • 通过 StartProtection 管理保护单,止损和止盈以点数表示。

参数

名称 说明
Volume 下单数量。
Take Profit (ticks) 距离入场价的止盈点数。
Stop Loss (ticks) 距离入场价的止损点数。
Allow Buy 允许做多。
Allow Sell 允许做空。
Trade Hour 入场的小时 (0-23)。
Trade Minute 入场的分钟 (0-59)。
Trade Second 入场的秒钟 (0-59)。
Candle Type 用于跟踪时间的K线类型,默认使用1秒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>
/// 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;
	}
}