在 GitHub 上查看

Breakdown Level Day 策略

该策略在指定时间在日内高低区间外放置买入和卖出止损单。当价格突破早盘区间时尝试捕捉趋势。还可以使用止损、止盈、保本移动和跟踪止损来管理已开仓位。

细节

  • 入场:在 OrderTime 时,于日内高点上方 Delta 个跳动点挂买入止损单,并在日内低点下方 Delta 个跳动点挂卖出止损单。
  • 出场:止损和止盈与突破订单同时提交。保本移动和跟踪止损可调整保护性止损。
  • 指标:无。
  • 周期:默认 1 分钟K线。
  • 风险:仓位大小由策略的 Volume 属性决定。

参数

  • OrderTime — 提交挂单的时间。
  • Delta — 距离区间边界的跳动点数。
  • StopLoss — 保护性止损的跳动点数。
  • TakeProfit — 止盈目标的跳动点数。
  • BreakEven — 当盈利达到该跳动点数时将止损移至入场价。
  • Trailing — 跟踪止损的跳动点数。
  • CandleType — 用于计算的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>
/// Day range breakout strategy. Tracks the day's high/low during accumulation period,
/// then enters on breakout above high or below low.
/// </summary>
public class BreakdownLevelDayStrategy : Strategy
{
	private readonly StrategyParam<int> _lookback;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _rangeHigh;
	private decimal _rangeLow;
	private int _barCount;
	private bool _rangeEstablished;

	public int Lookback { get => _lookback.Value; set => _lookback.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BreakdownLevelDayStrategy()
	{
		_lookback = Param(nameof(Lookback), 20)
			.SetGreaterThanZero()
			.SetDisplay("Lookback", "Bars to establish range", "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();
		_rangeHigh = 0;
		_rangeLow = decimal.MaxValue;
		_barCount = 0;
		_rangeEstablished = false;
	}

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

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

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

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

		if (!_rangeEstablished)
		{
			if (candle.HighPrice > _rangeHigh)
				_rangeHigh = candle.HighPrice;
			if (candle.LowPrice < _rangeLow)
				_rangeLow = candle.LowPrice;

			_barCount++;

			if (_barCount >= Lookback)
				_rangeEstablished = true;

			return;
		}

		var price = candle.ClosePrice;

		// Breakout above range high
		if (price > _rangeHigh && Position <= 0)
		{
			BuyMarket();
			// Reset range for next setup
			_rangeHigh = candle.HighPrice;
			_rangeLow = candle.LowPrice;
			_barCount = 1;
			_rangeEstablished = false;
		}
		// Breakdown below range low
		else if (price < _rangeLow && Position >= 0)
		{
			SellMarket();
			_rangeHigh = candle.HighPrice;
			_rangeLow = candle.LowPrice;
			_barCount = 1;
			_rangeEstablished = false;
		}
		else
		{
			// Update range
			if (candle.HighPrice > _rangeHigh)
				_rangeHigh = candle.HighPrice;
			if (candle.LowPrice < _rangeLow)
				_rangeLow = candle.LowPrice;
		}
	}
}