GitHub で見る

シンプルなトレーリングストップ戦略

この例はStockSharpの高レベルAPIを使用して、トレーリングストップで未決ポジションを管理する方法を示しています。

概要

  • 最初の完了したローソク足を受信した後に単一のロングポジションを開設します。
  • トレーリングストップによるポジション保護を有効化します。
  • ストップ価格は現在の価格から一定の距離を保って追随します。

パラメーター

  • TrailPoints – ストップをトレールするために使用する価格ポイントの距離。
  • CandleType – 戦略が処理するローソク足の種類。

ロジック

  1. 開始時に戦略はローソク足を購読し、トレーリング付きでStartProtectionを有効化します。
  2. 最初の完了したローソク足の後、戦略は成行で買います。
  3. ポジションに有利な方向に価格が動くと、TrailPointsで定義された距離を保つようにストップレベルが移動します。
  4. 価格が反転してトレーリングストップに触れると、ポジションは自動的に決済されます。

この戦略は簡略化されており、トレーリングストップの基本的な使用方法を示すことを目的としています。

using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Demonstrates exactly one market long entry after the first finished candle,
/// followed by the engine's fill-price-based native trailing stop.
/// </summary>
public class SimpleTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<decimal> _trailPoints;
	private readonly StrategyParam<DataType> _candleType;
	private bool _submittedEntry;
	private Order _entryOrder;

	public decimal TrailPoints { get => _trailPoints.Value; set => _trailPoints.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SimpleTrailingStopStrategy()
	{
		_trailPoints = Param(nameof(TrailPoints), 100m).SetGreaterThanZero()
			.SetDisplay("Trail Points", "Trailing distance in security price steps", "Protection");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_submittedEntry = false;
		_entryOrder = null;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_submittedEntry = false;
		_entryOrder = null;

		StartProtection(
			takeProfit: null,
			stopLoss: new Unit(TrailPoints * (Security.PriceStep ?? 1m), UnitTypes.Absolute),
			isStopTrailing: true,
			useMarketOrders: true);

		SubscribeCandles(CandleType).Bind(ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished || _submittedEntry ||
			!IsFormedAndOnlineAndAllowTrading())
			return;

		// Mark before sending: avoid duplicate opens on synchronous fills or callbacks.
		_submittedEntry = true;
		_entryOrder = BuyMarket(Volume);
		// Do not reopen after a protective stop fills: this is a one-shot example.
	}
}