在 GitHub 上查看

OBV ATR 策略

该策略监控能量潮 (OBV),当 OBV 突破最近的高点或低点时开仓,多空皆可,类似 ATR 突破通道。

细节

  • 入场条件:OBV 上穿前高做多;下破前低做空。
  • 多空方向:双向。
  • 出场条件:反向信号或保护单。
  • 止损:有。
  • 默认值
    • LookbackLength = 30
    • CandleType = TimeSpan.FromMinutes(5)
  • 过滤器
    • 分类:突破
    • 方向:双向
    • 指标:OBV, Highest, Lowest
    • 止损:有
    • 复杂度:中等
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:中
using System;
using System.Collections.Generic;

using Ecng.Common;

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

using StockSharp.Algo;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// On-Balance Volume (OBV) breakout strategy with ATR-like channel.
/// Opens long when OBV crosses above previous high, short when below previous low.
/// </summary>
public class ObvAtrStrategy : Strategy
{
	private readonly StrategyParam<int> _lookbackLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevObv;
	private decimal? _prevHigh;
	private decimal? _prevLow;
	private int _mode;
	private int _prevMode;

	/// <summary>
	/// Lookback length for OBV high/low calculation.
	/// </summary>
	public int LookbackLength
	{
		get => _lookbackLength.Value;
		set => _lookbackLength.Value = value;
	}

	/// <summary>
	/// Candle type for strategy calculation.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initialize <see cref="ObvAtrStrategy"/>.
	/// </summary>
	public ObvAtrStrategy()
	{
		_lookbackLength = Param(nameof(LookbackLength), 60)
			.SetGreaterThanZero()
			.SetDisplay("OBV Lookback", "Lookback length for OBV highs and lows", "Parameters")
			
			.SetOptimize(10, 100, 10);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for strategy", "Parameters");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_prevObv = null;
		_prevHigh = null;
		_prevLow = null;
		_mode = 0;
		_prevMode = 0;
	}

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

		var obv = new OnBalanceVolume();
		var highest = new Highest { Length = LookbackLength };
		var lowest = new Lowest { Length = LookbackLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(obv, (candle, value) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				var obvVal = value.ToDecimal();

				var prevHigh = _prevHigh;
				var prevLow = _prevLow;
				var prevObv = _prevObv;

				var high = highest.Process(value).ToDecimal();
				var low = lowest.Process(value).ToDecimal();

				_prevObv = obvVal;
				_prevHigh = high;
				_prevLow = low;

				if (prevHigh.HasValue && prevObv.HasValue && obvVal > prevHigh.Value && prevObv <= prevHigh.Value)
					_mode = 1;
				else if (prevLow.HasValue && prevObv.HasValue && obvVal < prevLow.Value && prevObv >= prevLow.Value)
					_mode = -1;

				var bullSignal = _mode == 1 && _prevMode != 1;
				var bearSignal = _mode == -1 && _prevMode != -1;
				_prevMode = _mode;

				if (!IsFormedAndOnlineAndAllowTrading())
					return;

				if (bullSignal && Position <= 0)
					BuyMarket();

				if (bearSignal && Position >= 0)
					SellMarket();
			})
			.Start();

		StartProtection(
			takeProfit: new Unit(5, UnitTypes.Percent),
			stopLoss: new Unit(3, UnitTypes.Percent));

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