在 GitHub 上查看

Explosion 区间扩张策略

概述

Explosion Strategy 源自 MetaTrader 5 指标交易程序 “Explosion”。策略比较当前收盘完成的 K 线高低差与前一根 K 线的区间,当当前区间大于前一区间乘以设定的 Range Ratio 时,在蜡烛实体方向开仓。StockSharp 版本保留了原始 EA 的主要风控机制,并增加了方便的交易时间窗口与移动止损参数。

交易规则

  • 区间放大:计算当前蜡烛的高低区间,与上一根蜡烛区间相乘 Range Ratio 比较,只有放大满足条件时才产生信号。
  • 方向判定
    • 若收盘价高于开盘价且当前仓位为空或做空,则开多单。
    • 若收盘价低于开盘价且当前仓位为空或做多,则开空单。
  • 交易时段:仅在蜡烛收盘时间位于 Start HourEnd Hour(包含边界)之间时才处理信号。
  • 每日限制:启用 One Trade Per Day 时,每个自然日只执行首个满足条件的交易。
  • 进场间隔:开仓后至少等待 Pause (sec) 秒才允许下一次进场。
  • 最大敞口:净头寸的绝对值不得超过 Max Positions * Order Volume

离场与风控

  • 初始保护:可选的止损与止盈以价格步长表示,并基于入场价计算。
  • 移动止损:当盈利达到 Trailing Stop + Trailing Step 后,止损价格会向利润方向推进,逻辑与原版 EA 保持一致。
  • 目标触发:若蜡烛在生命周期内触碰止损或止盈,立即通过市价单平仓。

参数说明

  • Candle Type – 用于计算的 K 线类型。
  • Order Volume – 每笔交易的手数(lot)。
  • Range Ratio – 当前区间需要超过前一区间的倍数。
  • Max Positions – 允许的最大净仓位手数。
  • Pause (sec) – 进场之间的最小等待时间(秒)。
  • Start Hour / End Hour – 允许交易的小时区间(0–23)。
  • One Trade Per Day – 是否限制每天仅一笔交易。
  • Stop Loss – 初始止损距离(以价格步长表示)。
  • Take Profit – 初始止盈距离(以价格步长表示)。
  • Trailing Stop – 移动止损的基础距离。
  • Trailing Step – 更新移动止损前所需的额外距离。

转换说明

  • 使用高层的 SubscribeCandles + Bind API,无需额外指标即可实现信号逻辑。
  • 移动止损、交易时段、间隔与每日限制等机制与 MQ5 版本一致。
  • 头寸规模由固定手数参数控制,未实现原 EA 中基于风险百分比的动态仓位计算。
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 ExplosionRangeExpansionStrategy : 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 ExplosionRangeExpansionStrategy()
	{
		_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;
	}
}