在 GitHub 上查看

Hedger 策略

该策略在设定价格放置限价单,同时挂出相反方向的止损单以对冲持仓。支持做多和做空,并提供多种风险控制。

当价格逆向移动时,对冲单保护主要仓位。当盈利达到目标的 75% 时,75-50 规则会把止损移动到利润的一半。可选的风险对冲会在价格大幅不利时开立相反的市价单,并可在指定的跳数后收紧止损。

细节

  • 入场条件:在 EntryPrice 处下限价单,并在 EntryPrice ± Spread 处挂对冲止损单。
  • 多空方向:由 IsLong 参数控制。
  • 出场条件:止损、止盈、75-50 规则或风险对冲触发。
  • 止损:支持,可选择收紧。
  • 过滤器:无。

参数

  • EntryPrice – 挂单价格。
  • StopLoss – 保护性止损价位。
  • TakeProfit – 止盈目标。
  • Volume – 下单数量。
  • Spread – 对冲单距离。
  • IsLong – true 为做多,否则做空。
  • UseRiskHedge – 大幅逆势时开相反市价单。
  • UseRiskSl – 价格不利 RiskSlTicks 跳后收紧止损。
  • RiskSlTicks – 收紧止损所需跳数。
  • UseRule7550 – 启用 75-50 规则。
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 crossover strategy.
/// </summary>
public class HedgerStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public HedgerStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}