在 GitHub 上查看

三重抛物线SAR策略

三重抛物线SAR策略在6小时、3小时和1小时周期上计算三个抛物线SAR指标。当两个较高周期确认方向且1小时SAR翻转时,在1小时周期上开仓。

细节

  • 入场条件
    • 长仓:6小时和3小时SAR低于收盘价,同时1小时SAR从上方跌破价格。
    • 短仓:6小时和3小时SAR高于收盘价,同时1小时SAR从下方突破价格。
  • 多空方向:双向。
  • 出场条件:当1小时SAR反向或任意高周期SAR反转时平仓。
  • 止损:无。
  • 默认值
    • Acceleration = 0.02
    • MaxAcceleration = 0.2
    • HigherTimeframe = TimeSpan.FromHours(6)
    • MiddleTimeframe = TimeSpan.FromHours(3)
    • TradingTimeframe = TimeSpan.FromHours(1)
  • 过滤器
    • 分类:Trend
    • 方向:Both
    • 指标:Parabolic SAR
    • 止损:无
    • 复杂度:Basic
    • 周期:Multi-timeframe
    • 季节性:No
    • 神经网络:No
    • 背离:No
    • 风险等级: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 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;
	}
}