Auf GitHub ansehen

Drei Parabolic SAR-Strategie

Die Drei Parabolic SAR-Strategie verwendet drei Parabolic SAR-Indikatoren, die auf 6-Stunden-, 3-Stunden- und 1-Stunden-Kerzen berechnet werden. Ein Trade wird auf dem 1-Stunden-Zeitrahmen eröffnet, wenn die beiden höheren Zeitrahmen die Richtung bestätigen und der 1-Stunden-SAR kippt.

Details

  • Einstiegskriterien:
    • SAR auf 6h-Kerzen ist unter dem Schlusskurs und SAR auf 3h-Kerzen unter dem Schlusskurs für Long; darüber für Short.
    • Auf 1h-Kerzen kreuzt der SAR den Preis: von oben nach unten für Long, von unten nach oben für Short.
  • Long/Short: Beide Richtungen.
  • Ausstiegskriterien: Position wird geschlossen, wenn der 1h-SAR gegen die Position dreht oder wenn einer der höheren Zeitrahmen-SARs umkehrt.
  • Stops: Nein.
  • Standardwerte:
    • Acceleration = 0.02
    • MaxAcceleration = 0.2
    • HigherTimeframe = TimeSpan.FromHours(6)
    • MiddleTimeframe = TimeSpan.FromHours(3)
    • TradingTimeframe = TimeSpan.FromHours(1)
  • Filter:
    • Kategorie: Trend
    • Richtung: Beide
    • Indikatoren: Parabolic SAR
    • Stops: Nein
    • Komplexität: Grundlegend
    • Zeitrahmen: Multi-Zeitrahmen
    • Saisonalität: Nein
    • Neuronale Netze: Nein
    • Divergenz: Nein
    • Risikolevel: Mittel
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 strategy using fast and slow SAR parameters.
/// Buys when price is above both SAR levels, sells when below both.
/// </summary>
public class ThreeParabolicSarStrategy : Strategy
{
	private readonly StrategyParam<decimal> _fastAcceleration;
	private readonly StrategyParam<decimal> _slowAcceleration;
	private readonly StrategyParam<DataType> _candleType;

	private bool _prevFastAbove;
	private bool _prevSlowAbove;
	private bool _hasPrev;

	public decimal FastAcceleration { get => _fastAcceleration.Value; set => _fastAcceleration.Value = value; }
	public decimal SlowAcceleration { get => _slowAcceleration.Value; set => _slowAcceleration.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ThreeParabolicSarStrategy()
	{
		_fastAcceleration = Param(nameof(FastAcceleration), 0.04m)
			.SetDisplay("Fast Acceleration", "Fast SAR acceleration", "SAR");

		_slowAcceleration = Param(nameof(SlowAcceleration), 0.01m)
			.SetDisplay("Slow Acceleration", "Slow SAR acceleration", "SAR");

		_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();
		_prevFastAbove = false;
		_prevSlowAbove = false;
		_hasPrev = false;
	}

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

		var fastSar = new ParabolicSar { Acceleration = FastAcceleration, AccelerationMax = 0.2m };
		var slowSar = new ParabolicSar { Acceleration = SlowAcceleration, AccelerationMax = 0.1m };

		SubscribeCandles(CandleType).Bind(fastSar, slowSar, ProcessCandle).Start();
	}

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

		var fastAbove = candle.ClosePrice > fastSar;
		var slowAbove = candle.ClosePrice > slowSar;

		if (!_hasPrev)
		{
			_prevFastAbove = fastAbove;
			_prevSlowAbove = slowAbove;
			_hasPrev = true;
			return;
		}

		// Buy when both SAR levels flip bullish
		if (fastAbove && slowAbove && (!_prevFastAbove || !_prevSlowAbove) && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Sell when both SAR levels flip bearish
		else if (!fastAbove && !slowAbove && (_prevFastAbove || _prevSlowAbove) && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
		// Exit long if slow SAR turns bearish
		else if (Position > 0 && !slowAbove)
		{
			SellMarket();
		}
		// Exit short if slow SAR turns bullish
		else if (Position < 0 && slowAbove)
		{
			BuyMarket();
		}

		_prevFastAbove = fastAbove;
		_prevSlowAbove = slowAbove;
	}
}