在 GitHub 上查看

按时间开仓和平仓策略

一个简单的时间型策略,在指定的时间开仓,并在另一个预设时间平仓。可以配置交易方向(买入或卖出)和下单量。该示例展示了不使用指标或其他过滤条件的定时交易执行。

细节

  • 入场条件
    • 多头:在 Open TimeIs Buy 为真时。
    • 空头:在 Open TimeIs Buy 为假时。
  • 多空方向:根据 Is Buy,可做多或做空。
  • 出场条件
    • Close Time 平仓,与盈亏无关。
  • 止损:无。
  • 默认值
    • Open Time = 13:00。
    • Close Time = 13:01。
    • Volume = 1。
    • Is Buy = true。
    • Candle Type = 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>
/// Strategy that opens a position at a specified time and closes it at another time.
/// </summary>
public class OpeningClosingOnTimeStrategy : Strategy
{
	private readonly StrategyParam<TimeSpan> _openTime;
	private readonly StrategyParam<TimeSpan> _closeTime;
	private readonly StrategyParam<bool> _isBuy;
	private readonly StrategyParam<DataType> _candleType;
	
	private bool _positionOpened;
	
	/// <summary>
	/// Time of day to open the position.
	/// </summary>
	public TimeSpan OpenTime
	{
		get => _openTime.Value;
		set => _openTime.Value = value;
	}
	
	/// <summary>
	/// Time of day to close the position.
	/// </summary>
	public TimeSpan CloseTime
	{
		get => _closeTime.Value;
		set => _closeTime.Value = value;
	}
	
	
	/// <summary>
	/// Direction of the initial trade.
	/// </summary>
	public bool IsBuy
	{
		get => _isBuy.Value;
		set => _isBuy.Value = value;
	}
	
	/// <summary>
	/// Candle type used for time tracking.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}
	
	/// <summary>
	/// Initializes a new instance of <see cref="OpeningClosingOnTimeStrategy"/>.
	/// </summary>
	public OpeningClosingOnTimeStrategy()
	{
		_openTime = Param(nameof(OpenTime), new TimeSpan(0, 0, 0))
		.SetDisplay("Open Time", "Time of day to open position", "General");
		_closeTime = Param(nameof(CloseTime), new TimeSpan(12, 0, 0))
		.SetDisplay("Close Time", "Time of day to close position", "General");
		_isBuy = Param(nameof(IsBuy), true)
		.SetDisplay("Is Buy", "True for buy, false for sell", "General");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
		.SetDisplay("Candle Type", "Type of candles", "General");
	}
	
	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}
	
	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_positionOpened = false;
	}
	
	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		
		_positionOpened = Position != 0;
		
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(ProcessCandle).Start();
	}
	
	private void ProcessCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var t = candle.OpenTime.TimeOfDay;

		if (!_positionOpened && t >= OpenTime && t < CloseTime)
		{
			if (IsBuy)
				BuyMarket();
			else
				SellMarket();

			_positionOpened = true;
			return;
		}

		if (_positionOpened && t >= CloseTime)
		{
			if (Position > 0)
				SellMarket();
			else if (Position < 0)
				BuyMarket();

			_positionOpened = false;
		}
	}
}