在 GitHub 上查看

Invest System 4.5 策略 (C#)

概述

Invest System 4.5 原本是一个 MetaTrader 5 智能交易系统,此处将其迁移到 StockSharp 高层策略 API。策略在 EUR/USD 上运行,根据上一根已经收盘的 4 小时蜡烛的方向进行交易。每个新的 4 小时时段只允许开一笔单,仓位规模会根据实际盈亏以及账户权益的增长自动调整。

实现完全依赖高层 API:策略自动订阅 4 小时蜡烛用于判断趋势,同时订阅分钟级别蜡烛用于监控进场窗口;StartProtection 方法用来一次性设置以点数表示的止损和止盈。

交易流程

  1. 方向判断:每当 4 小时蜡烛收盘时记录其涨跌方向。收阳意味着下一根 4 小时期间只寻找多头入场,收阴则只考虑空头。如果开收价相同则沿用上一根蜡烛的方向。
  2. 进场窗口:新 4 小时蜡烛开启后,打开一个固定时长(默认 15 分钟)的进场窗口。在窗口有效期内,策略观察更低周期(默认 1 分钟)的收盘数据,只要条件满足就会在窗口内发送一张市价单,随后立即关闭窗口。
  3. 单次持仓:策略不加仓、不做网格,任何时候最多只持有一笔仓位。如果仍有持仓,将忽略新的信号直到下一根 4 小时蜡烛开始。
  4. 盈亏记录:当仓位完全平仓后,记录本次交易的实际盈亏,用于触发下面的分级加仓规则。

仓位管理

策略复刻了原 EA 的两层资金管理机制:

  • 权益阶梯:首次运行时保存初始余额。当账户权益达到初始余额的 2 倍、3 倍直至 6 倍时,基础手数按比例提升。一级使用 BaseLot,二级翻倍,三级为三倍,以此类推。额外手数 (Lot2Lot3Lot4) 根据原始比例(×2、×7、×14)自动计算。
  • Plan B 模式:在交易之间维护一个当前手数。
    • 基础手数亏损后,将手数提高到 Lot3
    • 如果在 Lot3 上继续亏损,则启动 Plan B:重新映射手数,使基础手数变为 Lot2,激进手数变为 Lot4。当前手数不会立即改变,但下次亏损会使用新的激进手数。达到新的权益高点时 Plan B 自动退出。
    • 盈利交易会把手数恢复到当前阶梯的基础手数。 借助上述规则,可以在 StockSharp 中完整复现 MT5 中的阶梯式加仓行为。

风险控制

  • 使用 StartProtection 以点值设置固定止损与止盈。这两个保护订单在策略启动时一次性配置,效果等同于原 EA 在每笔订单上附加相同的价差。
  • 仅使用市价单。策略不执行分批减仓或对冲,平仓动作由保护单自动完成。

参数

参数 说明 默认值 优化范围
StopLossPips 止损点数,设为 0 可关闭止损。 240 120 – 360,步长 20
TakeProfitPips 止盈点数,设为 0 可关闭止盈。 40 20 – 80,步长 10
EntryWindowMinutes 新的 4 小时蜡烛开始后允许进场的分钟数。 15 5 – 30,步长 5
SignalCandleType 监控进场窗口所用的低周期蜡烛类型(默认 1 分钟)。 1 分钟蜡烛
TrendCandleType 判断方向所用的高周期蜡烛类型(默认 4 小时)。 4 小时蜡烛
BaseLot 初始基础手数,其它手数会按照既定比例推导。 0.1 0.05 – 0.3,步长 0.05

目录结构

2772_Invest_System_45/
├── CS/
│   └── InvestSystem45Strategy.cs
├── README.md
├── README_ru.md
└── README_zh.md

使用说明

  • 需要确保所选证券同时提供 4 小时和分钟级蜡烛数据,OnStarted 会自动创建对应订阅。
  • 点值通过 Security.PriceStep 计算,并在遇到三位或五位小数报价时乘以 10,以匹配 MetaTrader 的点数定义。
  • 阶梯逻辑依赖 Portfolio.CurrentValue 的实时更新。在仿真或回测时,请确认投资组合模型会及时刷新该数值,否则手数调整会失真。
  • 根据需求,本版本仅提供 C# 实现,不包含 Python 代码。
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>
/// Invest System 4.5 strategy converted from MetaTrader.
/// Trades in the direction of the previous 4-hour candle within the first minutes of the new session.
/// </summary>
public class InvestSystem45Strategy : Strategy
{
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _takeProfitPips;
	private readonly StrategyParam<int> _entryWindowMinutes;
	private readonly StrategyParam<DataType> _signalCandleType;
	private readonly StrategyParam<DataType> _trendCandleType;
	private readonly StrategyParam<decimal> _baseLot;

	private decimal _pipSize;
	private decimal _minBalance;
	private decimal _maxBalance;
	private int _lotStage;
	private bool _planBActive;

	private decimal _stageLot1;
	private decimal _stageLot2;
	private decimal _stageLot3;
	private decimal _stageLot4;
	private decimal _lotOption1;
	private decimal _lotOption2;
	private decimal _currentVolume;

	private bool _needsPostTradeAdjustment;
	private bool _hasOpenPosition;
	private decimal _pnlAtEntry;
	private decimal _lastTradePnL;

	private int _trendDirection;
	private DateTime? _entryWindowStart;
	private DateTime? _entryWindowEnd;
	private bool _entryWindowActive;

	private decimal _entryPrice;
	private decimal _stopPrice;
	private decimal _takePrice;

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

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

	/// <summary>
	/// Minutes allowed for entries after a new trend candle opens.
	/// </summary>
	public int EntryWindowMinutes
	{
		get => _entryWindowMinutes.Value;
		set => _entryWindowMinutes.Value = value;
	}

	/// <summary>
	/// Candle type that drives entry timing.
	/// </summary>
	public DataType SignalCandleType
	{
		get => _signalCandleType.Value;
		set => _signalCandleType.Value = value;
	}

	/// <summary>
	/// Higher timeframe candle used to define trade direction.
	/// </summary>
	public DataType TrendCandleType
	{
		get => _trendCandleType.Value;
		set => _trendCandleType.Value = value;
	}

	/// <summary>
	/// Base lot size used to derive martingale steps.
	/// </summary>
	public decimal BaseLot
	{
		get => _baseLot.Value;
		set => _baseLot.Value = value;
	}

	/// <summary>
	/// Initialize <see cref="InvestSystem45Strategy"/>.
	/// </summary>
	public InvestSystem45Strategy()
	{
		_stopLossPips = Param(nameof(StopLossPips), 240)
			.SetNotNegative()
			.SetDisplay("Stop Loss (pips)", "Stop loss distance in pips", "Risk")
			
			.SetOptimize(120, 360, 20);

		_takeProfitPips = Param(nameof(TakeProfitPips), 40)
			.SetNotNegative()
			.SetDisplay("Take Profit (pips)", "Take profit distance in pips", "Risk")
			
			.SetOptimize(20, 80, 10);

		_entryWindowMinutes = Param(nameof(EntryWindowMinutes), 15)
			.SetGreaterThanZero()
			.SetDisplay("Entry Window", "Minutes after 4H open when entries are allowed", "Timing")
			
			.SetOptimize(5, 30, 5);

		_signalCandleType = Param(nameof(SignalCandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Signal Candles", "Candles used to time entries", "Timing");

		_trendCandleType = Param(nameof(TrendCandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Trend Candles", "Higher timeframe candles for direction", "Timing");

		_baseLot = Param(nameof(BaseLot), 0.1m)
			.SetGreaterThanZero()
			.SetDisplay("Base Lot", "Starting lot size before scaling", "Risk")
			
			.SetOptimize(0.05m, 0.3m, 0.05m);
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		if (Security is null)
			yield break;

		yield return (Security, SignalCandleType);

		if (!SignalCandleType.Equals(TrendCandleType))
			yield return (Security, TrendCandleType);
	}

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

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

		ResetState();
		_pipSize = CalculatePipSize();
		// Recreate lot options according to current stage and plan mode.
		RecalculateLotOptions();

		var trendSubscription = SubscribeCandles(TrendCandleType);
		trendSubscription.Bind(ProcessTrendCandle).Start();

		var entrySubscription = SubscribeCandles(SignalCandleType);
		entrySubscription.Bind(ProcessEntryCandle).Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, entrySubscription);
			DrawOwnTrades(area);
		}
	}

	/// <inheritdoc />
	protected override void OnPositionReceived(Position position)
	{
		base.OnPositionReceived(position);

		if (Position != 0m)
		{
			// Record entry state to compute realized PnL later.
			if (!_hasOpenPosition)
			{
				_hasOpenPosition = true;
				_needsPostTradeAdjustment = true;
				_pnlAtEntry = PnL;
			}

			_entryWindowActive = false;
			return;
		}

		if (!_hasOpenPosition)
			return;

		_hasOpenPosition = false;
		_lastTradePnL = PnL - _pnlAtEntry;
		// Mirror MetaTrader profit calculation for Plan B rules.

		HandlePostTradeAdjustment();
	}

	private void ProcessTrendCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished)
			return;

		// Store direction from the last completed 4H candle.
		if (candle.ClosePrice > candle.OpenPrice)
		{
			_trendDirection = 1;
		}
		else if (candle.ClosePrice < candle.OpenPrice)
		{
			_trendDirection = -1;
		}

		_entryWindowStart = candle.CloseTime;
		_entryWindowEnd = _entryWindowStart?.AddMinutes(EntryWindowMinutes);
		// Open a new entry window immediately at the next candle open.
		_entryWindowActive = true;
	}

	private void ProcessEntryCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished)
			return;

		// Check SL/TP for open positions.
		if (Position > 0m && _entryPrice > 0m)
		{
			if (_stopPrice > 0m && candle.LowPrice <= _stopPrice)
			{
				SellMarket(Position);
				ResetTargets();
				return;
			}
			if (_takePrice > 0m && candle.HighPrice >= _takePrice)
			{
				SellMarket(Position);
				ResetTargets();
				return;
			}
		}
		else if (Position < 0m && _entryPrice > 0m)
		{
			if (_stopPrice > 0m && candle.HighPrice >= _stopPrice)
			{
				BuyMarket(Math.Abs(Position));
				ResetTargets();
				return;
			}
			if (_takePrice > 0m && candle.LowPrice <= _takePrice)
			{
				BuyMarket(Math.Abs(Position));
				ResetTargets();
				return;
			}
		}

		// Update balance-dependent scaling before evaluating signals.
		UpdateBalanceState();

		if (!_entryWindowActive || !_entryWindowStart.HasValue || !_entryWindowEnd.HasValue)
			return;

		var openTime = candle.OpenTime;
		if (openTime < _entryWindowStart.Value)
			return;

		if (openTime > _entryWindowEnd.Value)
		{
			_entryWindowActive = false;
			return;
		}

		if (_trendDirection == 0)
			return;

		if (Position != 0m)
			return;

		// Lazy initialize volume when strategy is ready.
		if (_currentVolume <= 0m)
			_currentVolume = _lotOption1;

		if (_currentVolume <= 0m)
			return;

		if (_trendDirection > 0)
		{
			BuyMarket(_currentVolume);
		}
		else
		{
			SellMarket(_currentVolume);
		}

		// Allow only one trade per 4H candle similar to MetaTrader logic.
		_entryWindowActive = false;
	}

	private void HandlePostTradeAdjustment()
	{
		if (!_needsPostTradeAdjustment)
			return;

		_needsPostTradeAdjustment = false;

		// Apply lot escalation rules after each closed trade.
		UpdateBalanceState();

		if (_lastTradePnL < 0m)
		{
			if (_currentVolume == _lotOption2 && !_planBActive)
			{
				_planBActive = true;
				RecalculateLotOptions();
			}
			else if (_currentVolume == _lotOption1)
			{
				_currentVolume = _lotOption2;
			}
			else
			{
				_currentVolume = _lotOption2;
			}
		}
		else if (_lastTradePnL > 0m)
		{
			_currentVolume = _lotOption1;
		}
	}

	private void UpdateBalanceState()
	{
		var balance = Portfolio?.CurrentValue;
		if (balance is null || balance.Value <= 0m)
			return;

		if (_minBalance <= 0m)
		{
			_minBalance = balance.Value;
			_maxBalance = balance.Value;
		}

		if (balance.Value > _maxBalance)
		{
			_maxBalance = balance.Value;
			if (_planBActive)
			{
				_planBActive = false;
				RecalculateLotOptions();
			}
		}

		var newStage = 1;
		if (_minBalance > 0m)
		{
			// Check for equity milestones to scale base lots.
			for (var stage = 6; stage >= 2; stage--)
			{
				if (balance.Value > _minBalance * stage)
				{
					newStage = stage;
					break;
				}
			}
		}

		if (newStage != _lotStage)
		{
			_lotStage = newStage;
			RecalculateLotOptions();
		}
	}

	private decimal CalculatePipSize()
	{
		var step = Security?.PriceStep ?? 1m;
		var decimals = Security?.Decimals ?? 0;

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

		return step;
	}

	/// <inheritdoc />
	protected override void OnOwnTradeReceived(MyTrade trade)
	{
		base.OnOwnTradeReceived(trade);
		if (trade?.Trade == null) return;

		if (Position != 0m && _entryPrice == 0m)
		{
			_entryPrice = trade.Trade.Price;
			var slDist = StopLossPips * _pipSize;
			var tpDist = TakeProfitPips * _pipSize;

			if (Position > 0m)
			{
				_stopPrice = slDist > 0m ? _entryPrice - slDist : 0m;
				_takePrice = tpDist > 0m ? _entryPrice + tpDist : 0m;
			}
			else
			{
				_stopPrice = slDist > 0m ? _entryPrice + slDist : 0m;
				_takePrice = tpDist > 0m ? _entryPrice - tpDist : 0m;
			}
		}

		if (Position == 0m)
			ResetTargets();
	}

	private void ResetTargets()
	{
		_entryPrice = 0m;
		_stopPrice = 0m;
		_takePrice = 0m;
	}

	private void ResetState()
	{
		_pipSize = 0m;
		_minBalance = 0m;
		_maxBalance = 0m;
		_lotStage = 1;
		_planBActive = false;
		_stageLot1 = 0m;
		_stageLot2 = 0m;
		_stageLot3 = 0m;
		_stageLot4 = 0m;
		_lotOption1 = 0m;
		_lotOption2 = 0m;
		_currentVolume = 0m;
		_needsPostTradeAdjustment = false;
		_hasOpenPosition = false;
		_pnlAtEntry = 0m;
		_lastTradePnL = 0m;
		_trendDirection = 0;
		_entryWindowStart = null;
		_entryWindowEnd = null;
		_entryWindowActive = false;
		_entryPrice = 0m;
		_stopPrice = 0m;
		_takePrice = 0m;
	}

	private void RecalculateLotOptions()
	{
		var baseLot = BaseLot * _lotStage;

		_stageLot1 = baseLot;
		_stageLot2 = baseLot * 2m;
		_stageLot3 = baseLot * 7m;
		_stageLot4 = baseLot * 14m;

		// Stage-specific lot multipliers replicate the original configuration.
		if (_planBActive)
		{
			_lotOption1 = _stageLot2;
			_lotOption2 = _stageLot4;
		}
		else
		{
			_lotOption1 = _stageLot1;
			_lotOption2 = _stageLot3;
		}

		if (_currentVolume <= 0m)
			_currentVolume = _lotOption1;
	}
}