在 GitHub 上查看

Billy Expert 策略

概览

  • 基于 MetaTrader 4 指标 "Billy_expert.mq4" 移植而来。
  • 仅做多:等待连续四根 K 线的最高价和开盘价依次降低,判定空头动能衰竭。
  • 通过两个随机指标确认:一个在交易级别,另一个在更高时间框架上运行。
  • 主要针对外汇对,但只要能提供分钟级蜡烛数据的品种都可使用。

信号逻辑

价格形态过滤

  1. 只处理已经收盘的 K 线。
  2. 需要满足 High[0] < High[1] < High[2] < High[3]Open[0] < Open[1] < Open[2] < Open[3],即四根蜡烛的高点和开盘价严格递减。
  3. 该形态表示下跌动能衰竭,为潜在的反转做准备。

随机指标确认

  1. 在交易时间框架和确认时间框架上分别计算随机指标,参数均为 5/3/3。
  2. 每个指标都要求 %K 在当前和前一根柱子上均位于 %D 之上(%K(0) > %D(0)%K(1) > %D(1))。
  3. 仅当两个随机指标同时给出看涨信号时才允许开仓。

仓位管理

  • 开仓:按策略的 Volume 参数发送市价买单;若存在空头仓位,会先对冲再反手。
  • 止损:根据 Stop Loss (pts) 参数,在入场价下方设置固定距离,值为 0 时不启用。
  • 止盈:根据 Take Profit (pts) 参数,在入场价上方设置固定距离,值为 0 时不启用。
  • 持仓上限:Max Orders 控制最多可同时持有的多头数量。由于 StockSharp 使用净仓位,策略通过将当前持仓量除以 Volume 近似还原 MT4 的订单数量。
  • 移动止损:原版虽有参数但未实现逻辑,移植版同样保持未实现,以保持一致性。

参数

名称 说明 默认值
Trading Candle 触发价格形态与快速随机指标的时间框架。 1 分钟
Slow Stochastic Candle 慢速随机指标使用的确认时间框架。 5 分钟
Stochastic Length %K 的计算长度。 5
%K Smoothing %K 的平滑周期。 3
%D Period %D 的平滑周期。 3
Slowing %K 的额外平滑。 3
Stop Loss (pts) 止损距离(价格步长单位)。 0
Take Profit (pts) 止盈距离(价格步长单位)。 12
Max Orders 同时允许的最大多头数量。 1

使用建议

  • 启动前请设置策略的 Volume,默认值为 0 会导致无法下单。
  • 价格步长优先使用 Security.PriceStep,其次是 Security.Step,若均未设置则使用 1 并记录警告,请确保品种信息正确。
  • 当确认时间框架不同于交易时间框架时,会沿用最近一根已完成的慢速随机值,直到新蜡烛生成,与 MT4 的行为一致。
  • 止损和止盈通过检测价格触及后发送市价单完成,相当于 MT4 中的服务器端执行。
  • Max Orders > 1 时,请保证每次加仓的 Volume 一致,以便正确估算订单数量。

与 MT4 版本的差异

  • 增加了价格步长的缺省检查,并在需要时输出警告日志。
  • 加入多重数据完整性判断(价格历史与两个随机指标)以避免过早交易。
  • 仅在蜡烛收盘后运行逻辑,避免像 MT4 那样在同一根柱子上重复触发,提高可预测性。
using System;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Long-only reversal strategy that looks for consecutive descending highs
/// and enters when RSI confirms oversold conditions with momentum turning up.
/// </summary>
public class BillyExpertReversalStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _rsiLength;

	private decimal _prevHigh1, _prevHigh2, _prevHigh3;
	private int _barCount;
	private decimal _prevRsi;
	private bool _hasPrevRsi;
	private decimal _entryPrice;

	public BillyExpertReversalStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for analysis.", "General");

		_rsiLength = Param(nameof(RsiLength), 14)
			.SetDisplay("RSI Length", "Length for RSI indicator.", "Indicators");
	}

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

	public int RsiLength
	{
		get => _rsiLength.Value;
		set => _rsiLength.Value = value;
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh1 = 0;
		_prevHigh2 = 0;
		_prevHigh3 = 0;
		_barCount = 0;
		_prevRsi = 50;
		_hasPrevRsi = false;
		_entryPrice = 0;
	}

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

		_prevHigh1 = 0;
		_prevHigh2 = 0;
		_prevHigh3 = 0;
		_barCount = 0;
		_prevRsi = 50;
		_hasPrevRsi = false;
		_entryPrice = 0;

		var rsi = new RelativeStrengthIndex { Length = RsiLength };

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

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

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

		_barCount++;

		var high = candle.HighPrice;
		var close = candle.ClosePrice;

		// Check descending highs pattern (3 consecutive lower highs)
		var descendingHighs = _barCount >= 4 &&
			high < _prevHigh1 &&
			_prevHigh1 < _prevHigh2 &&
			_prevHigh2 < _prevHigh3;

		// RSI turning up from oversold
		var rsiBullish = _hasPrevRsi && _prevRsi < 40 && rsiValue > _prevRsi;

		// Manage long position
		if (Position > 0)
		{
			// Exit on take-profit, stop-loss, or RSI overbought
			if (_entryPrice > 0 && close >= _entryPrice * 1.015m)
			{
				SellMarket();
			}
			else if (_entryPrice > 0 && close <= _entryPrice * 0.985m)
			{
				SellMarket();
			}
			else if (rsiValue > 75)
			{
				SellMarket();
			}
		}

		// Manage short position (exit only, this is mostly long-only)
		if (Position < 0)
		{
			if (rsiValue < 30)
			{
				BuyMarket();
			}
		}

		// Entry: descending highs (selling exhaustion) + RSI confirms reversal
		if (Position == 0)
		{
			if (descendingHighs && rsiBullish)
			{
				_entryPrice = close;
				BuyMarket();
			}
			// Also allow short on ascending lows pattern with overbought RSI
			else if (_barCount >= 4 && rsiValue > 70 && _prevRsi > 70)
			{
				_entryPrice = close;
				SellMarket();
			}
		}

		// Update history
		_prevHigh3 = _prevHigh2;
		_prevHigh2 = _prevHigh1;
		_prevHigh1 = high;
		_prevRsi = rsiValue;
		_hasPrevRsi = true;
	}
}