在 GitHub 上查看

Golden Ratio Cubes策略

Golden Ratio Cubes策略利用斐波那契数计算金分率扩展。策略追踪一段时间内的最高价和最低价,并基于黄金分割(φ ≈ 1.618)计算突破水平。当价格收盘突破这些水平时,按照突破方向入场。

细节

  • 入场条件:
    • 收盘价高于黄金分割扩展水平 → 买入。
    • 收盘价低于黄金分割扩展水平 → 卖出。
  • 方向:多头和空头。
  • 出场条件:
    • 相反方向的突破信号。
  • 止损:无。
  • 默认参数:
    • Lookback = 34
    • Phi = 1.618
  • 过滤器:
    • 类型:突破
    • 方向:多头和空头
    • 指标:Highest, Lowest
    • 止损:无
    • 复杂度:低
    • 时间框架:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

/// <summary>
/// Golden Ratio Cubes Strategy.
/// The range spans the highest high and lowest low of the previous Lookback candles. Its golden ratio extensions are
/// lowest + Phi * range above and highest - Phi * range below. A close above the upper extension buys and a close below the
/// lower extension sells; an opposite breakout reverses the position.
/// </summary>
public class GoldenRatioCubesStrategy : Strategy
{
	private readonly StrategyParam<int> _lookback;
	private readonly StrategyParam<decimal> _phi;
	private readonly StrategyParam<DataType> _candleType;

	private Highest _highest;
	private Lowest _lowest;
	private decimal? _prevHighest;
	private decimal? _prevLowest;

	/// <summary>
	/// Candles the range spans.
	/// </summary>
	public int Lookback
	{
		get => _lookback.Value;
		set => _lookback.Value = value;
	}

	/// <summary>
	/// Golden ratio of the extensions.
	/// </summary>
	public decimal Phi
	{
		get => _phi.Value;
		set => _phi.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public GoldenRatioCubesStrategy()
	{
		_lookback = Param(nameof(Lookback), 34)
			.SetGreaterThanZero()
			.SetDisplay("Lookback", "Candles the range spans", "Indicators");

		_phi = Param(nameof(Phi), 1.618m)
			.SetGreaterThanZero()
			.SetDisplay("Phi", "Golden ratio of the extensions", "Indicators");

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

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

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

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

		_prevHighest = null;
		_prevLowest = null;

		_highest = new Highest { Length = Lookback };
		_lowest = new Lowest { Length = Lookback };

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

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

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

		// The range is measured on the candles before this one.
		var rangeHigh = _prevHighest;
		var rangeLow = _prevLowest;

		var highest = _highest.Process(candle.HighPrice, candle.OpenTime, true).ToDecimal();
		var lowest = _lowest.Process(candle.LowPrice, candle.OpenTime, true).ToDecimal();

		if (_highest.IsFormed && _lowest.IsFormed)
		{
			_prevHighest = highest;
			_prevLowest = lowest;
		}

		if (rangeHigh is not decimal high || rangeLow is not decimal low)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var range = high - low;
		var upperExtension = low + Phi * range;
		var lowerExtension = high - Phi * range;
		var close = candle.ClosePrice;

		if (close > upperExtension && Position <= 0)
			BuyMarket(Volume + Math.Abs(Position));
		else if (close < lowerExtension && Position >= 0)
			SellMarket(Volume + Math.Abs(Position));
	}
}