GitHub で見る

Parabolic SARアラート戦略

この戦略はParabolic SAR(Stop and Reverse)インジケーターを監視して、潜在的なトレンド転換を検知します。SARの値が価格の上から下に転換すると、アルゴリズムはそれを強気シグナルと解釈してロングポジションを建てます。SARが価格の下から上に移動すると、ショートポジションを建てます。

デフォルトの加速係数(0.02)と最大加速(0.2)はクラシックなParabolic SAR設定に従います。これらのパラメーターはインジケーターが価格にどのくらい速く近づくかを制御します。値が高いほどSARの反応が速くなりますが、ダマシが増える可能性があります。この戦略は完成したローソク足のみを処理し、履歴データを参照せずにクロスオーバーを識別するために前回のSARと価格の値を保存します。

リスク管理は明示的に定義されていません。この例ではエグジットに逆シグナルを使用します。追加の保護はフレームワークの組み込みメカニズムを通じて有効にできます。

詳細

  • エントリー条件: Parabolic SARが終値をクロスする。
  • ロング/ショート: 両方。
  • エグジット条件: 逆シグナル。
  • ストップ: 未定義。
  • デフォルト値:
    • InitialAcceleration = 0.02
    • MaxAcceleration = 0.2
    • CandleType = 5 minute
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: Parabolic SAR
    • ストップ: オプション
    • 複雑さ: 基本
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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 Alert Strategy.
/// Opens long or short positions when Parabolic SAR flips relative to price.
/// </summary>
public class ParabolicSarAlertStrategy : Strategy
{
	private readonly StrategyParam<decimal> _initialAcceleration;
	private readonly StrategyParam<decimal> _maxAcceleration;
	private readonly StrategyParam<DataType> _candleType;

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

	public decimal InitialAcceleration { get => _initialAcceleration.Value; set => _initialAcceleration.Value = value; }
	public decimal MaxAcceleration { get => _maxAcceleration.Value; set => _maxAcceleration.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ParabolicSarAlertStrategy()
	{
		_initialAcceleration = Param(nameof(InitialAcceleration), 0.02m)
			.SetDisplay("Initial Acceleration", "Initial acceleration factor for Parabolic SAR", "SAR Settings");
		_maxAcceleration = Param(nameof(MaxAcceleration), 0.2m)
			.SetDisplay("Max Acceleration", "Maximum acceleration factor for Parabolic SAR", "SAR Settings");
		_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()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSar = 0;
		_prevClose = 0;
		_initialized = false;
	}

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

		var parabolicSar = new ParabolicSar
		{
			Acceleration = InitialAcceleration,
			AccelerationMax = MaxAcceleration
		};

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

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

		if (!_initialized)
		{
			_prevSar = sarValue;
			_prevClose = candle.ClosePrice;
			_initialized = true;
			return;
		}

		var crossUp = _prevSar > _prevClose && sarValue < candle.ClosePrice;
		var crossDown = _prevSar < _prevClose && sarValue > candle.ClosePrice;

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

		_prevSar = sarValue;
		_prevClose = candle.ClosePrice;
	}
}