GitHub で見る

Ichimoku Momentum MACD 戦略

サマリー

  • タイプ: Momentumの確認を伴うトレンドフォロー。
  • 時間軸: 設定可能(デフォルト15分足)。
  • インジケーター: Ichimoku(Tenkan/Kijun)、線形加重移動平均、Momentum、MACD。
  • ストップ: StartProtection経由で価格ポイントでのオプションの固定テイクプロフィットとストップロス。

戦略の説明

この戦略はMetaTraderのエキスパート「Ichimoku」(フォルダMQL/23469)の意思決定フローを再現します。前の閉じたローソク足を評価し、4つの確認すべてが一致したときに次のバーの開始時に新しいトレードを開きます:

  1. Ichimokuのアライメント – ロングトレードでは、Tenkan(転換線)がKijun(基準線)より上にある必要があり、ショートでは下にある必要があります。
  2. LWMAトレンドフィルター – ロングでは速い線形加重移動平均が遅いLWMAより上に、ショートでは下に留まる必要があります。両方の平均は購読されたローソク足と同じタイムフレームで計算されます。
  3. Momentumの強度 – Momentumオシレーターの中立レベル100からの絶対距離は、最後の3本の閉じたローソク足のうち少なくとも1本で設定可能な閾値より大きくなければなりません。
  4. MACDの確認 – MACDのヒストグラムは方向と一致する必要があります(MACDラインが同じ符号でシグナルラインを超えた位置にある)。

4つの条件すべてが強気に揃い、戦略が現在ロングでない場合、設定されたボリュームに加えて既存のショートポジションをフラットにするために必要なユニットを購入します。条件が弱気に転換すると、売り側でプロセスを反映します。反対のシグナルは常にオープンポジションを閉じ、保護注文なしでも決定論的な出口を提供します。

リスク管理はStockSharpのStartProtectionを通じて処理され、楽器ポイントで表された固定のテイクプロフィットとストップロス距離を使用できます。パラメーターをゼロに設定すると対応する保護レッグが無効になります。

パラメーター概要

パラメーター 説明
FastMaPeriod トレンドフィルターに使用する速い線形加重移動平均の長さ。
SlowMaPeriod 遅い線形加重移動平均の長さ。
MomentumPeriod Momentumオシレーターのルックバック期間。
MomentumThreshold 最後の3本のローソク足のうち少なくとも1本でMomentumが達する必要がある100からの最小距離。
MacdFastPeriod MACDフィルターの速いEMA長。
MacdSlowPeriod MACDフィルターの遅いEMA長。
MacdSignalPeriod MACDフィルターのシグナルEMA長。
TenkanPeriod IchimokuのTenkan-senの長さ。
KijunPeriod IchimokuのKijun-senの長さ。
SenkouSpanBPeriod IchimokuのSenkou Span Bの長さ。
TakeProfitPoints 価格ポイントでのオプションのテイクプロフィット距離(0で無効)。
StopLossPoints 価格ポイントでのオプションのストップロス距離(0で無効)。
CandleType すべてのインジケーター計算に使用するタイムフレーム。

使用上の注記

  • 戦略は完了したローソク足のみを読み取り、前のバーのインジケーター値を保存し、MetaTrader EAのshift=1ロジックに対応します。
  • 異なるMomentumスケーリングを持つ市場(例:仮想通貨 vs. Forexペア)に切り替える際はMomentumThresholdを調整してください。
  • 保護注文は内部的に管理されます;取引所レベルのブラケット注文は送信されません。
  • チャートが利用可能な場合、価格ローソク足、両方のLWMA、Ichimokuクラウド、および実行されたトレードが表示されます。
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 IchimokuMomentumMacdStrategy : 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 IchimokuMomentumMacdStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12).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;
	}
}