在 GitHub 上查看

三线突破策略

该策略利用 Three Line Break 指标来捕捉趋势反转。 指标将当前的最高价和最低价与前 N 根已完成蜡烛的最高点和最低点进行比较。 在下跌趋势中向上突破最近高点表示新一轮上升趋势并开多单;在上升趋势中跌破最近低点则开空单。 每当出现相反信号时,仓位会反向。

细节

  • 入场条件
    • 多头:Downtrend 变为 Uptrend
    • 空头:Uptrend 变为 Downtrend
  • 多/空:双向
  • 出场条件:相反信号(反手)
  • 止损:无
  • 默认值
    • LinesBreak = 3
    • CandleType = TimeSpan.FromHours(12).TimeFrame()
  • 筛选
    • 分类:趋势跟随
    • 方向:双向
    • 指标:Highest, Lowest(Three Line Break 逻辑)
    • 止损:无
    • 复杂度:基础
    • 时间框架:波段
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy based on the Three Line Break pattern.
/// Detects trend reversals when price breaks above/below recent N-bar high/low.
/// </summary>
public class ThreeLineBreakStrategy : Strategy
{
	private readonly StrategyParam<int> _linesBreak;
	private readonly StrategyParam<DataType> _candleType;

	private Lowest _lowest;
	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _trendUp = true;

	public int LinesBreak { get => _linesBreak.Value; set => _linesBreak.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ThreeLineBreakStrategy()
	{
		_linesBreak = Param(nameof(LinesBreak), 3)
			.SetGreaterThanZero()
			.SetDisplay("Lines Break", "Number of lines for trend detection", "General");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_lowest = null;
		_prevHigh = 0;
		_prevLow = 0;
		_trendUp = true;
	}

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

		var highest = new Highest { Length = LinesBreak };
		_lowest = new Lowest { Length = LinesBreak };
		Indicators.Add(_lowest);

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(highest, ProcessCandle)
			.Start();

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

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

		var lowValue = _lowest.Process(highValue);

		if (!highValue.IsFormed || !lowValue.IsFormed)
			return;

		var currentHigh = highValue.GetValue<decimal>();
		var currentLow = lowValue.GetValue<decimal>();

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevHigh = currentHigh;
			_prevLow = currentLow;
			return;
		}

		if (_prevHigh == 0 || _prevLow == 0)
		{
			_prevHigh = currentHigh;
			_prevLow = currentLow;
			return;
		}

		var trendUp = _trendUp;

		if (trendUp && candle.LowPrice < _prevLow)
			trendUp = false;
		else if (!trendUp && candle.HighPrice > _prevHigh)
			trendUp = true;

		if (trendUp != _trendUp)
		{
			if (trendUp && Position <= 0)
				BuyMarket();
			else if (!trendUp && Position >= 0)
				SellMarket();
		}

		_trendUp = trendUp;
		_prevHigh = currentHigh;
		_prevLow = currentLow;
	}
}