在 GitHub 上查看

AO 背离策略

该策略寻找价格与 Awesome Oscillator (AO) 之间的多头和空头背离。当价格创出更低低点而 AO 形成更高低点时,出现多头背离;当价格创出更高高点而 AO 形成更低高点时,出现空头背离。

出现多头背离时,策略开多仓;出现空头背离时,策略开空仓。出现相反信号时仓位反转。

细节

  • 入场条件:AO 与价格的多头或空头背离。
  • 多/空:双向。
  • 退出条件:相反的背离信号。
  • 止损:无。
  • 默认值
    • CandleType = 5 分钟
    • FastLength = 5
    • SlowLength = 34
    • Lookback = 5
    • UseEma = false
  • 过滤条件
    • 类别: 指标
    • 方向: 双向
    • 指标: Awesome Oscillator
    • 止损: 无
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 是
    • 风险级别: 中等
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>
/// AO Divergence strategy.
/// Buys when AO crosses above zero, sells when AO crosses below zero.
/// Uses EMA as trend filter.
/// </summary>
public class AoDivergenceStrategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevAo;
	private int _barIndex;
	private int _lastTradeBar;

	/// <summary>
	/// EMA trend filter period.
	/// </summary>
	public int EmaLength
	{
		get => _emaLength.Value;
		set => _emaLength.Value = value;
	}

	/// <summary>
	/// Cooldown bars between trades.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public AoDivergenceStrategy()
	{
		_emaLength = Param(nameof(EmaLength), 40)
			.SetDisplay("EMA Length", "EMA trend filter period", "Indicator");

		_cooldownBars = Param(nameof(CooldownBars), 380)
			.SetDisplay("Cooldown Bars", "Bars between trades", "Trading");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).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();
		_prevAo = 0;
		_barIndex = 0;
		_lastTradeBar = 0;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var ao = new AwesomeOscillator();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, ao, ProcessCandle)
			.Start();

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

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

		_barIndex++;

		var cooldownOk = _barIndex - _lastTradeBar > CooldownBars;

		// AO zero line crossover with EMA trend
		var aoCrossUp = _prevAo <= 0 && aoValue > 0;
		var aoCrossDown = _prevAo >= 0 && aoValue < 0;

		if (aoCrossUp && candle.ClosePrice > emaValue && Position <= 0 && cooldownOk)
		{
			BuyMarket();
			_lastTradeBar = _barIndex;
		}
		else if (aoCrossDown && candle.ClosePrice < emaValue && Position >= 0 && cooldownOk)
		{
			SellMarket();
			_lastTradeBar = _barIndex;
		}

		_prevAo = aoValue;
	}
}