在 GitHub 上查看

Close Orders Strategy

该工具策略用于根据用户设定的过滤条件立即平掉现有仓位并取消挂单。它既可以只处理所选品种,也可以遍历整个投资组合。可选的时间窗口和价格区间限制能精确控制被处理的订单。

细节

  • 目的:风险控制和手动平仓。
  • 工作流程
    • 启动时检查可选的时间窗口。
    • 如果时间允许,按过滤条件关闭仓位并取消挂单。
    • 完成后策略会自动停止。
  • 过滤条件
    • CloseAllSecurities – 处理投资组合中的所有品种,而不仅是当前品种。
    • CloseOpenLongOrders / CloseOpenShortOrders – 平掉多头或空头仓位。
    • ClosePendingLongOrders / ClosePendingShortOrders – 取消买入或卖出挂单。
    • SpecificOrderId – 非零时只处理指定交易 ID 的订单。
    • CloseOrdersWithinRangeCloseRangeHighCloseRangeLow – 按开仓价区间限制。
    • EnableTimeControlStartCloseTimeStopCloseTime – 仅在特定时间段执行。
  • 默认值
    • 所有关闭选项均启用。
    • SpecificOrderId = 0。
    • CloseOrdersWithinRange = false。
    • CloseRangeHigh = 0。
    • CloseRangeLow = 0。
    • EnableTimeControl = false。
    • StartCloseTime = 02:00。
    • StopCloseTime = 02:30。
  • 说明
    • 策略不会开立新的仓位。
    • 当价格区间的上下限为零或负数时,忽略该过滤条件。
    • 启用 CloseAllSecurities 时,将处理投资组合中的所有品种。
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>
/// Strategy that closes positions based on time-of-day and EMA conditions.
/// Opens positions during trend and closes them at specific time.
/// </summary>
public class CloseOrdersStrategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevEma;
	private bool _hasPrev;

	public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public CloseOrdersStrategy()
	{
		_emaLength = Param(nameof(EmaLength), 40)
			.SetGreaterThanZero()
			.SetDisplay("EMA Length", "EMA 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;
		_hasPrev = false;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaLength };

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

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

		if (!_hasPrev)
		{
			_prevEma = emaVal;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		// EMA rising -> buy
		if (emaVal > _prevEma && close > emaVal && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// EMA falling -> sell
		else if (emaVal < _prevEma && close < emaVal && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		_prevEma = emaVal;
	}
}