在 GitHub 上查看

Parabolic SAR Bug

Parabolic SAR Bug 策略利用 Parabolic SAR 指标捕捉趋势反转。当 SAR 点位从价格上方翻到下方时开多单,反之开空单。可选的反向模式会反转信号。通过内置的保护模块支持止损、止盈以及可选的跟踪止损。

细节

  • 入场条件:Parabolic SAR 方向变化。
  • 多空方向:双向。
  • 出场条件:相反的 SAR 信号或保护性止损。
  • 止损类型:止损、止盈、可选跟踪止损。
  • 默认值
    • Step = 0.02
    • MaxStep = 0.2
    • StopLossPercent = 2
    • TakeProfitPercent = 1
    • UseTrailingStop = false
    • Reverse = false
    • CloseOnSar = true
    • CandleType = TimeSpan.FromMinutes(5)
  • 筛选
    • 类别: 趋势
    • 方向: 双向
    • 指标: Parabolic SAR
    • 止损: 止损、止盈
    • 复杂度: 基础
    • 时间框架: 日内 (5 分钟)
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中等
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.
/// Buys when price crosses above SAR, sells when price crosses below.
/// </summary>
public class ParabolicSarBugStrategy : Strategy
{
	private readonly StrategyParam<decimal> _step;
	private readonly StrategyParam<decimal> _maxStep;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSar;
	private decimal _prevClose;
	private bool _initialized;

	public decimal Step { get => _step.Value; set => _step.Value = value; }
	public decimal MaxStep { get => _maxStep.Value; set => _maxStep.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ParabolicSarBugStrategy()
	{
		_step = Param(nameof(Step), 0.02m)
			.SetDisplay("Step", "Acceleration factor", "Indicator");

		_maxStep = Param(nameof(MaxStep), 0.2m)
			.SetDisplay("Max Step", "Maximum acceleration", "Indicator");

		_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();
		_prevSar = 0;
		_prevClose = 0;
		_initialized = false;
	}

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

		var sar = new ParabolicSar
		{
			Acceleration = Step,
			AccelerationMax = MaxStep
		};

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

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

		var close = candle.ClosePrice;

		if (!_initialized)
		{
			_prevSar = sarValue;
			_prevClose = close;
			_initialized = true;
			return;
		}

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

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

		_prevSar = sarValue;
		_prevClose = close;
	}
}