GitHub で見る

Bulls Bears Eyes戦略

概要

この戦略はBulls PowerBears Powerインジケーターを使用して、強気と弱気の圧力のバランスを評価します。2つのインジケーターは0から100にスケーリングされた単一のオシレーターに組み合わされます。高い値は買い方の優位性を示し、低い値は売り方の強さを示します。

取引の判断はオリジナルのBullsBearsEyesエキスパートと同様のしきい値レベルに基づいています。オシレーターが買われ過ぎレベルを下から上に抜けると、ロングポジションが開かれ、ショートポジションは閉じられます。逆に、売られ過ぎレベルを下に抜けるとショートエントリーがトリガーされ、既存のロングが閉じられます。しきい値の間の中立的な値は現在のポジションを維持しますが、反対の取引を閉じます。

パラメーター

  • Period – Bulls/Bears Powerの平均化期間(デフォルト:13)。
  • High Level – ロングシグナルを生成する買われ過ぎしきい値(デフォルト:75)。
  • Middle Level – トレンド解釈に使用する参照中間レベル(デフォルト:50)。
  • Low Level – ショートシグナルを生成する売られ過ぎしきい値(デフォルト:25)。
  • Candle Type – 戦略が処理するローソク足の時間軸(デフォルト:4時間足)。

エントリーとエグジットのルール

  1. 各ローソク足のBulls PowerとBears Powerを計算し、0から100の間のオシレーター値を導き出す。
  2. ロングエントリー: オシレーターがHigh Levelを下から上に抜ける。ロングを開く前にショートポジションを閉じる。
  3. ショートエントリー: オシレーターがLow Levelを上から下に抜ける。ショートを開く前に既存のロングポジションを閉じる。
  4. ポジション決済: オシレーターが側面を切り替える(中間ゾーンの上/下)と、反対のポジションが閉じられる。

オシレーターは視覚的な分析のためにローソク足とともにプロットされます。

注意事項

  • 戦略はインジケーター処理に高レベルのSubscribeCandlesBind APIを使用します。
  • 保護メカニズムは起動時にStartProtection()で有効化されます。
  • 早期シグナルを避けるために、完成したローソク足のみが評価されます。
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>
/// EMA crossover strategy.
/// </summary>
public class BullsBearsEyesStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BullsBearsEyesStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 13)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Parameters");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Parameters");
		_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();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

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

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}