GitHub で見る

MasterMind戦略

Stochasticオシレーターとウィリアムズ%Rを使用して、極端な売られすぎと買われすぎの状態を捉える戦略です。

概要

この戦略は2つのモメンタムインジケーターを監視します:

  • Stochastic Oscillator ベース期間100、スムージング3/3。
  • Williams %R 期間100。

Stochasticの%D値が3を下回り、Williams %Rが-99.9を下回ると売られすぎの市場を示し、ロングポジションを開きます。 Stochasticの%Dが97を上回り、Williams %Rが-0.1を上回ると買われすぎの市場を示し、ショートポジションを開きます。

トレードに入った後、アルゴリズムはストップロス、テイクプロフィット、トレーリングストップ、オプションのブレークイーブン移動でリスクを管理します。

パラメーター

  • StochasticLength – StochasticとWilliams %Rの計算期間。
  • StopLoss – エントリー価格からのストップロス距離(ポイント)。
  • TakeProfit – テイクプロフィット距離(ポイント)。
  • TrailingStop – トレーリングの起動距離(ポイント)。
  • TrailingStep – トレーリングストップのステップ(ポイント)。
  • BreakEven – ストップをエントリーに移動する利益(ポイント)。
  • CandleType – 戦略計算に使用するローソク足の時間軸。

インジケーター

  • StochasticOscillator
  • WilliamsR

取引ルール

  1. %D < 3 かつ Williams %R < -99.9 のとき買い
  2. %D > 97 かつ Williams %R > -0.1 のとき売り
  3. エントリー後、ストップロスとテイクプロフィットを適用する。
  4. 価格がBreakEven進んだらストップをエントリーに移動する。
  5. 価格がTrailingStop動いたらトレーリングストップを起動し、TrailingStepずつ移動する。

注記

この戦略はStockSharpの高レベルAPIを使用しており、教育用の例として意図されています。

using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy entering on extreme Stochastic and Williams %R values.
/// </summary>
public class TheMasterMindStrategy : Strategy
{
	private readonly StrategyParam<int> _stochLength;
	private readonly StrategyParam<DataType> _candleType;

	private WilliamsR _wpr;
	private decimal? _prevD;
	private int _lastSignal;

	/// <summary>
	/// Stochastic base length.
	/// </summary>
	public int StochasticLength
	{
		get => _stochLength.Value;
		set => _stochLength.Value = value;
	}

	/// <summary>
	/// Candle type for strategy calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of <see cref="TheMasterMindStrategy"/>.
	/// </summary>
	public TheMasterMindStrategy()
	{
		_stochLength = Param(nameof(StochasticLength), 14)
			.SetDisplay("Stochastic Length", "Base length for Stochastic", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for calculations", "Common");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevD = null;
		_lastSignal = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var stochastic = new StochasticOscillator
		{
			K = { Length = StochasticLength },
			D = { Length = 3 }
		};

		_wpr = new WilliamsR { Length = StochasticLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(stochastic, (candle, stochValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				// Process WilliamsR manually (candle-based)
				var wprResult = _wpr.Process(candle);
				if (!_wpr.IsFormed)
					return;

				var stoch = (IStochasticOscillatorValue)stochValue;
				if (stoch.D is not decimal d || stoch.K is not decimal k)
					return;

				var wpr = wprResult.ToDecimal();

				var buySignal = _prevD >= 20m && d < 20m && wpr < -85m;
				var sellSignal = _prevD <= 80m && d > 80m && wpr > -15m;

				if (buySignal && _lastSignal != 1 && Position <= 0)
				{
					BuyMarket();
					_lastSignal = 1;
				}
				else if (sellSignal && _lastSignal != -1 && Position >= 0)
				{
					SellMarket();
					_lastSignal = -1;
				}

				_prevD = d;
			})
			.Start();

		StartProtection(
			new Unit(2000m, UnitTypes.Absolute),
			new Unit(1000m, UnitTypes.Absolute));
	}
}