GitHub で見る

PSAR トレーダー戦略

PSAR トレーダー戦略は、Parabolic SAR インジケーターの変化に基づいて行動します。SAR が価格の下に移動するとロングポジションを開き、SAR が価格の上に移動するとショートポジションを開きます。オプションの「Close On Opposite」設定は、反対のシグナルが現れたときにポジションを反転させます。取引は設定されたセッション時間中のみ行われます。ストップロスとテイクプロフィットは保護モジュールによって管理されます。

詳細

  • エントリー条件: 価格が Parabolic SAR を交差すること。
  • ロング/ショート: 両方向。
  • エグジット条件: 反対の SAR 交差またはポジション反転。
  • ストップ: あり、パラメーターで固定。
  • デフォルト値:
    • SarStep = 0.001m
    • SarMaxStep = 0.2m
    • StartHour = 0
    • EndHour = 23
    • CloseOnOpposite = true
    • TakeValue = 50 (absolute)
    • StopValue = 50 (absolute)
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: トレンド
    • 方向: 両方
    • インジケーター: Parabolic SAR
    • ストップ: 固定
    • 複雑さ: 基本
    • 時間軸: イントラデイ (5m)
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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 cross strategy.
/// Opens long when price moves above SAR and short when price moves below SAR.
/// </summary>
public class PsarTraderStrategy : Strategy
{
	private readonly StrategyParam<decimal> _sarStep;
	private readonly StrategyParam<decimal> _sarMaxStep;
	private readonly StrategyParam<DataType> _candleType;

	private bool _prevPriceAboveSar;
	private bool _hasPrev;

	public decimal SarStep { get => _sarStep.Value; set => _sarStep.Value = value; }
	public decimal SarMaxStep { get => _sarMaxStep.Value; set => _sarMaxStep.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public PsarTraderStrategy()
	{
		_sarStep = Param(nameof(SarStep), 0.001m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Step", "Acceleration factor for Parabolic SAR", "Parabolic SAR");

		_sarMaxStep = Param(nameof(SarMaxStep), 0.2m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Max Step", "Maximum acceleration factor", "Parabolic SAR");

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

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

		var parabolicSar = new ParabolicSar
		{
			AccelerationStep = SarStep,
			AccelerationMax = SarMaxStep
		};

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

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

		var isPriceAboveSar = candle.ClosePrice > sarValue;

		if (!_hasPrev)
		{
			_prevPriceAboveSar = isPriceAboveSar;
			_hasPrev = true;
			return;
		}

		if (_prevPriceAboveSar != isPriceAboveSar)
		{
			if (isPriceAboveSar && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (!isPriceAboveSar && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prevPriceAboveSar = isPriceAboveSar;
	}
}