在 GitHub 上查看

Explosion 策略

该策略复现了 MetaTrader 上的 “Explosion” 专家顾问。它监控每根已完成 K 线的波动幅度,当最新的蜡烛出现“爆发”——其高度超过上一根蜡烛范围的两倍时入场。方向由实体决定:收盘价高于开盘价时做多,收盘价低于开盘价时做空。

交易规则

  1. 只处理来自 CandleType 订阅的已完成蜡烛。
  2. 计算当前区间 High - Low 并与前一根蜡烛的区间比较。
  3. currentRange > previousRange * 2 且收盘价高于开盘价时开多单。
  4. currentRange > previousRange * 2 且收盘价低于开盘价时开空单。
  5. OnlyOnePositionPerBar 启用时,每根蜡烛的开盘时间最多触发一次对应方向的交易;同一根蜡烛上的重复信号会被忽略。
  6. 策略以净头寸方式运行,因此反向信号会先平掉当前仓位,再建立新的方向。
  7. 风险控制:
    • StopLossPipsTakeProfitPips 在入场价附近设置以点为单位的虚拟止损/止盈。
    • TrailingStopPipsTrailingStepPips 在价格获利达到设定距离后启动并逐步上移/下移止损。
    • 可选的点值乘数模拟 MQL 的自动小数检测,在三位或五位小数的品种上将点值放大 10 倍。

参数

参数 默认值 说明
TradeVolume 0.01 入场时发送的市价单数量。
StopLossPips 20 止损距离(点)。为 0 表示关闭止损。
TakeProfitPips 10 止盈距离(点)。为 0 表示关闭止盈。
TrailingStopPips 25 跟踪止损的激活距离(点)。为 0 表示关闭跟踪。
TrailingStepPips 5 每次推进跟踪止损前所需的额外盈利(点)。启用跟踪时必须为正。
UseAutoPipMultiplier true 在 3/5 位小数的品种上将点值乘以 10,复现自动小数逻辑。
OnlyOnePositionPerBar true 限制每根蜡烛只建一次仓。
CandleType TimeSpan.FromMinutes(1).TimeFrame() 用于计算信号的蜡烛序列。

转换说明

  • StockSharp 采用净头寸制度,无法像原始 EA 那样同时持有多个对冲订单;该实现等价于在 MQL 中开启 OnlyOneOpenedPos 选项。
  • 跟踪止损在蜡烛收盘时更新,而非逐笔报价。阈值与原始逻辑一致,同时符合高层 API 的工作方式。
  • 点值乘数重现了自动识别小数位数的功能,可在五位报价的外汇品种上保持与原策略相同的距离计算。

使用建议

  1. 选择与原 EA 相同的交易品种和时间周期(如外汇 M15/M30 图表)。
  2. 根据标的波动性调整各项以点计的风险参数。
  3. 建议开启日志,观察跟踪止损的推进及保护水平的更新情况。
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 ExplosionStrategy : 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 ExplosionStrategy()
	{
		_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;
	}
}