在 GitHub 上查看

比尔·威廉姆斯第二智者策略

该策略实现了比尔·威廉姆斯交易系统中的第二个“智者”形态。 通过分析 Awesome Oscillator(AO)柱状图来捕捉动量变化:

  • 买入:当 AO 大于零并形成一个高点,之后出现三个依次降低的柱子。
  • 卖出:当 AO 小于零并形成一个低点,之后出现三个依次升高的柱子。

每当出现信号时,策略将关闭反向仓位并按信号方向开新仓。 默认使用四小时K线,可通过参数修改时间框架。

策略没有设置止损或止盈;只有在出现相反信号时才会翻转仓位。 策略还会在图表上绘制K线、AO指标以及成交记录,便于可视化分析。

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>
/// Bill Williams Wise Man 2 strategy using Awesome Oscillator.
/// </summary>
public class BWWiseMan2Strategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal _ao0;
	private decimal _ao1;
	private decimal _ao2;
	private int _aoCount;

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

	public BWWiseMan2Strategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_ao0 = 0;
		_ao1 = 0;
		_ao2 = 0;
		_aoCount = 0;
	}

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

		var ao = new AwesomeOscillator();

		SubscribeCandles(CandleType)
			.Bind(ao, ProcessCandle)
			.Start();
	}

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

		_ao2 = _ao1;
		_ao1 = _ao0;
		_ao0 = aoValue;

		if (_aoCount < 3)
		{
			_aoCount++;
			return;
		}

		var buySignal = (_ao2 < 0 && _ao1 < 0 && _ao0 > 0) || (_ao2 < _ao1 && _ao1 < _ao0 && _ao0 > 0);
		var sellSignal = (_ao2 > 0 && _ao1 > 0 && _ao0 < 0) || (_ao2 > _ao1 && _ao1 > _ao0 && _ao0 < 0);

		if (buySignal && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (sellSignal && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
	}
}