在 GitHub 上查看

Surefirething 策略

概述

Surefirething 策略复刻了 MetaTrader 5 上的经典智能交易系统,它会在最新完成的K线收盘价附近同时挂出对称的买入和卖出限价单。每根K线结束后都会重建网格,所有止损与止盈都以点(pip)为单位管理,并且会在服务器时间23:50强制平掉全部仓位。

K线处理

  • 支持可配置的K线类型(默认:1小时周期)。
  • 当一根K线收盘后,计算放大的区间:range = (high - low) * 1.1
  • 根据该区间得到两个突破价位:
    • L4 = close - range / 2:买入限价单。
    • H4 = close + range / 2:卖出限价单。
  • 在发布新网格之前会取消已有的挂单,保证始终只有一张买入限价单和一张卖出限价单。

挂单管理

  • L4H4 位置以设定的下单量挂出限价单。
  • 只要有一笔交易成交,就会立刻取消另一侧的挂单。
  • 每天 23:50(服务器时间)执行收盘流程:
    • 取消所有剩余挂单。
    • 如有持仓则以市价全部平仓。
    • 重置止损与止盈跟踪,为下一交易日做准备。

风险控制

  • 止损与止盈距离以点数表示,并通过合约的最小报价步长转换为具体价格(对 5 位和 3 位小数的品种自动换算成传统的 1 个 pip)。
  • 可以启用点数单位的移动止损。当价格向有利方向移动超过 TrailingStopPips + TrailingStepPips 时,止损会被推进到:多单为 当前价 - TrailingStopPips,空单为 当前价 + TrailingStopPips
  • 每根K线都会检查是否触发止损或止盈。一旦价格穿越相应价位,立即用市价单离场。

参数

  • OrderVolume – 两侧限价单的基础下单量(默认值 0.1)。
  • StopLossPips – 止损距离(点)(默认值 50)。
  • TakeProfitPips – 止盈距离(点)(默认值 50)。
  • TrailingStopPips – 移动止损距离(点)(默认值 25)。
  • TrailingStepPips – 触发移动止损所需的额外点数(默认值 1)。启用移动止损时必须大于0。
  • CandleType – 用于计算的K线类型(默认:1小时)。

说明

  • 实现遵循原始 MQL 版本的约束:启用移动止损时不允许将移动步长设为0。
  • 本策略暂不提供 Python 版本。
namespace StockSharp.Samples.Strategies;

using System;

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

/// <summary>
/// Surefirething breakout strategy.
/// Buys when price breaks above the previous candle's high, sells when price breaks below the previous candle's low.
/// Uses EMA as a trend filter.
/// </summary>
public class SurefirethingStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _emaPeriod;

	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _initialized;

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

	public int EmaPeriod
	{
		get => _emaPeriod.Value;
		set => _emaPeriod.Value = value;
	}

	public SurefirethingStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Time frame used for signals", "General");

		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "Trend filter EMA period", "Indicators");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0m;
		_prevLow = 0m;
		_initialized = false;
	}

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

		_prevHigh = 0;
		_prevLow = 0;
		_initialized = false;

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, OnProcess)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (!_initialized)
		{
			_prevHigh = candle.HighPrice;
			_prevLow = candle.LowPrice;
			_initialized = true;
			return;
		}

		// Breakout above previous high with EMA filter
		if (candle.ClosePrice > _prevHigh && candle.ClosePrice > emaValue && Position <= 0)
		{
			BuyMarket();
		}
		// Breakout below previous low with EMA filter
		else if (candle.ClosePrice < _prevLow && candle.ClosePrice < emaValue && Position >= 0)
		{
			SellMarket();
		}

		_prevHigh = candle.HighPrice;
		_prevLow = candle.LowPrice;
	}
}