GitHub で見る

TPSL Insert戦略

この戦略は MetaTrader 4 スクリプト TPSL-Insert.mq4 の StockSharp 翻訳です。エントリーまたはエグジットシグナルを生成しません。その唯一の目的は、既存のポジションにテイクプロフィットとストップロス注文を付加することです。

機能の仕組み

  1. 開始時に、戦略は TakeProfitPipsStopLossPips パラメーターを読み取ります。
  2. 値は証券の PriceStep を使用してピップから価格に変換されます。
  3. 保護注文を設定するために StartProtection が呼び出されます。
    • ポジションが既に存在する場合、保護注文はすぐに送信されます。
    • 戦略によって開かれる将来のポジションは自動的に保護されます。

この動作は、ポジションが手動または外部システムによって開かれ、リスク制限を素早く挿入する必要がある場合に役立ちます。

パラメーター

名前 説明 デフォルト
TakeProfitPips ピップ単位のテイクプロフィット距離。 35
StopLossPips ピップ単位のストップロス距離。 100

注意事項

  • 戦略は市場データを購読せず、取引ロジックを含みません。
  • StartProtection は保護注文の作成とキャンセルを処理します。
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;

/// <summary>
/// EMA crossover strategy with take-profit and stop-loss protection.
/// </summary>
public class TpslInsertStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevDiff;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public TpslInsertStrategy()
	{
		_fastLength = Param(nameof(FastLength), 8)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "General");

		_slowLength = Param(nameof(SlowLength), 21)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle Type", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevDiff = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var emaFast = new ExponentialMovingAverage { Length = FastLength };
		var emaSlow = new ExponentialMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(emaFast, emaSlow, ProcessCandle).Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, emaFast);
			DrawIndicator(area, emaSlow);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var diff = fast - slow;
		var crossUp = _prevDiff <= 0 && diff > 0;
		var crossDown = _prevDiff >= 0 && diff < 0;
		_prevDiff = diff;

		if (crossUp && Position <= 0)
			BuyMarket();
		else if (crossDown && Position >= 0)
			SellMarket();
	}
}