在 GitHub 上查看

Hedge Average 策略

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

交易逻辑

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

参数

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

说明

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

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>
/// Hedge Average strategy using fast and slow SMA crossover.
/// Adapted from open/close MA comparison to close-price SMA crossover.
/// </summary>
public class HedgeAverageStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	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 HedgeAverageStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 5)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast SMA period", "Parameters");

		_slowPeriod = Param(nameof(SlowPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow SMA period", "Parameters");

		_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();
		_prevFast = null;
		_prevSlow = null;
	}

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

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

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fast);
			DrawIndicator(area, slow);
			DrawOwnTrades(area);
		}
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			return;
		}

		if (_prevFast is decimal pf && _prevSlow is decimal ps)
		{
			if (pf <= ps && fastValue > slowValue && Position <= 0)
				BuyMarket();
			else if (pf >= ps && fastValue < slowValue && Position >= 0)
				SellMarket();
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}