GitHub で見る

Broadening Top戦略

概要

Broadening Top戦略は、MetaTraderの元のエキスパートアドバイザー「Broadening top」に着想を得たトレンドフォロー型システムです。トレンド方向とモメンタム確認を組み合わせ、拡大型フォーメーション後に現れるブレイクアウトを捉えることに焦点を当てます。2本の線形加重移動平均、モメンタムオシレーター、MACDフィルターが連携して、強気および弱気のブレイクアウトを検出します。

取引ロジック

  1. トレンドフィルター: 戦略は高速と低速の線形加重移動平均(LWMA)を比較します。ロング取引では高速LWMAが低速LWMAを上回る必要があり、ショート取引ではその逆を想定します。
  2. モメンタム確認: モメンタムオシレーターを直近3本の確定ローソク足で監視します。これらの値のいずれかが中立レベル(100)から設定しきい値以上離れている場合のみ取引を許可します(ロングとショートで別値)。
  3. MACD整合: 追加フィルターがMACDラインとシグナルラインを確認します。ロングポジションはMACDラインがシグナルラインを上回る場合のみ、ショートは下回る場合のみ発動します。
  4. ポジション処理: 反対方向の取引を開く前に現在のポジションを閉じ、同時に有効なポジションが1つだけになるようにします。

リスク管理

戦略はStartProtectionで保護注文を管理します。

  • 価格ステップ(pips)で定義された任意のストップロスおよびテイクプロフィット距離。
  • 設定可能なトレーリングステップを持つ任意のトレーリングストップ。

パラメーター

パラメーター 説明 既定値
OrderVolume ロット/契約での注文サイズ。 1
FastMaLength 高速線形加重移動平均の長さ。 6
SlowMaLength 低速線形加重移動平均の長さ。 85
MomentumPeriod モメンタムオシレーターのルックバック期間。 14
MomentumBuyThreshold ロングエントリー許可に必要な中立モメンタムレベル(100)からの最小距離。 0.3
MomentumSellThreshold ショートエントリー許可に必要な中立モメンタムレベル(100)からの最小距離。 0.3
MacdFast MACD内の高速EMA長。 12
MacdSlow MACD内の低速EMA長。 26
MacdSignal MACD内のシグナルEMA。 9
TakeProfitPips 価格ステップで測るテイクプロフィット距離。 50
StopLossPips 価格ステップで測るストップロス距離。 20
TrailingStopPips 価格ステップで測るトレーリングストップ距離。 40
TrailingStepPips トレーリングストップ更新前に必要な追加距離。 10
CandleType 計算に使うローソク足タイプ/時間軸。 15分足
EnableLongs ロング取引を有効または無効にします。 true
EnableShorts ショート取引を有効または無効にします。 true

指標

  • LinearWeightedMovingAverage: 高速および低速トレンドフィルター。
  • Momentum: 中立レベルから離れる市場加速を確認します。
  • MovingAverageConvergenceDivergenceSignal: MACDラインとシグナルラインで方向確認を提供します。

使用上の注意

  • 元のMQL動作を再現するため、モメンタムしきい値は直近3本の確定ローソク足で評価されます。
  • 保護注文(ストップロス、テイクプロフィット、トレーリングストップ)は任意で、対応する距離をゼロに設定すると無効化できます。
  • pipサイズを正しく計算するには、価格ステップと小数情報を提供する銘柄へ戦略を接続する必要があります。
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 BroadeningTopStrategy : 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 BroadeningTopStrategy()
	{
		_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;
	}
}