在 GitHub 上查看

Hedge Average 策略

该策略源自 MetaTrader 的 "Hedge Average" 专家。它比较两个周期内开盘价与收盘价的简单移动平均线。

交易逻辑

  • 计算 Period1 和 Period2 的开盘价与收盘价 SMA。
  • 当长周期开盘均线高于其收盘均线且短周期开盘均线低于其收盘均线时,开多单。
  • 当长周期开盘均线低于其收盘均线且短周期开盘均线高于其收盘均线时,开空单。
  • 只有在 StartHour 与 EndHour 之间才允许交易。
  • 可选的止损和止盈以绝对价格单位设置,启用时 trailing stop 会随着价格移动。

参数

  • Period1 – 快速均线周期。
  • Period2 – 慢速均线周期。
  • StartHour – 开始交易的小时。
  • EndHour – 结束交易的小时。
  • CandleType – 使用的K线周期。
  • TakeProfit – 止盈距离(价格单位)。
  • StopLoss – 止损距离(价格单位)。
  • UseTrailing – 是否启用基于止损距离的追踪止损。

说明

该策略采用单一持仓,不包含原版 MQL 中基于金额的获利目标。

using System;
using System.Collections.Generic;
using System.Linq;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Hedge Average strategy comparing open/close SMAs over a fast and a slow period.
/// </summary>
public class HedgeAverageStrategy : Strategy
{
	private readonly StrategyParam<int> _period1;
	private readonly StrategyParam<int> _period2;
	private readonly StrategyParam<int> _startHour;
	private readonly StrategyParam<int> _endHour;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<bool> _useTrailing;

	private readonly List<decimal> _opens = [];
	private readonly List<decimal> _closes = [];
	private decimal _entryPrice;
	private decimal? _stopPrice;
	private decimal? _takePrice;
	private decimal? _bestPrice;
	private DateTimeOffset? _entryCandleTime;

	public int Period1 { get => _period1.Value; set => _period1.Value = value; }
	public int Period2 { get => _period2.Value; set => _period2.Value = value; }
	public int StartHour { get => _startHour.Value; set => _startHour.Value = value; }
	public int EndHour { get => _endHour.Value; set => _endHour.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
	public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public bool UseTrailing { get => _useTrailing.Value; set => _useTrailing.Value = value; }

	public HedgeAverageStrategy()
	{
		_period1 = Param(nameof(Period1), 5).SetGreaterThanZero();
		_period2 = Param(nameof(Period2), 20).SetGreaterThanZero();
		_startHour = Param(nameof(StartHour), 0).SetRange(0, 23);
		_endHour = Param(nameof(EndHour), 23).SetRange(0, 23);
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame());
		_takeProfit = Param(nameof(TakeProfit), 0m).SetNotNegative();
		_stopLoss = Param(nameof(StopLoss), 0m).SetNotNegative();
		_useTrailing = Param(nameof(UseTrailing), false);
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_opens.Clear();
		_closes.Clear();
		ResetProtection();
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		SubscribeCandles(CandleType).Bind(ProcessCandle).Start();
	}

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

		_opens.Add(candle.OpenPrice);
		_closes.Add(candle.ClosePrice);

		var keep = Math.Max(Period1, Period2);
		if (_opens.Count > keep)
		{
			_opens.RemoveRange(0, _opens.Count - keep);
			_closes.RemoveRange(0, _closes.Count - keep);
		}

		if (Position != 0 && ApplyProtection(candle))
			return;

		if (_opens.Count < keep || Position != 0 || !IsTradingHour(candle.OpenTime.Hour))
			return;

		var fastOpen = AverageTail(_opens, Period1);
		var fastClose = AverageTail(_closes, Period1);
		var slowOpen = AverageTail(_opens, Period2);
		var slowClose = AverageTail(_closes, Period2);

		if (slowOpen > slowClose && fastOpen < fastClose)
			Enter(Sides.Buy, candle.ClosePrice, candle.OpenTime);
		else if (slowOpen < slowClose && fastOpen > fastClose)
			Enter(Sides.Sell, candle.ClosePrice, candle.OpenTime);
	}

	private void Enter(Sides side, decimal price, DateTimeOffset candleTime)
	{
		if (side == Sides.Buy)
			BuyMarket();
		else
			SellMarket();

		_entryPrice = price;
		_entryCandleTime = candleTime;
		_bestPrice = price;
		_stopPrice = StopLoss > 0m ? (side == Sides.Buy ? price - StopLoss : price + StopLoss) : null;
		_takePrice = TakeProfit > 0m ? (side == Sides.Buy ? price + TakeProfit : price - TakeProfit) : null;
	}

	private bool ApplyProtection(ICandleMessage candle)
	{
		if (_entryCandleTime is DateTimeOffset entryTime && candle.OpenTime <= entryTime)
			return false;

		if (Position > 0)
		{
			_bestPrice = _bestPrice is decimal best ? Math.Max(best, candle.HighPrice) : candle.HighPrice;

			if (UseTrailing && StopLoss > 0m)
			{
				var candidate = _bestPrice.Value - StopLoss;
				if (_stopPrice is null || candidate > _stopPrice)
					_stopPrice = candidate;
			}

			if ((_stopPrice is decimal stop && candle.LowPrice <= stop) ||
				(_takePrice is decimal take && candle.HighPrice >= take))
			{
				SellMarket(Math.Abs(Position));
				ResetProtection();
				return true;
			}
		}
		else if (Position < 0)
		{
			_bestPrice = _bestPrice is decimal best ? Math.Min(best, candle.LowPrice) : candle.LowPrice;

			if (UseTrailing && StopLoss > 0m)
			{
				var candidate = _bestPrice.Value + StopLoss;
				if (_stopPrice is null || candidate < _stopPrice)
					_stopPrice = candidate;
			}

			if ((_stopPrice is decimal stop && candle.HighPrice >= stop) ||
				(_takePrice is decimal take && candle.LowPrice <= take))
			{
				BuyMarket(Math.Abs(Position));
				ResetProtection();
				return true;
			}
		}

		return false;
	}

	private bool IsTradingHour(int hour)
		=> StartHour <= EndHour
			? hour >= StartHour && hour <= EndHour
			: hour >= StartHour || hour <= EndHour;

	private static decimal AverageTail(List<decimal> values, int length)
		=> values.Skip(values.Count - length).Average();

	private void ResetProtection()
	{
		_entryPrice = 0m;
		_stopPrice = null;
		_takePrice = null;
		_bestPrice = null;
		_entryCandleTime = null;
	}
}