GitHub で見る

ATR Trailing Stops 戦略

ATR Trailingはaverage true rangeの倍数を使用して、オープンポジションの後ろにストップをトレーリングします。価格が移動平均をクロスするとエントリーが発生し、トレーリングストップはボラティリティに応じて調整されます。

テストでは年間平均リターン約157%が示されています。暗号資産市場で最も優れたパフォーマンスを発揮します。

価格が前進するにつれて、ストップは最新のATR読み値に基づいて上(または下)にラチェットし、決して後退しません。これにより、トレンドが続く限り利益が確定されます。

トレーリングストップが発動するか、価格が移動平均を逆にクロスするとエグジットが発生します。

詳細

  • エントリー条件: 価格がMAの上または下。
  • ロング/ショート: 両方向。
  • エグジット条件: トレーリングストップ発動または価格がMAをクロス。
  • ストップ: はい。
  • デフォルト値:
    • AtrPeriod = 14
    • AtrMultiplier = 3.0m
    • MAPeriod = 20
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: トレンド
    • 方向: 両方
    • インジケーター: ATR, MA
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Strategy that uses ATR for trailing stop management.
/// It enters positions using a simple moving average and manages exits with a dynamic
/// trailing stop calculated as a multiple of ATR.
/// </summary>
public class AtrTrailingStrategy : Strategy
{
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _atrMultiplier;
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private decimal _entryPrice;
	private decimal _trailingStopLevel;
	private int _cooldown;

	/// <summary>
	/// Period for ATR calculation.
	/// </summary>
	public int AtrPeriod
	{
		get => _atrPeriod.Value;
		set => _atrPeriod.Value = value;
	}

	/// <summary>
	/// ATR multiplier for trailing stop calculation.
	/// </summary>
	public decimal AtrMultiplier
	{
		get => _atrMultiplier.Value;
		set => _atrMultiplier.Value = value;
	}

	/// <summary>
	/// Period for Moving Average calculation for entry.
	/// </summary>
	public int MAPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	/// <summary>
	/// Type of candles used for strategy calculation.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Cooldown bars between trades.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Initialize the ATR Trailing strategy.
	/// </summary>
	public AtrTrailingStrategy()
	{
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetDisplay("ATR Period", "Period for ATR calculation", "Indicators")
			.SetOptimize(7, 21, 7);

		_atrMultiplier = Param(nameof(AtrMultiplier), 3.0m)
			.SetDisplay("ATR Multiplier", "ATR multiplier for trailing stop", "Risk")
			.SetOptimize(2.0m, 4.0m, 0.5m);

		_maPeriod = Param(nameof(MAPeriod), 20)
			.SetDisplay("MA Period", "Period for Moving Average calculation for entry", "Indicators")
			.SetOptimize(10, 50, 5);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_cooldownBars = Param(nameof(CooldownBars), 500)
			.SetRange(1, 1000)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = default;
		_trailingStopLevel = default;
		_cooldown = default;
	}

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

		_entryPrice = 0;
		_trailingStopLevel = 0;
		_cooldown = 0;

		var atr = new AverageTrueRange { Length = AtrPeriod };
		var sma = new SimpleMovingAverage { Length = MAPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(atr, sma, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var trailingStopDistance = atrValue * AtrMultiplier;

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		if (Position == 0)
		{
			if (candle.ClosePrice > smaValue)
			{
				BuyMarket();
				_entryPrice = candle.ClosePrice;
				_trailingStopLevel = _entryPrice - trailingStopDistance;
				_cooldown = CooldownBars;
			}
			else if (candle.ClosePrice < smaValue)
			{
				SellMarket();
				_entryPrice = candle.ClosePrice;
				_trailingStopLevel = _entryPrice + trailingStopDistance;
				_cooldown = CooldownBars;
			}
		}
		else if (Position > 0)
		{
			var newTrailingStopLevel = candle.ClosePrice - trailingStopDistance;
			if (newTrailingStopLevel > _trailingStopLevel)
				_trailingStopLevel = newTrailingStopLevel;

			if (candle.LowPrice <= _trailingStopLevel)
			{
				SellMarket();
				_cooldown = CooldownBars;
			}
		}
		else if (Position < 0)
		{
			var newTrailingStopLevel = candle.ClosePrice + trailingStopDistance;
			if (newTrailingStopLevel < _trailingStopLevel || _trailingStopLevel == 0)
				_trailingStopLevel = newTrailingStopLevel;

			if (candle.HighPrice >= _trailingStopLevel)
			{
				BuyMarket();
				_cooldown = CooldownBars;
			}
		}
	}
}