在 GitHub 上查看

Aroon Horn Sign

Aroon Horn Sign 策略使用 Aroon 指标寻找趋势反转。 它在较大时间框架的K线中监控 Aroon Up 和 Aroon Down 线。当 Aroon Up 上穿 Aroon Down 并保持在 50 水平以上时,表明可能出现 多头反转。策略会平掉任何空头仓位并开立多头。当 Aroon Down 占优并位于 50 以上时,任何多头仓位都会被关闭并建立空头。

策略采用以价格单位表示的固定止盈和止损,通过内置的风险 保护模块触发。由于逻辑仅依赖 Aroon 指标,该方法适用于不同 市场与时间框架,无需额外过滤器。

细节

  • 数据:价格K线。
  • 入场条件
    • 多头Aroon Up > Aroon DownAroon Up >= 50。
    • 空头Aroon Down > Aroon UpAroon Down >= 50。
  • 出场条件
    • 多头在出现空头信号时平仓。
    • 空头在出现多头信号时平仓。
  • 止损:通过 StartProtection 设置固定止盈和止损。
  • 默认值
    • AroonPeriod = 9
    • CandleType = 4 小时K线
    • TakeProfit = 2000(价格单位)
    • StopLoss = 1000(价格单位)
  • 过滤器
    • 分类:趋势反转
    • 方向:多头和空头
    • 指标:Aroon
    • 复杂度:简单
    • 风险等级:中等
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>
/// Aroon Horn Sign trend reversal strategy.
/// Opens long when Aroon Up crosses above Aroon Down above 50.
/// Opens short when the opposite occurs.
/// </summary>
public class AroonHornSignStrategy : Strategy
{
	private readonly StrategyParam<int> _aroonPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private int _prevTrend;

	public int AroonPeriod { get => _aroonPeriod.Value; set => _aroonPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AroonHornSignStrategy()
	{
		_aroonPeriod = Param(nameof(AroonPeriod), 9)
			.SetDisplay("Aroon Period", "Aroon indicator period", "Indicators");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevTrend = 0;
	}

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

		_prevTrend = 0;

		var aroon = new Aroon { Length = AroonPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(aroon, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var value = (IAroonValue)aroonValue;

		var up = value.Up;
		var down = value.Down;

		if (up is null || down is null)
			return;

		var trend = _prevTrend;

		if (up > down && up >= 50m)
			trend = 1;
		else if (down > up && down >= 50m)
			trend = -1;

		if (_prevTrend <= 0 && trend > 0 && Position <= 0)
			BuyMarket();
		else if (_prevTrend >= 0 && trend < 0 && Position >= 0)
			SellMarket();

		_prevTrend = trend;
	}
}