在 GitHub 上查看

BreakThrough 策略

BreakThrough 策略在价格突破用户设定的趋势线水平时开仓。 主要使用两个价格水平:

  • Buy Line – 触发多头的价格。
  • Sell Line – 触发空头的价格。

当价格从另一侧穿越这些水平时,策略按方向进场。 还可以设置附加的线,在价格触及时立即平仓。 保护性止损、止盈和追踪止损以入场价的点数表示。

详情

  • 入场条件
    • 多头:价格根据初始位置向上或向下突破 Buy Line。
    • 空头:价格根据初始位置向上或向下突破 Sell Line。
  • 多空方向:双向。
  • 出场条件
    • 价格触及附加的止盈或止损线。
    • 价格达到以点数表示的止盈或止损距离。
    • 触发追踪止损。
  • 止损:是,使用 StopLossPipsTakeProfitPipsTrailingStopPips
  • 默认值
    • BuyLinePrice = 0(关闭)
    • SellLinePrice = 0(关闭)
    • TakeProfitPips = 100
    • StopLossPips = 30
    • TrailingStopPips = 20
  • 过滤器
    • 类型: 突破
    • 方向: 双向
    • 指标: 无
    • 止损: 是
    • 复杂度: 简单
    • 时间框架: 任意(默认 1 分钟)
    • 风险等级: 中等
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>
/// Breakout strategy using Highest/Lowest channel.
/// </summary>
public class BreakThroughStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _hasPrev;

	public int Period { get => _period.Value; set => _period.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BreakThroughStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Channel lookback period", "Parameters");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0;
		_prevLow = 0;
		_hasPrev = false;
	}

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

		var highest = new Highest { Length = Period };
		var lowest = new Lowest { Length = Period };

		SubscribeCandles(CandleType)
			.Bind(highest, lowest, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevHigh = high;
			_prevLow = low;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		if (close > _prevHigh && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (close < _prevLow && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevHigh = high;
		_prevLow = low;
	}
}