在 GitHub 上查看

Bykov Trend 策略

该策略在 StockSharp 高级 API 上重现了 MetaTrader 中的 "Bykov Trend" 系统。指标基于 Williams %R 振荡器,用于捕捉趋势反转。当趋势由空头转为多头时开多;当趋势由多头转为空头时开空。

系统在选定的时间框上只交易一个品种,并且同一时间只持有一个方向的仓位。相反信号会反转仓位。

细节

  • 入场条件
    • 多头:Williams %R 在低于 -100 + K 后上穿 -KK = 33 - Risk)。
    • 空头:Williams %R 在高于 -K 后下穿 -100 + K
  • 方向:双向。
  • 出场条件
    • 相反信号平掉当前仓位并反向开仓。
  • 止损:无。
  • 默认参数
    • Risk = 3 (K = 30)。
    • SSP = 9(Williams %R 周期)。
    • CandleType = 1 小时 K 线。
  • 过滤器
    • 分类:趋势跟随
    • 方向:双向
    • 指标:仅 Williams %R
    • 止损:无
    • 复杂度:简单
    • 时间框架:灵活
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Bykov Trend strategy based on Williams %R indicator.
/// Generates buy or sell signals when trend changes occur.
/// </summary>
public class BykovTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _risk;
	private readonly StrategyParam<int> _ssp;
	private readonly StrategyParam<DataType> _candleType;

	private bool _previousUptrend;

	/// <summary>
	/// Risk parameter from original indicator (K = 33 - Risk).
	/// </summary>
	public int Risk
	{
		get => _risk.Value;
		set => _risk.Value = value;
	}

	/// <summary>
	/// Williams %R period.
	/// </summary>
	public int Ssp
	{
		get => _ssp.Value;
		set => _ssp.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of <see cref="BykovTrendStrategy" />.
	/// </summary>
	public BykovTrendStrategy()
	{
		_risk = Param(nameof(Risk), 3)
			.SetGreaterThanZero()
			.SetDisplay("Risk", "Risk parameter from original indicator", "Indicator")
			
			.SetOptimize(1, 10, 1);

		_ssp = Param(nameof(Ssp), 9)
			.SetGreaterThanZero()
			.SetDisplay("SSP", "Williams %R period", "Indicator")
			
			.SetOptimize(5, 20, 1);

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

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

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

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

		var wpr = new WilliamsR { Length = Ssp };
		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(wpr, ProcessCandle)
			.Start();

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

	private void ProcessCandle(ICandleMessage candle, decimal wprValue)
	{
		// Process only finished candles
		if (candle.State != CandleStates.Finished)
			return;

		// Ensure that strategy is ready to trade
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var k = 33 - Risk;
		var uptrend = _previousUptrend;

		if (wprValue < -100 + k)
			uptrend = false;
		else if (wprValue > -k)
			uptrend = true;

		var buySignal = !_previousUptrend && uptrend;
		var sellSignal = _previousUptrend && !uptrend;

		_previousUptrend = uptrend;

		if (buySignal)
		{
			// Close short positions and enter long
			BuyMarket();
		}
		else if (sellSignal)
		{
			// Close long positions and enter short
			SellMarket();
		}
	}
}