GitHub で見る

Exp Cronex Chaikin戦略

この戦略は、MetaTraderエキスパートアドバイザーExp_CronexChaikin.mq5をStockSharpの高レベルAPIにポートします。元のロボットは累積/分散値からChaikinオシレーターを再構築し、Cronex「XMA」フィルターで2回平滑化し、高速線と低速線のクロスオーバーで取引します。StockSharpバージョンは同じロジックを再現しながら、各段階を設定可能なパラメーターとして公開しています。

トレーディングロジック

  1. 設定されたローソク足シリーズ(CandleType)をサブスクライブします。
  2. 選択されたVolumeSource(ティックまたは実際のボリューム)を使用して、完了した各ローソク足の累積/分散(AD)ラインを再計算します。
  3. AD ラインを2つの移動平均(ChaikinFastPeriodChaikinSlowPeriodChaikinMethod)で平滑化し、その差を取ることでChaikinオシレーターを適用します。
  4. SmoothingMethodFastPeriodSlowPeriodPhaseで制御されるCronexフィルターを使用して、結果のオシレーターを2回平滑化します。これら2つの平滑化された値は、元のインジケーターの「高速」線と「シグナル」線に対応します。
  5. SignalBar個の完了したローソク足を遡り、その棒と前の棒の両方のCronexラインを比較します。
  6. 高速線が低速線より上にある場合、戦略はオプションでショートポジションを決済し、BuyOpenEnabledがtrueの場合、ルックバックバーで新鮮な上昇クロスが検出されたらロングポジションを開きます。
  7. 高速線が低速線より下にある場合、SellOpenEnabledBuyCloseEnabledで制御されるショート取引に対して逆のアクションが実行されます。
  8. 新しいポジションが開かれるたびに、ストップロスとテイクプロフィット注文(ポイントで表現)がStopLossTakeProfitで再計算されます。

単一のネットポジションのみが維持されます。シグナルの方向が変わると、戦略は現在のポジションを決済するために必要なボリュームと新しい取引サイズを組み合わせてMetaTraderのネッティング動作を模倣します。

インジケーターと平滑化オプション

  • Chaikinオシレーター: 選択されたChaikinMethod移動平均タイプを累積/分散ラインに適用して構築されます。利用可能なオプションには、単純、指数、平滑化、線形加重平均が含まれます。
  • Cronexスムーザー: SmoothingMethodパラメーターはCronex XMAファミリー(SMA、EMA、SMMA、LWMA、Jurik JJMA/JurX、Parabolic MA、T3、VIDYA、AMA)を公開します。PhaseパラメーターはMQL実装と全く同じようにJurikベースのフィルターに影響します。

パラメーター

パラメーター 説明
CandleType インジケーターの計算に使用するローソク足のデータタイプ。デフォルトは4時間時間軸です。
ChaikinMethod Chaikinオシレーター内で使用される移動平均手法。
ChaikinFastPeriod / ChaikinSlowPeriod 累積/分散ラインに適用される高速・低速期間。
SmoothingMethod Chaikinオシレーター値に適用されるCronex平滑化アルゴリズム。
FastPeriod / SlowPeriod 高速・低速Cronexラインの長さ。
Phase Jurikベースのスムーザーの位相パラメーター(範囲-100〜+100)。
VolumeSource 累積/分散ラインの計算時にティックまたは実際のボリュームを選択。
SignalBar クロスオーバーシグナルを含む必要がある完了バーの遡り数。
BuyOpenEnabled / SellOpenEnabled ロングまたはショート取引の開始を有効または無効にします。
BuyCloseEnabled / SellCloseEnabled 逆シグナル時に反対ポジションの決済を許可します。
TakeProfit / StopLoss 各エントリー後に適用される計器ポイントでの利益目標と保護ストップ距離。
Volume 標準StockSharpポジションサイズ(元のエキスパートのロットサイズとして機能)。

MQLバージョンとの違い

  • TradeAlgorithms.mqhからのマネー管理とスリッページルーティンは、組み込みのVolumeSetStopLossSetTakeProfitヘルパーに置き換えられます。
  • StockSharp実装は完了したローソク足でのみADラインを再計算し、テストとライブ取引の決定論的動作を確保します。
  • Cronex平滑化オプションはStockSharpインジケーターに依存します:JurikフィルターはJurikMovingAverage(フェーズ制御付き)でサポートされ、VIDYAとParMAは他のCronex変換と一致した指数近似を使用します。
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 ExpCronexChaikinStrategy : 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 ExpCronexChaikinStrategy()
	{
		_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;
	}
}