在 GitHub 上查看

Hull Trend OSMA 策略

该策略是 MetaTrader "Exp_HullTrendOSMA" 专家的转换版本。

概述

策略使用 Hull Trend OSMA 指标。该指标计算 Hull 移动平均线以及其平滑版本,两者的差值即为振荡器。当振荡器在连续两个已完成的 K 线中上升时,策略开多仓;当振荡器在连续两个已完成的 K 线中下降时,策略开空仓。每次信号出现时都会平掉反向仓位。

参数

  • Hull Period – Hull 移动平均线的周期。
  • Signal Period – 应用于振荡器的平滑移动平均线周期。
  • Take Profit – 以价格单位表示的止盈距离。
  • Stop Loss – 以价格单位表示的止损距离。
  • Candle Type – 计算所使用的 K 线时间框架(默认 8 小时)。

说明

  • 使用 StockSharp 高级 API,自动订阅 K 线。
  • 入场和平仓均使用市价单执行。
  • 止损和止盈保护在策略启动时初始化一次。
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 based on Hull Trend OSMA indicator.
/// Opens long when oscillator rises twice in a row, short when it falls twice in a row.
/// </summary>
public class HullTrendOsmaStrategy : Strategy
{
	private readonly StrategyParam<int> _hullPeriod;
	private readonly StrategyParam<int> _signalPeriod;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prev1;
	private decimal? _prev2;

	public int HullPeriod { get => _hullPeriod.Value; set => _hullPeriod.Value = value; }
	public int SignalPeriod { get => _signalPeriod.Value; set => _signalPeriod.Value = value; }
	public decimal TakeProfitPct { get => _takeProfitPct.Value; set => _takeProfitPct.Value = value; }
	public decimal StopLossPct { get => _stopLossPct.Value; set => _stopLossPct.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public HullTrendOsmaStrategy()
	{
		_hullPeriod = Param(nameof(HullPeriod), 20)
			.SetDisplay("Hull Period", "Period for Hull Moving Average", "Indicators");

		_signalPeriod = Param(nameof(SignalPeriod), 5)
			.SetDisplay("Signal Period", "Period for signal SMA", "Indicators");

		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");

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

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prev1 = null;
		_prev2 = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var hma = new HullMovingAverage { Length = HullPeriod };
		var signal = new ExponentialMovingAverage { Length = SignalPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(hma, signal, ProcessCandle)
			.Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

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

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

		var osma = hmaValue - signalValue;

		if (_prev1 is null || _prev2 is null)
		{
			_prev2 = _prev1;
			_prev1 = osma;
			return;
		}

		var prev = _prev1.Value;
		var prevPrev = _prev2.Value;

		var isRising = prev > prevPrev && osma >= prev;
		var isFalling = prev < prevPrev && osma <= prev;

		if (isRising && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (isFalling && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prev2 = _prev1;
		_prev1 = osma;
	}
}