GitHub で見る

Brake Parabolic戦略

この戦略はBrake Parabolicインジケーターを実装しており、価格の上方または下方に放物線状のバリアを投影します。バリアが突破されると、トレンドが反転し、ブレイクアウトの方向に新しいポジションが開かれます。アルゴリズムはパラメーターABShiftで定義された曲線で極値価格を追跡します。

テストでは平均年間収益率約48%を示しています。高い時間軸のトレンド市場で最も優れたパフォーマンスを発揮します。

システムはバリアが側面を切り替えるのを待ちます。強気の反転は既存のショートポジションを閉じて新しいロングポジションを開きます。弱気の反転は既存のロングポジションを閉じてショートポジションを開きます。トレンド中は、インジケーターが方向を確認したときに反対のポジションが閉じられます。

詳細

  • エントリー条件:
    • ロング: バリアが価格の上から下に切り替わる。
    • ショート: バリアが価格の下から上に切り替わる。
  • ロング/ショート: 両方向。
  • エグジット条件: 反対シグナルまたはインジケーターが反対トレンドを確認。
  • ストップ: 固定ストップなし;決済はバリアの反転に依存。
  • デフォルト値:
    • A = 1.5
    • B = 1.0
    • BeginShift = 10
    • CandleType = 4時間時間軸
  • フィルター:
    • カテゴリ: トレンド
    • 方向: 両方
    • インジケーター: カスタム
    • ストップ: いいえ
    • 複雑さ: 中程度
    • 時間軸: スイング
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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.
/// </summary>
public class BrakeParabolicStrategy : Strategy
{
	private readonly StrategyParam<decimal> _sarStep;
	private readonly StrategyParam<decimal> _sarMax;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevClose;
	private decimal _prevSar;
	private bool _hasPrev;

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

	public BrakeParabolicStrategy()
	{
		_sarStep = Param(nameof(SarStep), 0.02m)
			.SetDisplay("SAR Step", "Acceleration step", "Indicators");
		_sarMax = Param(nameof(SarMax), 0.2m)
			.SetDisplay("SAR Max", "Maximum acceleration", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

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

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

		var sar = new ParabolicSar { AccelerationStep = SarStep, AccelerationMax = SarMax };

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

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

		var close = candle.ClosePrice;

		if (!_hasPrev)
		{
			_prevClose = close;
			_prevSar = sarVal;
			_hasPrev = true;
			return;
		}

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

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

		_prevClose = close;
		_prevSar = sarVal;
	}
}