在 GitHub 上查看

Autotrader Momentum 策略

概述

Autotrader Momentum 策略 是对 MetaTrader 5 专家顾问 Autotrader Momentum (barabashkakvn 版本) 的移植。策略通过比较监控 K 线与历史参考 K 线的收盘价来识别动量方向:当收盘价高于参考值时判定为多头动量,低于参考值时判定为空头动量,并立即在市场价执行订单。实现完全基于 StockSharp 的高级 API,保持了原脚本“新 K 线触发”的处理方式。

为保持与 MQL 脚本一致的点值控制,止损、止盈与跟踪止损均以“点 (pip)”为单位配置,并根据交易品种的 PriceStep 自动换算为价格偏移量。当报价精度为 3 位或 5 位小数时,会额外乘以 10,复现原策略对三位/五位报价的点值调整。每根完成的 K 线在判断新信号之前都会先执行跟踪止损与保护性退出逻辑,确保风险控制优先。

交易流程

  1. 订阅配置的 CandleType,仅处理状态为 Finished 的 K 线,模拟 EA 只在新 K 线生成时做出决策。
  2. 维护一个长度为 max(CurrentBarIndex, ComparableBarIndex) + 1 的收盘价窗口。
  3. 计算监控 K 线 (CurrentBarIndex,默认 0) 与历史参考 K 线 (ComparableBarIndex,默认 15) 的收盘价差。
  4. 若监控收盘价高于参考收盘价,则平掉所有空头仓位并按配置的交易量开多。
  5. 若监控收盘价低于参考收盘价,则平掉所有多头仓位并按配置的交易量开空。
  6. 每次开仓都会重新计算加权平均建仓价,并刷新止损、止盈和跟踪止损价格。

StockSharp 采用净头寸模型,因此在反向信号出现时,会先补足相反方向的持仓量,再加上配置的基础交易量,实现与 MQL 版本“先平后开”的效果。

参数说明

  • CandleType – 用于比较的 K 线类型,默认 1 小时。
  • TradeVolume – 每次信号使用的基础成交量,反手时还会加上对冲所需的量。
  • StopLossPips – 止损距离(点)。设为 0 可关闭固定止损。
  • TakeProfitPips – 止盈距离(点)。设为 0 可关闭固定止盈。
  • TrailingStopPips – 跟踪止损距离(点)。设为 0 可关闭跟踪止损。
  • TrailingStepPips – 推进跟踪止损所需的最小有利波动(点),启用跟踪止损时必须大于 0。
  • CurrentBarIndex – 监控 K 线的索引,0 表示最新完成的 K 线。
  • ComparableBarIndex – 用于比较的历史 K 线索引。

所有以点为单位的设置都会依据 PriceStep 换算成真实价格偏移。当最小报价单位代表三位或五位小数时,会乘以 10 来模拟 MetaTrader 中的点值定义。

风险控制

  • 固定止损/止盈:StopLossPipsTakeProfitPips 大于 0 时,策略会基于加权建仓价维护对应的止损、止盈价位。
  • 跟踪止损:TrailingStopPipsTrailingStepPips 均大于 0 时启用。只有当价格相对建仓价的有利波动超过 TrailingStopPips + TrailingStepPips 时,才会推进止损,复现原脚本中“移动前需足够波动”的限制。
  • 状态清理: 当仓位被策略或外部操作清零时,会立即清空缓存的止损/止盈信息,避免遗留无效价格水平。

实现细节

  • 仅使用 StockSharp 的高层 API(BuyMarketSellMarket),遵循移植规范,不额外维护指标集合。
  • 通过滚动列表缓存收盘价,使 CurrentBarIndexComparableBarIndex 可在运行时调整。
  • 由于采用净头寸模式,多次同向加仓会实时重新计算加权建仓价,再据此刷新风险参数。
  • 在每根 K 线的信号评估之前先执行跟踪止损与保护性退出,避免在已触发离场的 K 线上重复开仓。

原始策略信息

  • 来源: MQL/22409/Autotrader Momentum.mq5
  • 作者: barabashkakvn(MetaTrader 社区)
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>
/// Momentum strategy converted from the MetaTrader 5 expert advisor "Autotrader Momentum".
/// Compares the most recent closing price with a historical reference bar and reverses positions when momentum shifts.
/// Includes configurable fixed stops, take profit targets, and an optional trailing stop engine measured in pips.
/// </summary>
public class AutotraderMomentumStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _takeProfitPips;
	private readonly StrategyParam<int> _trailingStopPips;
	private readonly StrategyParam<int> _trailingStepPips;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<int> _currentBarIndex;
	private readonly StrategyParam<int> _comparableBarIndex;

	private readonly List<decimal> _closeHistory = new();

	private decimal? _entryPrice;
	private decimal? _stopPrice;
	private decimal? _takeProfitPrice;
	private bool _isLongPosition;

	private decimal _pipValue;
	private decimal _stopLossOffset;
	private decimal _takeProfitOffset;
	private decimal _trailingStopOffset;
	private decimal _trailingStepOffset;
	private int _cooldownLeft;

	/// <summary>
	/// Initializes a new instance of the <see cref="AutotraderMomentumStrategy"/> class.
	/// </summary>
	public AutotraderMomentumStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Primary timeframe for price comparisons", "Data");

		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetDisplay("Trade Volume", "Base order volume used for market entries", "Trading")
			.SetGreaterThanZero();

		_stopLossPips = Param(nameof(StopLossPips), 50)
			.SetDisplay("Stop Loss (pips)", "Protective stop distance expressed in pips", "Risk")
			.SetNotNegative();

		_takeProfitPips = Param(nameof(TakeProfitPips), 50)
			.SetDisplay("Take Profit (pips)", "Profit target distance expressed in pips", "Risk")
			.SetNotNegative();

		_trailingStopPips = Param(nameof(TrailingStopPips), 0)
			.SetDisplay("Trailing Stop (pips)", "Distance maintained by the trailing stop in pips", "Risk")
			.SetNotNegative();

		_trailingStepPips = Param(nameof(TrailingStepPips), 5)
			.SetDisplay("Trailing Step (pips)", "Minimum progress before the trailing stop advances", "Risk")
			.SetNotNegative();

		_cooldownBars = Param(nameof(CooldownBars), 2)
			.SetDisplay("Cooldown Bars", "Bars to wait after entries and exits", "Risk")
			.SetNotNegative();

		_currentBarIndex = Param(nameof(CurrentBarIndex), 0)
			.SetDisplay("Current Bar Index", "Index of the candle used as the signal source", "Logic")
			.SetNotNegative();

		_comparableBarIndex = Param(nameof(ComparableBarIndex), 8)
			.SetDisplay("Comparable Bar Index", "Historical candle index used for momentum comparison", "Logic")
			.SetNotNegative();
	}

	/// <summary>
	/// Gets or sets the candle type used for generating signals.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Gets or sets the base order volume.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set => _tradeVolume.Value = value;
	}

	/// <summary>
	/// Gets or sets the stop-loss distance in pips.
	/// </summary>
	public int StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// Gets or sets the take-profit distance in pips.
	/// </summary>
	public int TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

	/// <summary>
	/// Gets or sets the trailing stop distance in pips.
	/// </summary>
	public int TrailingStopPips
	{
		get => _trailingStopPips.Value;
		set => _trailingStopPips.Value = value;
	}

	/// <summary>
	/// Gets or sets the trailing step distance in pips.
	/// </summary>
	public int TrailingStepPips
	{
		get => _trailingStepPips.Value;
		set => _trailingStepPips.Value = value;
	}

	/// <summary>
	/// Gets or sets the index of the candle considered the "current" bar in comparisons.
	/// </summary>
	public int CurrentBarIndex
	{
		get => _currentBarIndex.Value;
		set => _currentBarIndex.Value = value;
	}

	/// <summary>
	/// Gets or sets the index of the historical bar used for comparison.
	/// </summary>
	public int ComparableBarIndex
	{
		get => _comparableBarIndex.Value;
		set => _comparableBarIndex.Value = value;
	}

	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

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

		_closeHistory.Clear();
		ResetPositionState();

		_pipValue = 0m;
		_stopLossOffset = 0m;
		_takeProfitOffset = 0m;
		_trailingStopOffset = 0m;
		_trailingStepOffset = 0m;
		_cooldownLeft = 0;
	}

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

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

		Volume = TradeVolume;

		_pipValue = CalculatePipValue();
		_stopLossOffset = StopLossPips > 0 ? StopLossPips * _pipValue : 0m;
		_takeProfitOffset = TakeProfitPips > 0 ? TakeProfitPips * _pipValue : 0m;
		_trailingStopOffset = TrailingStopPips > 0 ? TrailingStopPips * _pipValue : 0m;
		_trailingStepOffset = TrailingStepPips > 0 ? TrailingStepPips * _pipValue : 0m;
		_cooldownLeft = 0;

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle)
	{
		// Ignore incomplete candles to mirror the original new-bar processing style.
		if (candle.State != CandleStates.Finished)
			return;

		if (_cooldownLeft > 0)
			_cooldownLeft--;

		// Update trailing and risk management before evaluating fresh signals.
		UpdateTrailingStop(candle);
		var exitTriggered = ManageProtectiveExits(candle);

		// Maintain the rolling window of closes used for momentum comparisons.
		UpdateCloseHistory(candle.ClosePrice);

		// Skip signal generation if an exit order has just been triggered on this bar.
		if (exitTriggered)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_cooldownLeft > 0)
			return;

		var requiredHistory = Math.Max(CurrentBarIndex, ComparableBarIndex) + 1;
		if (_closeHistory.Count < requiredHistory)
			return;

		var currentClose = GetCloseAtIndex(CurrentBarIndex);
		var comparableClose = GetCloseAtIndex(ComparableBarIndex);
		if (currentClose == null || comparableClose == null)
			return;

		// Enter long when the monitored bar closes above the reference bar.
		if (currentClose > comparableClose && Position <= 0)
		{
			EnterPosition(true, candle);
		}
		// Enter short when the monitored bar closes below the reference bar.
		else if (currentClose < comparableClose && Position >= 0)
		{
			EnterPosition(false, candle);
		}
	}

	private void UpdateCloseHistory(decimal closePrice)
	{
		var maxCount = Math.Max(CurrentBarIndex, ComparableBarIndex) + 1;
		if (maxCount <= 0)
			maxCount = 1;

		_closeHistory.Add(closePrice);
		if (_closeHistory.Count > maxCount)
			_closeHistory.RemoveAt(0);
	}

	private decimal? GetCloseAtIndex(int indexFromCurrent)
	{
		if (indexFromCurrent < 0)
			return null;

		var targetIndex = _closeHistory.Count - 1 - indexFromCurrent;
		if (targetIndex < 0 || targetIndex >= _closeHistory.Count)
			return null;

		return _closeHistory[targetIndex];
	}

	private void EnterPosition(bool isLong, ICandleMessage candle)
	{
		var baseVolume = TradeVolume;
		if (baseVolume <= 0m)
			return;

		var previousPosition = Position;
		decimal volume;

		if (isLong)
		{
			volume = baseVolume;
			if (previousPosition < 0m)
				volume += Math.Abs(previousPosition);

			if (volume <= 0m)
				return;

			// Buy enough volume to close any short exposure and add the configured trade size.
			BuyMarket(volume);

			if (previousPosition <= 0m)
			{
				// Treat reversals and fresh entries as a brand-new long position.
				_entryPrice = candle.ClosePrice;
			}
			else
			{
				// Blend the existing average price with the new fill to keep risk metrics consistent.
				var existingVolume = previousPosition;
				var totalVolume = existingVolume + baseVolume;
				if (totalVolume > 0m)
				{
					var existingEntry = _entryPrice ?? candle.ClosePrice;
					_entryPrice = (existingEntry * existingVolume + candle.ClosePrice * baseVolume) / totalVolume;
				}
			}

			_isLongPosition = true;
		}
		else
		{
			volume = baseVolume;
			if (previousPosition > 0m)
				volume += previousPosition;

			if (volume <= 0m)
				return;

			// Sell enough volume to close any long exposure and add the configured trade size.
			SellMarket(volume);

			if (previousPosition >= 0m)
			{
				// Treat reversals and fresh entries as a brand-new short position.
				_entryPrice = candle.ClosePrice;
			}
			else
			{
				// Blend the existing short average price with the new fill.
				var existingVolume = Math.Abs(previousPosition);
				var totalVolume = existingVolume + baseVolume;
				if (totalVolume > 0m)
				{
					var existingEntry = _entryPrice ?? candle.ClosePrice;
					_entryPrice = (existingEntry * existingVolume + candle.ClosePrice * baseVolume) / totalVolume;
				}
			}

			_isLongPosition = false;
		}

		_stopPrice = CalculateStopPrice(_isLongPosition, _entryPrice);
		_takeProfitPrice = CalculateTakeProfit(_isLongPosition, _entryPrice);
		_cooldownLeft = CooldownBars;
	}

	private decimal? CalculateStopPrice(bool isLong, decimal? entryPrice)
	{
		if (entryPrice == null || _stopLossOffset <= 0m)
			return null;

		return isLong ? entryPrice - _stopLossOffset : entryPrice + _stopLossOffset;
	}

	private decimal? CalculateTakeProfit(bool isLong, decimal? entryPrice)
	{
		if (entryPrice == null || _takeProfitOffset <= 0m)
			return null;

		return isLong ? entryPrice + _takeProfitOffset : entryPrice - _takeProfitOffset;
	}

	private void UpdateTrailingStop(ICandleMessage candle)
	{
		if (_trailingStopOffset <= 0m || _trailingStepOffset <= 0m || _entryPrice == null)
			return;

		if (Position > 0m)
		{
			var progress = candle.HighPrice - _entryPrice.Value;
			if (progress <= _trailingStopOffset + _trailingStepOffset)
				return;

			// Shift the trailing stop only when the move is large enough to respect the configured step.
			var desiredStop = candle.ClosePrice - _trailingStopOffset;
			if (_stopPrice is decimal currentStop)
			{
				if (desiredStop - currentStop >= _trailingStepOffset)
					_stopPrice = desiredStop;
			}
			else
			{
				_stopPrice = desiredStop;
			}
		}
		else if (Position < 0m)
		{
			var progress = _entryPrice.Value - candle.LowPrice;
			if (progress <= _trailingStopOffset + _trailingStepOffset)
				return;

			var desiredStop = candle.ClosePrice + _trailingStopOffset;
			if (_stopPrice is decimal currentStop)
			{
				if (currentStop - desiredStop >= _trailingStepOffset)
					_stopPrice = desiredStop;
			}
			else
			{
				_stopPrice = desiredStop;
			}
		}
	}

	private bool ManageProtectiveExits(ICandleMessage candle)
	{
		if (Position > 0m)
		{
			// Close the long position if the bar traded through the stop level.
			if (_stopPrice is decimal stop && candle.LowPrice <= stop)
			{
				SellMarket(Position);
				ResetPositionState();
				_cooldownLeft = CooldownBars;
				return true;
			}

			// Lock in profits once the take-profit threshold has been reached.
			if (_takeProfitPrice is decimal take && candle.HighPrice >= take)
			{
				SellMarket(Position);
				ResetPositionState();
				_cooldownLeft = CooldownBars;
				return true;
			}
		}
		else if (Position < 0m)
		{
			var volume = Math.Abs(Position);

			if (_stopPrice is decimal stop && candle.HighPrice >= stop)
			{
				BuyMarket(volume);
				ResetPositionState();
				_cooldownLeft = CooldownBars;
				return true;
			}

			if (_takeProfitPrice is decimal take && candle.LowPrice <= take)
			{
				BuyMarket(volume);
				ResetPositionState();
				_cooldownLeft = CooldownBars;
				return true;
			}
		}
		else
		{
			// Ensure cached state is flushed once all positions are closed externally.
			ResetPositionState();
		}

		return false;
	}

	private void ResetPositionState()
	{
		_entryPrice = null;
		_stopPrice = null;
		_takeProfitPrice = null;
		_isLongPosition = false;
	}

	private decimal CalculatePipValue()
	{
		var step = Security?.PriceStep ?? 0m;
		if (step <= 0m)
			return 1m;

		var scaled = step;
		var digits = 0;
		while (scaled < 1m && digits < 10)
		{
			scaled *= 10m;
			digits++;
		}

		// Adjust for three and five decimal quotes to emulate the MetaTrader point multiplier.
		var adjust = (digits == 3 || digits == 5) ? 10m : 1m;
		return step * adjust;
	}
}