在 GitHub 上查看

固定风险资金管理策略

概述

固定风险资金管理策略是 MetaTrader 5 EA Money Fixed Risk.mq5 的移植版本。原始脚本会定期计算在不超过账户权益固定百分比风险的情况下可以开出的最大仓位,并以该仓位开多单,同时设置对称的止损和止盈。本策略使用 StockSharp 的高级 API 复现同样的逻辑:订阅逐笔成交、计算仓位大小,并在进入后监控止损与止盈。

策略通过 SubscribeTrades() 订阅所有成交。当内部计数器累计到指定的 tick 数量时,会读取投资组合的当前权益,将配置的止损点数转换为价格距离,然后计算在给定风险百分比下允许的最大成交量。如果计算得到的成交量有效,策略会市价买入,止损与止盈分别放在入场价上下等距离位置,并在后续每个 tick 上检查是否触发退出条件。

数据要求

  • 需要逐笔成交数据,因为策略逻辑依赖 tick 计数,蜡烛数据不会触发信号。
  • 证券必须正确设置 PriceStepStepPriceVolumeStepMinVolume 以及可选的 MaxVolume,以便仓位计算符合交易所规则。

工作流程

  1. 通过 SubscribeTrades() 监听成交价和成交量。
  2. 每收到一个成交就递增内部计数器。
  3. 当计数器达到 Ticks Interval 时重置计数器,并执行:
    • 根据 PriceStepDecimals 计算每个 pip 的价格单位,对 3 位或 5 位小数报价自动乘以 10。
    • 将配置的止损点数转换成价格距离。
    • 依次读取 Portfolio.CurrentValueCurrentBalanceBeginValue,获取最新权益。
    • 根据止损距离与 StepPrice 计算单合约的货币风险。
    • 用风险限额除以单合约风险得到最大仓位,并根据交易所的数量步长与上下限进行归一化。
  4. 若归一化后的仓位大于最小交易量,则平掉任何空头头寸并按计算量开多单。
  5. 将止损与止盈价记录下来,并在后续 tick 中检查价格是否触发退出,触发后立即平仓。

参数说明

  • Stop Loss (pips) – 以 pip 表示的止损距离,止盈与止损距离相等。
  • Risk % – 每笔交易风险占账户权益的百分比。
  • Ticks Interval – 每隔多少个 tick 重新评估仓位并尝试进场。

所有参数都要求大于零,并支持优化。

资金管理细节

  • 风险金额 = Equity * (Risk % / 100)
  • 价格止损距离 = Stop Loss (pips) * pip size,其中 pip 大小在 3/5 位小数报价中等于 PriceStep * 10,否则等于 PriceStep
  • 单合约货币风险 = (stop distance / PriceStep) * StepPrice
  • 仓位 = 风险金额 / 单合约货币风险,向下按 VolumeStep 取整,并受 MinVolume/MaxVolume 限制。若低于最小交易量则跳过下单。

与原始 EA 的差异

  • 完全基于 StockSharp,未使用 MetaTrader API。
  • 调用了 StartProtection(),方便与平台级风险控制配合。
  • 使用策略投资组合的权益数据替代 MQL 中的账户信息类。
  • 通过 tick 监控来触发止损/止盈,示例中无需显式挂出保护性委托。

使用建议

  • 策略仅实现多头交易,与原脚本一致。如需做空,可在 ProcessTrade 中扩展逻辑。
  • 回测时需准备足够密集的逐笔数据,否则 tick 计数达不到阈值。
  • 在实盘前请确认证券元数据(最小变动价位、价格步长、最小手数等)与真实交易规则一致。
  • 示例遵循转换指南,不额外创建数据集合,而是通过字段维护状态。
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>
/// Recreates the Money Fixed Risk expert advisor using StockSharp's high level API.
/// Uses ATR to determine position sizing via risk percentage and opens long positions
/// with symmetric stop-loss and take-profit levels based on ATR.
/// </summary>
public class MoneyFixedRiskStrategy : Strategy
{
	private readonly StrategyParam<decimal> _atrMultiplier;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<int> _candleInterval;
	private readonly StrategyParam<DataType> _candleType;

	private int _candleCounter;
	private decimal _stopPrice;
	private decimal _takeProfitPrice;
	private decimal _entryPrice;

	/// <summary>
	/// ATR multiplier for stop distance.
	/// </summary>
	public decimal AtrMultiplier
	{
		get => _atrMultiplier.Value;
		set => _atrMultiplier.Value = value;
	}

	/// <summary>
	/// ATR calculation period.
	/// </summary>
	public int AtrPeriod
	{
		get => _atrPeriod.Value;
		set => _atrPeriod.Value = value;
	}

	/// <summary>
	/// Number of candles between position evaluations.
	/// </summary>
	public int CandleInterval
	{
		get => _candleInterval.Value;
		set => _candleInterval.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="MoneyFixedRiskStrategy"/>.
	/// </summary>
	public MoneyFixedRiskStrategy()
	{
		_atrMultiplier = Param(nameof(AtrMultiplier), 1.5m)
			.SetGreaterThanZero()
			.SetDisplay("ATR Multiplier", "Multiplier for ATR-based stop distance", "Risk")
			.SetOptimize(0.5m, 3m, 0.5m);

		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "Period for ATR calculation", "Indicators")
			.SetOptimize(7, 28, 7);

		_candleInterval = Param(nameof(CandleInterval), 10)
			.SetGreaterThanZero()
			.SetDisplay("Candle Interval", "Candles between position evaluations", "General")
			.SetOptimize(5, 30, 5);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for processing", "General");
	}

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

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

		_candleCounter = 0;
		_stopPrice = 0m;
		_takeProfitPrice = 0m;
		_entryPrice = 0m;
	}

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

		var atr = new AverageTrueRange { Length = AtrPeriod };

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

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

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

		var price = candle.ClosePrice;

		// Manage existing long position
		if (Position > 0 && _stopPrice > 0m)
		{
			if (candle.LowPrice <= _stopPrice || candle.HighPrice >= _takeProfitPrice)
			{
				SellMarket(Position);
				_stopPrice = 0m;
				_takeProfitPrice = 0m;
				_entryPrice = 0m;
			}
		}

		// Manage existing short position
		if (Position < 0 && _stopPrice > 0m)
		{
			if (candle.HighPrice >= _stopPrice || candle.LowPrice <= _takeProfitPrice)
			{
				BuyMarket(Math.Abs(Position));
				_stopPrice = 0m;
				_takeProfitPrice = 0m;
				_entryPrice = 0m;
			}
		}

		_candleCounter++;

		if (_candleCounter < CandleInterval)
			return;

		_candleCounter = 0;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (Position != 0)
			return;

		if (atrValue <= 0m)
			return;

		var stopDistance = atrValue * AtrMultiplier;

		// Alternate between long and short based on price relative to previous entry
		var goLong = _entryPrice == 0m || price > _entryPrice;

		if (goLong)
		{
			BuyMarket(Volume);
			_entryPrice = price;
			_stopPrice = price - stopDistance;
			_takeProfitPrice = price + stopDistance;
		}
		else
		{
			SellMarket(Volume);
			_entryPrice = price;
			_stopPrice = price + stopDistance;
			_takeProfitPrice = price - stopDistance;
		}
	}
}