在 GitHub 上查看

Alexav SpeedUp M1 策略

概述

  • 将 MetaTrader 5 上的 “Alexav SpeedUp M1” 专家顾问移植到 StockSharp 高级 API。
  • 默认使用 1 分钟周期,在市场出现异常放大的 K 线实体时触发交易。
  • 只保持一个净头寸:当检测到强势蜡烛时按照其方向建仓,并结合固定止损、固定止盈与阶梯式移动止损进行风险管理。
  • 所有参数以点(pip)为单位输入,策略会根据品种的最小跳动和小数位数自动换算成价格距离。

原版思路与移植差异

  • 原始 EA 依赖对冲账户,会同时开多单和空单。StockSharp 策略在净持仓模式下运行,因此本移植版每次只保留一个方向的仓位,按照大实体蜡烛的方向入场。
  • 移动止损沿用 MT5 逻辑:只有当利润达到 TrailingStop + TrailingStep 点时才会把止损向盈利方向推进一个 TrailingStop 距离,并且只有当价格再前进一个 TrailingStep 才会继续上调。
  • Pip 距离通过乘以最小跳动值转换为价格单位;对于 3 位或 5 位小数的外汇品种,会额外乘以 10 以模拟 MT5 对 pip 的处理。

入场规则

  1. 使用所选周期的已完成 K 线(默认 1 分钟)。
  2. 计算 K 线实体:abs(Close - Open)
  3. 当实体大于 MinimumBodySizePips * pipSize 且当前没有持仓时,按照蜡烛方向开仓:
    • 阳线 → 建多单。
    • 阴线 → 建空单。

离场规则

  • 止损:距离开仓价 StopLossPips * pipSize,若参数为 0 则不设置。
  • 止盈:距离开仓价 TakeProfitPips * pipSize,若参数为 0 则不设置。
  • 移动止损TrailingStopPips > 0TrailingStepPips > 0 时启用):
    • 当浮盈达到 TrailingStopPips + TrailingStepPips 点后激活。
    • 多头:止损更新为 Close - TrailingStopPips * pipSize,且只有当价格较上一次止损至少再前进一个 TrailingStep 才会继续上调。
    • 空头:止损更新为 Close + TrailingStopPips * pipSize,并满足同样的阶梯条件。

参数说明

  • OrderVolume – 交易手数,默认 0.1
  • StopLossPips – 止损距离(点),默认 30
  • TakeProfitPips – 止盈距离(点),默认 90
  • TrailingStopPips – 移动止损距离(点),默认 10
  • TrailingStepPips – 移动止损每次调整所需的最小利润(点),默认 5,启用移动止损时必须大于 0。
  • MinimumBodySizePips – 触发信号所需的最小实体(点),默认 100
  • CandleType – 计算所用的蜡烛周期,默认 1 Minute

可视化

  • 策略在有可用图表区域时会自动绘制所选蜡烛序列以及自身成交,便于回测与调试。

使用建议

  • 默认参数复制自 MT5 版本,可根据标的波动率调整各个距离参数。
  • 由于只支持净持仓,请勿在需要同时持有多空仓位的对冲环境中运行。
  • 若标的最小跳动较大,请相应减小 pip 参数,以保持相似的价格距离。
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>
/// Candle breakout strategy converted from the Alexav SpeedUp M1 expert advisor.
/// Enters in the direction of strong candle bodies and manages exits with optional stop-loss,
/// take-profit, and trailing stop logic.
/// </summary>
public class AlexavSpeedUpM1Strategy : Strategy
{
	private readonly StrategyParam<decimal> _orderVolume;
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _takeProfitPips;
	private readonly StrategyParam<int> _trailingStopPips;
	private readonly StrategyParam<int> _trailingStepPips;
	private readonly StrategyParam<int> _minimumBodySizePips;
	private readonly StrategyParam<DataType> _candleType;

	private Sides? _currentDirection;
	private decimal _entryPrice;
	private decimal? _stopPrice;
	private decimal? _takeProfitPrice;
	private decimal? _trailingStopDistance;
	private decimal? _trailingStepDistance;

	/// <summary>
	/// Order volume in lots.
	/// </summary>
	public decimal OrderVolume
	{
		get => _orderVolume.Value;
		set => _orderVolume.Value = value;
	}

	/// <summary>
	/// Stop-loss distance in pips.
	/// </summary>
	public int StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// Take-profit distance in pips.
	/// </summary>
	public int TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in pips.
	/// </summary>
	public int TrailingStopPips
	{
		get => _trailingStopPips.Value;
		set => _trailingStopPips.Value = value;
	}

	/// <summary>
	/// Trailing step in pips.
	/// </summary>
	public int TrailingStepPips
	{
		get => _trailingStepPips.Value;
		set => _trailingStepPips.Value = value;
	}

	/// <summary>
	/// Minimum candle body size required to open a trade, expressed in pips.
	/// </summary>
	public int MinimumBodySizePips
	{
		get => _minimumBodySizePips.Value;
		set => _minimumBodySizePips.Value = value;
	}

	/// <summary>
	/// Type of candles used for calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of <see cref="AlexavSpeedUpM1Strategy"/>.
	/// </summary>
	public AlexavSpeedUpM1Strategy()
	{
		_orderVolume = Param(nameof(OrderVolume), 0.1m)
		.SetGreaterThanZero()
		.SetDisplay("Order Volume", "Position size in lots", "General");

		_stopLossPips = Param(nameof(StopLossPips), 30)
		.SetDisplay("Stop Loss (pips)", "Stop-loss distance in pips", "Risk Management")
		
		.SetOptimize(10, 100, 10);

		_takeProfitPips = Param(nameof(TakeProfitPips), 90)
		.SetDisplay("Take Profit (pips)", "Take-profit distance in pips", "Risk Management")
		
		.SetOptimize(30, 180, 30);

		_trailingStopPips = Param(nameof(TrailingStopPips), 10)
		.SetDisplay("Trailing Stop (pips)", "Trailing stop distance in pips", "Risk Management")
		
		.SetOptimize(5, 30, 5);

		_trailingStepPips = Param(nameof(TrailingStepPips), 5)
		.SetDisplay("Trailing Step (pips)", "Price movement required to move the trailing stop", "Risk Management")
		
		.SetOptimize(5, 20, 5);

		_minimumBodySizePips = Param(nameof(MinimumBodySizePips), 100)
		.SetDisplay("Minimum Body (pips)", "Minimum candle body size to trigger entries", "Signal")
		
		.SetOptimize(50, 200, 10);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
		.SetDisplay("Candle Type", "Type of candles for analysis", "General");
	}

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

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

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

		if (TrailingStopPips > 0 && TrailingStepPips == 0)
			throw new InvalidOperationException("Trailing step must be greater than zero when trailing stop is enabled.");

		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 (_currentDirection != null && Position == 0)
			ResetPositionState();

		if (_currentDirection != null)
		{
			if (ManageActivePosition(candle))
				return;
		}


		var pipSize = GetPipSize();
		var minimumBody = MinimumBodySizePips <= 0 ? 0m : MinimumBodySizePips * pipSize;
		var bodySize = Math.Abs(candle.ClosePrice - candle.OpenPrice);

		if (bodySize <= minimumBody)
			return;

		if (_currentDirection != null)
			return;

		var direction = candle.ClosePrice >= candle.OpenPrice ? Sides.Buy : Sides.Sell;
		OpenPosition(direction, candle.ClosePrice);
	}

	private bool ManageActivePosition(ICandleMessage candle)
	{
		if (_currentDirection == null)
			return false;

		var high = candle.HighPrice;
		var low = candle.LowPrice;
		var close = candle.ClosePrice;

		if (_currentDirection == Sides.Buy)
		{
			if (_stopPrice is decimal stop && low <= stop)
			{
				ClosePosition();
				return true;
			}

			if (_takeProfitPrice is decimal take && high >= take)
			{
				ClosePosition();
				return true;
			}

			UpdateTrailingStopForLong(close);
		}
		else if (_currentDirection == Sides.Sell)
		{
			if (_stopPrice is decimal stop && high >= stop)
			{
				ClosePosition();
				return true;
			}

			if (_takeProfitPrice is decimal take && low <= take)
			{
				ClosePosition();
				return true;
			}

			UpdateTrailingStopForShort(close);
		}

		return false;
	}

	private void OpenPosition(Sides direction, decimal price)
	{
		if (OrderVolume <= 0)
			return;

		var desiredPosition = direction == Sides.Buy ? OrderVolume : -OrderVolume;
		var difference = desiredPosition - Position;

		if (difference > 0)
			BuyMarket(difference);
		else if (difference < 0)
			SellMarket(-difference);

		_currentDirection = direction;
		_entryPrice = price;

		var pipSize = GetPipSize();

		_stopPrice = StopLossPips > 0
			? direction == Sides.Buy
				? price - StopLossPips * pipSize
				: price + StopLossPips * pipSize
			: null;

		_takeProfitPrice = TakeProfitPips > 0
			? direction == Sides.Buy
				? price + TakeProfitPips * pipSize
				: price - TakeProfitPips * pipSize
			: null;

		if (TrailingStopPips > 0)
		{
			_trailingStopDistance = TrailingStopPips * pipSize;
			_trailingStepDistance = TrailingStepPips * pipSize;
		}
		else
		{
			_trailingStopDistance = null;
			_trailingStepDistance = null;
		}
	}

	private void ClosePosition()
	{
		var currentPosition = Position;

		if (currentPosition > 0)
			SellMarket(currentPosition);
		else if (currentPosition < 0)
			BuyMarket(-currentPosition);

		ResetPositionState();
	}

	private void UpdateTrailingStopForLong(decimal price)
	{
		if (_trailingStopDistance is not decimal trailing || _trailingStepDistance is not decimal step)
			return;

		if (price - _entryPrice < trailing + step)
			return;

		var candidate = price - trailing;

		if (_stopPrice is decimal stop && stop >= candidate - step)
			return;

		_stopPrice = candidate;
	}

	private void UpdateTrailingStopForShort(decimal price)
	{
		if (_trailingStopDistance is not decimal trailing || _trailingStepDistance is not decimal step)
			return;

		if (_entryPrice - price < trailing + step)
			return;

		var candidate = price + trailing;

		if (_stopPrice is decimal stop && stop <= candidate + step)
			return;

		_stopPrice = candidate;
	}

	private void ResetPositionState()
	{
		_currentDirection = null;
		_entryPrice = 0m;
		_stopPrice = null;
		_takeProfitPrice = null;
		_trailingStopDistance = null;
		_trailingStepDistance = null;
	}

	private decimal GetPipSize()
	{
		var step = Security?.PriceStep ?? 0.0001m;
		var decimals = Security?.Decimals ?? 5;

		if (decimals == 3 || decimals == 5)
			return step * 10m;

		return step;
	}
}