GitHub で見る

Brake Parabolic Strategy

The strategy implements the Brake Parabolic indicator that projects a parabolic barrier above or below price. When the barrier is broken, the trend flips and a new position is opened in the direction of the breakout. The algorithm trails the extreme price with a curved line defined by parameters A, B, and Shift.

Testing indicates an average annual return of about 48%. It performs best in trending markets on higher timeframes.

The system waits for the barrier to switch sides. A bullish flip closes any short and opens a new long position. A bearish flip closes any long and opens a short. While in a trend, opposite positions are closed when the indicator confirms the direction.

Details

  • Entry Criteria:
    • Long: Barrier switches from above price to below price.
    • Short: Barrier switches from below price to above price.
  • Long/Short: Both directions.
  • Exit Criteria: Opposite signal or indicator confirms opposing trend.
  • Stops: No fixed stops; exits rely on barrier reversal.
  • Default Values:
    • A = 1.5
    • B = 1.0
    • BeginShift = 10
    • CandleType = 4-hour timeframe
  • Filters:
    • Category: Trend
    • Direction: Both
    • Indicators: Custom
    • Stops: No
    • Complexity: Medium
    • Timeframe: Swing
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • 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>
/// Parabolic SAR crossover strategy.
/// </summary>
public class BrakeParabolicStrategy : Strategy
{
	private readonly StrategyParam<decimal> _sarStep;
	private readonly StrategyParam<decimal> _sarMax;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevClose;
	private decimal _prevSar;
	private bool _hasPrev;

	public decimal SarStep { get => _sarStep.Value; set => _sarStep.Value = value; }
	public decimal SarMax { get => _sarMax.Value; set => _sarMax.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BrakeParabolicStrategy()
	{
		_sarStep = Param(nameof(SarStep), 0.02m)
			.SetDisplay("SAR Step", "Acceleration step", "Indicators");
		_sarMax = Param(nameof(SarMax), 0.2m)
			.SetDisplay("SAR Max", "Maximum acceleration", "Indicators");
		_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();
		_prevClose = 0;
		_prevSar = 0;
		_hasPrev = false;
	}

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

		var sar = new ParabolicSar { AccelerationStep = SarStep, AccelerationMax = SarMax };

		SubscribeCandles(CandleType)
			.Bind(sar, ProcessCandle)
			.Start();
	}

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

		var close = candle.ClosePrice;

		if (!_hasPrev)
		{
			_prevClose = close;
			_prevSar = sarVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevClose <= _prevSar && close > sarVal;
		var crossDown = _prevClose >= _prevSar && close < sarVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevClose = close;
		_prevSar = sarVal;
	}
}