在 GitHub 上查看

Eugene Inside Breakout 策略

Eugene Inside Breakout 策略是 barabashkakvn 原版 MetaTrader 智能交易系统的直接移植。策略完全基于价格行为:先等待内部 K 线的压缩,再捕捉随后的区间突破。通过前一根 K 线实体计算出的确认水平,可以在进场前验证突破是否具备动能。

概述

策略监控当前 K 线是否突破上一根 K 线的高点或低点。做多时要求上一根 K 线的最低价低于前两根 K 线的最高价,强调突破前的收敛。做空时若上一根 K 线是内部 K 线,策略会拒绝入场,与原始 MQL 逻辑完全一致。下单全部使用固定手数的市价单。

交易逻辑

  • 强调最近高点/低点的突破,以尽早跟随潜在趋势。
  • 利用上一根 K 线的实体计算出两个三分之一回撤位(zigLevelBuyzigLevelSell)。只有当价格触及相应水平,或当前时间超过设定的激活小时数时,突破才被允许。
  • 当突破发生在与交易方向相反的内部 K 线之后时,阻止开仓。
  • 当出现相反方向且已经确认的突破信号时平仓,保证策略要么空仓,要么顺应最新信号持仓。

入场规则

做多

  1. 当前最高价高于上一根 K 线最高价。
  2. 当前最低价跌破上一根 K 线实体三分之一的买入确认位,或当前小时数超过激活参数。
  3. 当前最低价高于上一根最低价,同时上一根最低价低于两根之前的最高价。
  4. 当前没有持仓。

做空

  1. 当前最低价低于上一根 K 线最低价。
  2. 当前最高价触及上一根 K 线实体三分之一的卖出确认位,或当前小时数超过激活参数。
  3. 上一根 K 线不是内部 K 线(其高点不低于、低点不高于再前一根 K 线)。
  4. 当前最高价低于上一根最高价。
  5. 当前没有持仓。

离场规则

  • 当满足做空确认条件时平掉多单。
  • 当满足做多确认条件时平掉空单。

参数

名称 说明 默认值
CandleType 策略处理的 K 线周期。 1 小时 K 线
Volume 每次市价单的下单手数。 0.1
ActivationHour 超过该小时数后自动认可确认条件,对应原 MQL 代码中的 TimeCurrent() 过滤。 8

说明

  • 原脚本中的 “white bird” 与 “black bird” 检查因条件设置始终为假,已保留以保持一致性,但不会影响交易。
  • 策略不使用其他指标或移动止损,完全依赖价格信号,并在每次相反突破时反转持仓。
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>
/// Breakout strategy derived from the Eugene expert advisor.
/// </summary>
public class EugeneInsideBreakoutStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _activationHour;

	private decimal _prevOpen1;
	private decimal _prevClose1;
	private decimal _prevHigh1;
	private decimal _prevLow1;

	private decimal _prevOpen2;
	private decimal _prevClose2;
	private decimal _prevHigh2;
	private decimal _prevLow2;

	private bool _hasPrev1;
	private bool _hasPrev2;

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


	/// <summary>
	/// Hour of day after which confirmations are automatically valid.
	/// </summary>
	public int ActivationHour
	{
		get => _activationHour.Value;
		set => _activationHour.Value = value;
	}

	/// <summary>
	/// Initializes <see cref="EugeneInsideBreakoutStrategy"/>.
	/// </summary>
	public EugeneInsideBreakoutStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to process", "General");


		_activationHour = Param(nameof(ActivationHour), 8)
			.SetRange(0, 23)
			.SetDisplay("Activation Hour", "Hour when confirmations become unconditional", "Filters");

		ResetHistory();
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		ResetHistory();
	}

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

		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;

		if (!_hasPrev2)
		{
			UpdateHistory(candle);
			return;
		}

		var open1 = _prevOpen1;
		var close1 = _prevClose1;
		var open2 = _prevOpen2;
		var close2 = _prevClose2;
		var high0 = candle.HighPrice;
		var high1 = _prevHigh1;
		var high2 = _prevHigh2;
		var low0 = candle.LowPrice;
		var low1 = _prevLow1;
		var low2 = _prevLow2;

		// Replicate the original expert advisor checks for inside bars.
		var blackInsider = high1 <= high2 && low1 >= low2 && close1 <= open1;
		var whiteInsider = high1 <= high2 && low1 >= low2 && close1 > open1;
		var whiteBird = whiteInsider && close2 > open2;
		var blackBird = blackInsider && close2 < open2;

		// ZigZag style confirmation levels based on the previous candle body.
		var zigLevelBuy = close1 < open1
			? open1 - (close1 - open1) / 3m
			: open1 - (open1 - low1) / 3m;

		var zigLevelSell = close1 > open1
			? open1 + (close1 - open1) / 3m
			: open1 + (high1 - open1) / 3m;

		var confirmBuy = (low0 <= zigLevelBuy || candle.CloseTime.Hour >= ActivationHour) && !blackBird && !whiteInsider;
		var confirmSell = (high0 >= zigLevelSell || candle.CloseTime.Hour >= ActivationHour) && !whiteBird && !blackInsider;

		var buySignal = high0 > high1;
		var sellSignal = low0 < low1;

		if (Position == 0)
		{
			if (buySignal && confirmBuy && low0 > low1 && low1 < high2)
			{
				BuyMarket();
			}
			else if (sellSignal && confirmSell && high0 < high1)
			{
				SellMarket();
			}
		}
		else if (Position > 0)
		{
			if (sellSignal && confirmSell && high0 < high1)
				SellMarket();
		}
		else if (Position < 0)
		{
			if (buySignal && confirmBuy && low0 > low1 && low1 < high2)
				BuyMarket();
		}

		UpdateHistory(candle);
	}

	private void UpdateHistory(ICandleMessage candle)
	{
		// Keep the two most recent completed candles for decision making.
		_prevOpen2 = _prevOpen1;
		_prevClose2 = _prevClose1;
		_prevHigh2 = _prevHigh1;
		_prevLow2 = _prevLow1;
		_hasPrev2 = _hasPrev1;

		_prevOpen1 = candle.OpenPrice;
		_prevClose1 = candle.ClosePrice;
		_prevHigh1 = candle.HighPrice;
		_prevLow1 = candle.LowPrice;
		_hasPrev1 = true;
	}

	private void ResetHistory()
	{
		_prevOpen1 = default;
		_prevClose1 = default;
		_prevHigh1 = default;
		_prevLow1 = default;
		_prevOpen2 = default;
		_prevClose2 = default;
		_prevHigh2 = default;
		_prevLow2 = default;
		_hasPrev1 = false;
		_hasPrev2 = false;
	}
}