在 GitHub 上查看

Exp XPVT 策略

Exp XPVT 策略 是 MetaTrader 5 专家顾问 Exp_XPVT 的移植版本。策略比较价格-成交量趋势(Price and Volume Trend,PVT)序列与其平滑曲线之间的交叉信号:当原始 PVT 线跌破平滑线时开多,当其重新上穿时开空。可选的止损与止盈距离用于复制原始 EA 的风险控制行为。

指标原理

  • PVT 根据所选价格(收盘价、开盘价、中值等)累计“成交量加权的百分比涨跌幅”。
  • 二次滤波使用可配置的均线(SMA、EMA、SMMA、LWMA、Jurik、T3、VIDYA 近似或 Kaufman AMA)生成信号线。
  • Signal Bar 参数保留 MT5 的时序逻辑:策略读取前一根和前二根柱子的 PVT 与信号值,从而判断交叉并触发平仓。
  • PVT 可使用成交量或勾号数权重。如所选数据缺失,则自动回退到另一种来源。

交易流程

  1. 仅在每根 K 线收盘时计算指标,避免使用未完成的数值。
  2. 根据所选价格类型与成交量来源更新 PVT 值,并写入平滑指标。
  3. 若上一根柱子中 PVT 位于信号线上方,则平掉所有空头;若最新保存的 PVT 小于或等于信号线,且允许做多,则开多。
  4. 若上一根柱子中 PVT 位于信号线下方,则平掉所有多头;若最新保存的 PVT 大于或等于信号线,且允许做空,则开空。
  5. 开仓后按设置的点差自动附加止损与止盈(单位为价格最小变动)。
  6. 新仓位使用 Order Volume 指定的基础手数,同时在反向开仓时自动覆盖现有持仓,实现完整反手。

参数说明

  • Order Volume – 开仓及反手使用的基础手数。
  • Stop Loss / Take Profit – 以最小价格步长计的止损、止盈距离(0 表示不启用)。
  • Allow Buy Entry / Allow Sell Entry – 是否允许开多 / 开空。
  • Allow Buy Exit / Allow Sell Exit – 是否在出现反向信号时自动平掉现有多 / 空头仓位。
  • Candle Type – 指标计算所采用的时间框架。
  • Volume Source – 选择使用成交量或勾号数量作为 PVT 权重。
  • Smoothing Method / Length / Phase – 应用于 PVT 的平滑方式、周期及相位(仅 Jurik 类方法使用)。
  • Applied Price – 输入 PVT 的价格公式(收盘价、趋势跟随价格、DeMark 价格等)。
  • Signal Bar – 读取信号时的历史偏移,完全复刻 MT5 实现。

使用提示

  • 策略调用 StartProtection() 激活 StockSharp 的仓位保护功能,与原始 EA 保持一致。
  • Jurik 平滑通过反射设置相位参数;若指标不暴露该属性则忽略。
  • 当成交量与勾号数均不可用时,系统使用 0 作为权重以避免错误累加。
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

public class ExpXpvtStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public ExpXpvtStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}