在 GitHub 上查看

800BB 策略

本策略使用 StockSharp 的高级 API 复刻 MetaTrader 4 的 “800BB” 智能交易系统。价格突破超长周期的布林带后,只要下一根 K 线重新回到通道内,系统就执行均值回归交易。风险控制依靠 ATR 倍数的止损/止盈距离,并结合 RiskPercent 参数根据账户权益动态计算下单数量。

概览

  • 适用于通过 CandleType 参数提供的任意品种与时间框架。
  • 使用周期为 800、标准差系数为 2 的布林带来识别极端行情。
  • 只有当价格在突破后第一根 K 线重新开盘于带内时才确认信号。
  • 通过 ATR 估算的止损距离换算成“点”,再乘以所选风险百分比来确定下单量。
  • 当品种报价保留 3 或 5 位小数时,会像 MetaTrader 一样把价格步长乘以 10 计算点值。

交易逻辑

做多条件

  1. 前一根已完成 K 线的开盘价或收盘价低于下轨,说明价格向下突破。
  2. 当前 K 线开盘价位于或高于上一根下轨,表示价格重新回到带内。
  3. 当前没有持有多单;若有空单,会先行平仓。
  4. 根据 ATR 止损距离与设定的风险百分比计算下单量。
  5. 在该根 K 线开盘价买入,止损设在 StopLossAtrMultiplier × ATR 下方,止盈设在 TakeProfitAtrMultiplier × ATR 上方。

做空条件

  1. 前一根已完成 K 线的开盘价或收盘价高于上轨,说明价格向上突破。
  2. 当前 K 线开盘价位于或低于上一根上轨,表示价格重新回到带内。
  3. 当前没有持有空单;若有多单,会先行平仓。
  4. 根据同样的 ATR 与风险百分比计算下单量。
  5. 在该根 K 线开盘价卖出,止损设在 StopLossAtrMultiplier × ATR 上方,止盈设在 TakeProfitAtrMultiplier × ATR 下方。

出场管理

  • 保护性订单: 策略在每根已完成的 K 线上检测内部保存的止损和止盈水平,一旦突破就以市价平仓。
  • 反向信号: 出现反向条件时,会先平掉现有仓位,再建立新仓。
  • 可视化: 原始 EA 会绘制可能交易的竖线,本实现只在可用的图表上绘制 K 线、布林带以及自身成交记录。

参数

参数 默认值 说明
RiskPercent 2 每笔交易风险占账户权益的百分比。
TakeProfitAtrMultiplier 1.5 计算止盈距离的 ATR 倍数。
StopLossAtrMultiplier 1 计算止损距离的 ATR 倍数。
AtrPeriod 14 ATR 指标的回溯周期。
BollingerPeriod 800 布林带移动平均的周期。
BollingerDeviation 2 布林带的标准差倍数。
CandleType 1 小时 用于生成信号的时间框架或其他蜡烛类型。

注意事项

  • 需要投资组合适配器提供 Portfolio.CurrentValue,否则风险计算返回零,策略不会下单。
  • 如果品种没有有效的价格步长或 tick 价值,点值计算会回退到保守默认值。
  • 由于布林带周期极长(800 根 K 线),必须先累积足够的历史数据以完成 Bollinger 和 ATR 的初始化,策略才会开始交易。
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;

public class EightHundredBbStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public EightHundredBbStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}