Auf GitHub ansehen

BreakThrough Strategy

The BreakThrough strategy executes trades when price crosses user-defined trendline levels. Two main levels are used:

  • Buy Line – price level to trigger a long position.
  • Sell Line – price level to trigger a short position.

Once a line is crossed from the opposite side, the strategy enters the market in that direction. Optional additional lines allow closing a position when price touches a specific level. Protective stop-loss, take-profit and trailing-stop distances are measured in pips from the entry price.

Details

  • Entry Criteria:
    • Long: price crosses above or below the Buy Line depending on its initial position.
    • Short: price crosses above or below the Sell Line depending on its initial position.
  • Long/Short: both sides.
  • Exit Criteria:
    • Price hits an optional take-profit or stop-loss line.
    • Price reaches take-profit or stop-loss distance in pips.
    • Trailing stop is triggered.
  • Stops: yes, using StopLossPips, TakeProfitPips and TrailingStopPips.
  • Default Values:
    • BuyLinePrice = 0 (disabled)
    • SellLinePrice = 0 (disabled)
    • TakeProfitPips = 100
    • StopLossPips = 30
    • TrailingStopPips = 20
  • Filters:
    • Category: Breakout
    • Direction: Both
    • Indicators: None
    • Stops: Yes
    • Complexity: Simple
    • Timeframe: Any (default 1 minute)
    • Risk level: Medium
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;
	}
}