Auf GitHub ansehen

SAR Trailing-System-Strategie

Strategie, die in festen Zeitintervallen zufällige Long- oder Short-Positionen eingeht und den Ausstieg mithilfe des Parabolic-SAR-Indikators verwaltet. Der Parabolic-SAR-Wert fungiert als Trailing Stop: Die Position wird geschlossen, wenn der Preis das SAR-Level kreuzt.

Details

  • Einstiegskriterien:
    • Jedes TimerInterval, wenn keine offene Position vorhanden und UseRandomEntry aktiviert ist, wird ein zufälliger Long- oder Short-Trade eröffnet.
  • Long/Short: Beide
  • Ausstiegskriterien: Preis, der den Parabolic SAR kreuzt.
  • Stops: Anfänglicher Stop-Loss in Ticks mit Parabolic-SAR-Trailing-Ausstieg.
  • Standardwerte:
    • TimerInterval = 300 Sekunden
    • StopLossTicks = 10
    • AccelerationStep = 0.02
    • AccelerationMax = 0.2
    • UseRandomEntry = true
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame()
  • Filter:
    • Kategorie: Trendfolge
    • Richtung: Beide
    • Indikatoren: Parabolic SAR
    • Stops: Ja
    • Komplexität: Anfänger
    • Zeitrahmen: Kurzfristig
    • 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>
/// Strategy that uses Parabolic SAR for trend-following entries and exits.
/// Buy when price crosses above SAR, sell when below.
/// </summary>
public class SarTrailingSystemStrategy : Strategy
{
	private readonly StrategyParam<decimal> _accelerationStep;
	private readonly StrategyParam<decimal> _accelerationMax;
	private readonly StrategyParam<DataType> _candleType;

	public decimal AccelerationStep { get => _accelerationStep.Value; set => _accelerationStep.Value = value; }
	public decimal AccelerationMax { get => _accelerationMax.Value; set => _accelerationMax.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SarTrailingSystemStrategy()
	{
		_accelerationStep = Param(nameof(AccelerationStep), 0.02m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Step", "Parabolic SAR acceleration step", "Indicators");

		_accelerationMax = Param(nameof(AccelerationMax), 0.2m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Max", "Parabolic SAR maximum acceleration", "Indicators");

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

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

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

		var sar = new ParabolicSar
		{
			Acceleration = AccelerationStep,
			AccelerationStep = AccelerationStep,
			AccelerationMax = AccelerationMax
		};

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(sar, ProcessCandle).Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		// Price above SAR = uptrend, buy
		if (candle.ClosePrice > sarValue && Position <= 0)
			BuyMarket();
		// Price below SAR = downtrend, sell
		else if (candle.ClosePrice < sarValue && Position >= 0)
			SellMarket();
	}
}