在 GitHub 上查看

Times Direction 策略

该时间驱动策略在预定时间窗口内开仓一次多头或空头,并在另一个时间窗口内平仓。入场方向可配置,同时监控可选的止损和止盈。策略仅使用已完成的K线,不依赖指标。

细节

  • 入场条件
    • 当当前K线时间位于 [OpenTime, OpenTime + TradeInterval) 且没有持仓时,按设定方向开仓。
  • 离场条件
    • 当时间位于 [CloseTime, CloseTime + TradeInterval) 时平仓。
    • 如果达到止损或止盈水平也会平仓。
  • 多空方向:可设置。
  • 止损止盈:以价格单位设置,相对于入场价。
  • 默认值
    • Trade = Sell。
    • OpenTime = 1970-01-01 00:00。
    • CloseTime = 3000-01-01 00:00。
    • TradeInterval = 1 分钟。
    • StopLoss = 1000。
    • TakeProfit = 2000。
    • Volume = 0.1。
  • 过滤器
    • 分类:基于时间
    • 方向:单向
    • 指标:无
    • 止损:是
    • 复杂度:简单
    • 时间框架:短期
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Time based strategy that opens and closes a position at specified hours of the day.
/// Opens at OpenHour and closes at CloseHour, with SL/TP protection.
/// </summary>
public class TimesDirectionStrategy : Strategy
{
	private readonly StrategyParam<int> _openHour;
	private readonly StrategyParam<int> _closeHour;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;

	public int OpenHour { get => _openHour.Value; set => _openHour.Value = value; }
	public int CloseHour { get => _closeHour.Value; set => _closeHour.Value = value; }
	public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public TimesDirectionStrategy()
	{
		_openHour = Param(nameof(OpenHour), 2)
			.SetDisplay("Open Hour", "Hour of day to open position (UTC)", "General");

		_closeHour = Param(nameof(CloseHour), 14)
			.SetDisplay("Close Hour", "Hour of day to close position (UTC)", "General");

		_stopLoss = Param(nameof(StopLoss), 500m)
			.SetDisplay("Stop Loss", "Stop loss distance in price units", "Risk");

		_takeProfit = Param(nameof(TakeProfit), 1000m)
			.SetDisplay("Take Profit", "Take profit distance in price units", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to process", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0m;
	}

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

		_entryPrice = 0m;

		var sub = SubscribeCandles(CandleType);
		sub.Bind(ProcessCandle).Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, sub);
			DrawOwnTrades(area);
		}
	}

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

		var hour = candle.OpenTime.Hour;

		if (Position == 0)
		{
			if (hour == OpenHour && candle.OpenTime.DayOfWeek == DayOfWeek.Monday)
			{
				_entryPrice = candle.ClosePrice;
				BuyMarket();
			}
		}
		else
		{
			// Close at specified hour
			if (hour == CloseHour && candle.OpenTime.DayOfWeek == DayOfWeek.Friday)
			{
				SellMarket();
				_entryPrice = 0m;
				return;
			}

			// SL/TP check
			if (_entryPrice != 0m && Position > 0)
			{
				var sl = _entryPrice - StopLoss;
				var tp = _entryPrice + TakeProfit;
				if (candle.LowPrice <= sl || candle.HighPrice >= tp)
				{
					SellMarket();
					_entryPrice = 0m;
				}
			}
		}
	}
}