GitHub で見る

PSAR トレーダー Ticks 戦略

Parabolic SAR インジケーターに基づく戦略です。PSAR Trader Ticks は Parabolic SAR インジケーターのドットに従い、価格が一方から他方に交差したときに反応します。価格が SAR より上に動いたときにロングポジションを開き、価格が SAR より下に動いたときにショートポジションを開きます。取引は特定の時間帯に制限することができ、反対のシグナルが現れたときに既存のポジションをオプションでクローズできます。この戦略はティック単位で測定されたテイクプロフィットとストップロスのレベルも適用します。

詳細

  • エントリー条件: 価格が Parabolic SAR インジケーターを交差すること。
  • ロング/ショート: 両方向。
  • エグジット条件: 反対のシグナル(オプション)、ストップロスまたはテイクプロフィット。
  • ストップ: ティック単位のテイクプロフィットとストップロス。
  • デフォルト値:
    • Step = 0.001m
    • Maximum = 0.2m
    • TakeProfitTicks = 50
    • StopLossTicks = 50
    • StartHour = 0
    • EndHour = 23
    • CloseOnOpposite = true
    • 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>
/// PSAR Trader strategy - opens long when price crosses above SAR
/// and short when price crosses below SAR.
/// </summary>
public class PsarTraderTicksStrategy : Strategy
{
	private readonly StrategyParam<decimal> _step;
	private readonly StrategyParam<decimal> _maximum;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSar;
	private decimal _prevPrice;
	private bool _hasPrev;

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

	public PsarTraderTicksStrategy()
	{
		_step = Param(nameof(Step), 0.001m)
			.SetDisplay("SAR Step", "Acceleration factor step", "Indicators");
		_maximum = Param(nameof(Maximum), 0.2m)
			.SetDisplay("SAR Maximum", "Maximum acceleration factor", "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()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSar = 0;
		_prevPrice = 0;
		_hasPrev = false;
	}

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

		var psar = new ParabolicSar
		{
			AccelerationStep = Step,
			AccelerationMax = Maximum
		};

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

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

		if (!_hasPrev)
		{
			_prevSar = sarValue;
			_prevPrice = candle.ClosePrice;
			_hasPrev = true;
			return;
		}

		var prevAbove = _prevPrice > _prevSar;
		var currAbove = candle.ClosePrice > sarValue;

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

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