在 GitHub 上查看

Batman ATR 拖尾止损策略

该策略基于原版“Batman”专家顾问,使用 平均真实波幅 (ATR) 构建动态支撑和阻力线,并在价格突破这些水平时入场。

逻辑

  1. 计算可配置周期的 ATR。
  2. 根据当前价格计算支撑与阻力:
    • support = price - ATR * factor
    • resistance = price + ATR * factor
  3. 根据当前趋势维护最近的支撑或阻力。
  4. 价格向上突破阻力时,开多仓。
  5. 价格向下突破支撑时,开空仓。

价格可选择使用收盘价或典型价 (高+低+收)/3

参数

名称 说明
ATR Period ATR 指标的周期。
ATR Factor 用于生成止损线的 ATR 倍数。
Use Typical Price 若启用,则使用 (高+低+收)/3
Candle Type 用于计算的 K 线类型。

备注

  • 策略使用高层 API,采用 SubscribeCandlesBind
  • 在启动时调用 StartProtection() 保护持仓。
  • 仅在 K 线完成后执行交易。
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 based on ATR trailing stop similar to "Batman" EA.
/// Opens long when price breaks above ATR-based support.
/// Opens short when price breaks below ATR-based resistance.
/// </summary>
public class BatmanAtrTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _factor;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _levelUp;
	private decimal _levelDown;
	private int _direction;
	private bool _isInitialized;

	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
	public decimal Factor { get => _factor.Value; set => _factor.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BatmanAtrTrailingStopStrategy()
	{
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "ATR indicator period", "General");

		_factor = Param(nameof(Factor), 1.5m)
			.SetGreaterThanZero()
			.SetDisplay("ATR Factor", "Multiplier for ATR distance", "General");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_levelUp = 0;
		_levelDown = 0;
		_direction = 1;
		_isInitialized = false;
	}

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

		var stdev = new StandardDeviation { Length = AtrPeriod };
		SubscribeCandles(CandleType).Bind(stdev, ProcessCandle).Start();
	}

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

		var close = candle.ClosePrice;
		var currUp = close - stdevValue * Factor;
		var currDown = close + stdevValue * Factor;

		if (!_isInitialized)
		{
			_levelUp = currUp;
			_levelDown = currDown;
			_isInitialized = true;
			return;
		}

		if (_direction == 1)
		{
			if (currUp > _levelUp)
				_levelUp = currUp;

			if (candle.LowPrice < _levelUp)
			{
				_direction = -1;
				_levelDown = currDown;
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}
		else
		{
			if (currDown < _levelDown)
				_levelDown = currDown;

			if (candle.HighPrice > _levelDown)
			{
				_direction = 1;
				_levelUp = currUp;
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
		}
	}
}