GitHub で見る

Exp XPVT戦略

Exp XPVT戦略は、MetaTrader 5エキスパートアドバイザーExp_XPVTの変換版です。このシステムはPrice and Volume Trend(PVT)インジケーターとPVTシリーズに適用した設定可能な移動平均線のクロスオーバーをトレードします。生のPVTラインが平滑化バリアントを下回ると買いポジションを建て、上抜けクロスが売りエントリーを引き起こします。オプションのストップロス・テイクプロフィット距離は元のEAの動作を再現します。

インジケーターロジック

  • Price and Volume Trendは選択した適用価格(終値、始値、中値等)を使用して出来高加重の価格変化率を累積します。
  • 平滑化フィルター(SMA、EMA、平滑MA、LWMA、Jurik、T3、VIDYA近似、またはKaufman AMA)がシグナルラインを生成します。
  • 歴史的オフセット(Signal Bar)がMT5ロジックを再現:戦略は1本前と2本前のバーの平滑値と生値を比較してクロスオーバーや決済条件を検出します。
  • ティックまたはリアルボリュームを加重に使用できます。要求したボリュームタイプが利用できない場合、戦略は自動的にもう一方のソースにフォールバックします。

トレードルール

  1. 各確定ローソク足で、設定した適用価格とボリュームタイプからPVT値を計算します。
  2. 平滑化インジケーターを更新し、Signal Barに従って最新の値を保存します。
  3. 前のバーでPVTがシグナルラインを上回っていた場合、ショートポジションをクローズ。さらに最新保存PVTがシグナルライン以下であれば、買いポジションを建てます(買いエントリーが有効な場合)。
  4. 前のバーでPVTがシグナルラインを下回っていた場合、ロングポジションをクローズ。さらに最新保存PVTがシグナルライン以上であれば、売りポジションを建てます(売りエントリーが有効な場合)。
  5. トレードエントリー後、設定した距離(価格ステップ)でオプションのストップロス・テイクプロフィット注文が添付されます。
  6. マネー管理は元のEAを模倣:新規注文は設定したベースOrder Volumeを使用し、方向転換時に完全にリバースするために反対の露出を含みます。

パラメーター

  • Order Volume – 新規注文とリバーサルのベースボリューム。
  • Stop Loss – 保護ストップの価格ステップ距離(0で無効化)。
  • 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実装を再現。

注意事項

  • 戦略はインジケーターの安定性を確保するため、確定ローソク足のみ処理します。
  • Jurik系平滑化はインジケーターがフェーズパラメーターを公開している場合、リフレクションを使用して転送します。
  • ティックボリュームもリアルボリュームも利用できない場合、戦略はゼロボリュームにフォールバックし、誤った累積を防ぎます。
  • オプションのStartProtection呼び出しはStockSharpの組み込みポジションモニタリングを有効化し、元のEAの単一呼び出しと対応します。
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;
	}
}