GitHub で見る

LBS 戦略

LBS 戦略 は MetaTrader 5 エキスパートアドバイザー「LBS (barabashkakvn's edition)」の直接変換です。元のシステムは設定可能な取引ウィンドウ中に前のローソク足のブレイクアウトを監視し、両方の極値にストップ注文を配置します。StockSharp ポートは高レベル API(SubscribeCandles、SubscribeLevel1、BuyStop/SellStop)を使用して同じトレード管理ルールを維持します。

取引ロジック

  1. ストラテジーは選択した時間軸(CandleType)の完了したローソク足を監視します。
  2. ローソク足のクローズ時間が有効な取引時間(Hour1、Hour2、Hour3)のいずれかと一致すると、アルゴリズムはブレイクアウトレベルを計算します:
    • バイストップはローソク足の高値と現在のアスクにフリーズバッファを加えた値の大きい方に配置されます。
    • セルストップはローソク足の安値と現在のビッドからバッファを差し引いた値の小さい方に配置されます。
    • バッファは MetaTrader の SYMBOL_TRADE_FREEZE_LEVEL フォールバック(スプレッドの3倍、ただし10ピップ未満にはなりません)を再現します。
  3. ポジションが開かれると、反対の保留注文は即座にキャンセルされます。これは MQL エキスパートの DeleteAllPendingOrders ルーティンと同様です。
  4. 初期ストップロス価格は StopLossPips に従って添付されます。オプションのトレーリングロジック(TrailingStopPips と TrailingStepPips)は、浮動利益が設定された閾値を超えるとストップを移動させます。
  5. ストラテジーがオンライン状態で、ポジションが開いておらず、有効な Level1 クォートが利用可能な場合のみ注文が送信されます。

資金管理

MoneyMode は元のエキスパートの Lot/Risk スイッチを反映します:

  • FixedLot – VolumeOrRisk パラメーターは絶対的な取引量として解釈されます。
  • RiskPercent – ストラテジーは VolumeOrRisk をポートフォリオ価値の割合に変換します。リスク額をエントリー価格と保護ストップ間の距離(価格ステップ単位)で割って注文量を算出します。このモードを使用する場合、ストップロスを有効にする必要があります。そうでなければ注文はスキップされます。

すべてのボリュームはブローカーの拒否を避けるために、楽器の最小、最大、ステップ制約に正規化されます。

パラメーター

名前 デフォルト 説明
StopLossPips 50 ピップ単位の固定ストップまでの距離。ゼロは初期ストップとトレーリングモジュールの両方を無効にします。
TrailingStopPips 5 ピップ単位のトレーリングストップ距離。ゼロはトレーリングを無効にします。
TrailingStepPips 15 トレーリングストップが移動する前に必要な追加利益(ピップ単位)。トレーリングが有効な場合は正の値が必要です。
MoneyMode FixedLot 固定ボリュームとリスクパーセントのサイジングを選択します。
VolumeOrRisk 1.0 FixedLot モードでのロットサイズ、または RiskPercent モードでのリスク割合。
Hour1 10 最初の取引時間。無効にするには 0 に設定します。
Hour2 11 2番目の取引時間。無効にするには 0 に設定します。
Hour3 12 3番目の取引時間。無効にするには 0 に設定します。
CandleType 1時間の時間軸 ブレイクアウト検出に使用されるローソク足シリーズ。MetaTrader のチャート時間軸を反映するように調整します。

注意事項

  • 時間の比較にはローソク足のクローズ時間が使用されます。これは MetaTrader の TimeCurrent() が次のバーの始まりと等しくなる瞬間に対応します。
  • フリーズ/ストップレベルの近似により、ストップ注文が現在の bid/ask から10ピップ未満にならないことが保証され、最も一般的な MetaTrader エラーを防ぎます。
  • トレーリングストップはすべての Level1 ティックで更新されるため、元のエキスパートのティック駆動 OnTick ハンドラーに近い動作が確保されます。
  • リスクベースのサイジングは Portfolio.CurrentValue が利用可能な場合に使用し、そうでない場合は Portfolio.BeginValue にフォールバックします。

使用上のヒント

  1. ストラテジーを楽器に接続し、MetaTrader で使用したのと同じ時間軸を選択します。
  2. 取引したいセッションに応じて取引時間を設定します(0 に設定するとそのスロットが無効になります)。
  3. 自動スケーリングを希望する場合は RiskPercent モードを選択し、StopLossPips が正であることを確認してください。
  4. 固定ロット取引の場合は MoneyMode を FixedLot のままにし、VolumeOrRisk を希望するサイズに設定します。
  5. ストラテジーを開始します。次の設定された時間に2つの保留注文を配置し、保護ストップを自動的に維持します。
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

public enum LbsMoneyMode
{
	FixedLot,
	RiskPercent,
}

/// <summary>
/// Previous-candle breakout strategy with paired conditional stops and Level1 trailing.
/// </summary>
public class LbsStrategy : Strategy
{
	private readonly StrategyParam<int> _stopLossPips;
	private readonly StrategyParam<int> _trailingStopPips;
	private readonly StrategyParam<int> _trailingStepPips;
	private readonly StrategyParam<LbsMoneyMode> _moneyMode;
	private readonly StrategyParam<decimal> _volumeOrRisk;
	private readonly StrategyParam<int> _hour1;
	private readonly StrategyParam<int> _hour2;
	private readonly StrategyParam<int> _hour3;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _bid;
	private decimal? _ask;
	private Order _buyPending;
	private Order _sellPending;
	private Order _protectiveStop;
	private decimal _entryPrice;

	public int StopLossPips { get => _stopLossPips.Value; set => _stopLossPips.Value = value; }
	public int TrailingStopPips { get => _trailingStopPips.Value; set => _trailingStopPips.Value = value; }
	public int TrailingStepPips { get => _trailingStepPips.Value; set => _trailingStepPips.Value = value; }
	public LbsMoneyMode MoneyMode { get => _moneyMode.Value; set => _moneyMode.Value = value; }
	public decimal VolumeOrRisk { get => _volumeOrRisk.Value; set => _volumeOrRisk.Value = value; }
	public int Hour1 { get => _hour1.Value; set => _hour1.Value = value; }
	public int Hour2 { get => _hour2.Value; set => _hour2.Value = value; }
	public int Hour3 { get => _hour3.Value; set => _hour3.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LbsStrategy()
	{
		_stopLossPips = Param(nameof(StopLossPips), 50).SetNotNegative();
		_trailingStopPips = Param(nameof(TrailingStopPips), 5).SetNotNegative();
		_trailingStepPips = Param(nameof(TrailingStepPips), 15).SetNotNegative();
		_moneyMode = Param(nameof(MoneyMode), LbsMoneyMode.FixedLot);
		_volumeOrRisk = Param(nameof(VolumeOrRisk), 1m).SetGreaterThanZero();
		_hour1 = Param(nameof(Hour1), 10).SetRange(0, 23);
		_hour2 = Param(nameof(Hour2), 11).SetRange(0, 23);
		_hour3 = Param(nameof(Hour3), 12).SetRange(0, 23);
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame());
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType), (Security, DataType.Level1)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_bid = null;
		_ask = null;
		_buyPending = null;
		_sellPending = null;
		_protectiveStop = null;
		_entryPrice = 0m;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var quotes = new Subscription(DataType.Level1, Security);
		quotes.MarketData.BuildField = Level1Fields.BestAskPrice;
		SubscribeLevel1(quotes).Bind(ProcessLevel1).Start();
		SubscribeCandles(CandleType).Bind(ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished || Position != 0m)
			return;

		var barEnd = CandleType.Arg is TimeSpan timeFrame ? candle.OpenTime + timeFrame : candle.CloseTime;
		if (!IsTradingHour(barEnd.Hour) || _bid is not decimal bid || _ask is not decimal ask)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		CancelEntryStops();

		var point = GetPoint();
		var (buyPrice, sellPrice) = CalculateBreakoutLevels(
			candle.HighPrice, candle.LowPrice, bid, ask, point);

		var volume = CalculateVolume(point);
		if (volume <= 0m)
			return;

		_buyPending = CreateStopOrder(Sides.Buy, buyPrice, volume, "LBS buy breakout");
		_sellPending = CreateStopOrder(Sides.Sell, sellPrice, volume, "LBS sell breakout");

		RegisterOrder(_buyPending);
		RegisterOrder(_sellPending);
	}

	private void ProcessLevel1(Level1ChangeMessage message)
	{
		if (message.TryGetDecimal(Level1Fields.BestBidPrice) is decimal bid && bid > 0m)
			_bid = bid;
		if (message.TryGetDecimal(Level1Fields.BestAskPrice) is decimal ask && ask > 0m)
			_ask = ask;

		if (Position == 0m || _protectiveStop is null || TrailingStopPips <= 0 || TrailingStepPips <= 0)
			return;

		var point = GetPoint();
		var trail = TrailingStopPips * point;
		var step = TrailingStepPips * point;

		if (Position > 0m && _bid is decimal longPrice)
		{
			if (longPrice - _entryPrice < trail + step)
				return;

			var candidate = longPrice - trail;
			var current = GetActivation(_protectiveStop);
			if (candidate >= current + step)
				ReplaceProtectiveStop(Sides.Sell, candidate);
		}
		else if (Position < 0m && _ask is decimal shortPrice)
		{
			if (_entryPrice - shortPrice < trail + step)
				return;

			var candidate = shortPrice + trail;
			var current = GetActivation(_protectiveStop);
			if (candidate <= current - step)
				ReplaceProtectiveStop(Sides.Buy, candidate);
		}
	}

	protected override void OnOwnTradeReceived(MyTrade trade)
	{
		base.OnOwnTradeReceived(trade);

		if (trade?.Order is null || trade.Trade is null)
			return;

		if (IsSameOrder(trade.Order, _buyPending) || IsSameOrder(trade.Order, _sellPending))
		{
			var filledSide = trade.Order.Side;
			_entryPrice = trade.Trade.Price;

			if (filledSide == Sides.Buy)
				CancelIfActive(_sellPending);
			else
				CancelIfActive(_buyPending);

			_buyPending = null;
			_sellPending = null;

			if (StopLossPips > 0 && Position != 0m)
			{
				var point = GetPoint();
				var stop = filledSide == Sides.Buy
					? _entryPrice - StopLossPips * point
					: _entryPrice + StopLossPips * point;

				ReplaceProtectiveStop(filledSide.Invert(), stop);
			}

			return;
		}

		if (IsSameOrder(trade.Order, _protectiveStop) && Position == 0m)
		{
			_protectiveStop = null;
			_entryPrice = 0m;
		}
	}

	private bool IsTradingHour(int hour)
		=> (Hour1 != 0 && hour == Hour1) ||
		   (Hour2 != 0 && hour == Hour2) ||
		   (Hour3 != 0 && hour == Hour3);

	private decimal CalculateVolume(decimal point)
	{
		if (MoneyMode == LbsMoneyMode.FixedLot)
			return NormalizeVolume(VolumeOrRisk);

		if (StopLossPips <= 0)
			return 0m;

		var balance = Portfolio?.CurrentValue ?? Portfolio?.BeginValue ?? 0m;
		if (balance <= 0m)
			return 0m;

		var riskMoney = balance * VolumeOrRisk / 100m;
		var stepPrice = Security?.StepPrice ?? 0m;
		var lossPerUnit = stepPrice > 0m
			? StopLossPips * stepPrice
			: StopLossPips * point * (Security?.Multiplier ?? 1m);

		return lossPerUnit > 0m ? NormalizeVolume(riskMoney / lossPerUnit) : 0m;
	}

	private Order CreateStopOrder(Sides side, decimal activationPrice, decimal volume, string comment)
		=> new()
		{
			Security = Security,
			Portfolio = Portfolio,
			Type = OrderTypes.Conditional,
			Condition = new StopOrderCondition { ActivationPrice = Security.ShrinkPrice(activationPrice, ShrinkRules.Auto) },
			Side = side,
			Volume = volume,
			Comment = comment,
		};

	private void ReplaceProtectiveStop(Sides side, decimal activationPrice)
	{
		var old = _protectiveStop;
		var volume = Math.Abs(Position);
		if (volume <= 0m)
			return;

		var replacement = CreateStopOrder(side, activationPrice, volume, "LBS protective stop");

		if (old is { State: OrderStates.Active })
			ReRegisterOrder(old, replacement);
		else
			RegisterOrder(replacement);

		_protectiveStop = replacement;
	}

	private void CancelEntryStops()
	{
		CancelIfActive(_buyPending);
		CancelIfActive(_sellPending);
		_buyPending = null;
		_sellPending = null;
	}

	private void CancelIfActive(Order order)
	{
		if (order is { State: OrderStates.Active })
			CancelOrder(order);
	}

	private static bool IsSameOrder(Order left, Order right)
		=> left is not null && right is not null &&
		   (ReferenceEquals(left, right) || left.TransactionId == right.TransactionId);

	private static decimal GetActivation(Order order)
		=> order?.Condition is StopOrderCondition stop ? stop.ActivationPrice ?? 0m : 0m;

	private decimal NormalizeVolume(decimal volume)
	{
		if (Security?.MaxVolume is decimal max && max > 0m)
			volume = Math.Min(volume, max);
		if (Security?.MinVolume is decimal min && min > 0m)
			volume = Math.Max(volume, min);
		if (Security?.VolumeStep is decimal step && step > 0m)
			volume = Math.Floor(volume / step) * step;

		return volume;
	}

	private decimal GetPoint()
	{
		var point = Security?.PriceStep ?? 0m;
		return point > 0m ? point : 0.0001m;
	}

	internal static (decimal buy, decimal sell) CalculateBreakoutLevels(
		decimal candleHigh,
		decimal candleLow,
		decimal bid,
		decimal ask,
		decimal priceStep)
	{
		var spread = Math.Max(0m, ask - bid);
		var buffer = Math.Max(3m * spread, 10m * priceStep);
		return (Math.Max(candleHigh, ask + buffer), Math.Min(candleLow, bid - buffer));
	}
}