GitHub で見る

Stochastic 買われすぎ/売られすぎリバーサル

この戦略はStochasticオシレーターの極端なレベルに反応します。%Kラインが売られすぎ圏に沈むと、システムは反発を期待します。一方、買われすぎの読みは下落を予感させる可能性があります。本手法は短いイントラデイ足で動作するため、シグナルが素早く届きます。

テストでは年平均リターン約73%を示しています。暗号資産市場で最もよく機能します。

選択した時間軸にサブスクライブした後、%Kと%Dラインを監視します。%Kが20を下回り回復し始めると強気のセットアップが形成されます。逆に、%Kが80を超えて下向きに転じると弱気のセットアップが現れます。固定パーセントのストップが両方向のリスクをコントロールします。

%Kラインが50レベルを再び越えると、モメンタムが反対方向にシフトしたことを示してポジションが決済されます。ストップは最新のATRに連動するため、取引サイズはボラティリティに適応します。

詳細

  • エントリー条件:
    • ロング: %K < 20 で強気転換。
    • ショート: %K > 80 で弱気転換。
  • ロング/ショート: 両方。
  • エグジット条件: %Kが50レベルを越えるかストップロス。
  • ストップ: はい、2% の距離。
  • デフォルト値:
    • StochPeriod = 14
    • KPeriod = 3
    • DPeriod = 3
    • CandleType = 5 minute
  • フィルター:
    • カテゴリ: オシレーター
    • 方向: 両方
    • インジケーター: Stochastic
    • ストップ: はい
    • 複雑さ: 基本
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Stochastic Overbought/Oversold strategy.
/// Buys when K is oversold, sells when K is overbought.
/// </summary>
public class StochasticOverboughtOversoldStrategy : Strategy
{
	private readonly StrategyParam<int> _kPeriod;
	private readonly StrategyParam<int> _dPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private int _cooldown;

	/// <summary>
	/// K period.
	/// </summary>
	public int KPeriod
	{
		get => _kPeriod.Value;
		set => _kPeriod.Value = value;
	}

	/// <summary>
	/// D period.
	/// </summary>
	public int DPeriod
	{
		get => _dPeriod.Value;
		set => _dPeriod.Value = value;
	}

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

	/// <summary>
	/// Cooldown bars.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public StochasticOverboughtOversoldStrategy()
	{
		_kPeriod = Param(nameof(KPeriod), 3)
			.SetGreaterThanZero()
			.SetDisplay("K Period", "Smoothing period for %K", "Indicators");

		_dPeriod = Param(nameof(DPeriod), 3)
			.SetGreaterThanZero()
			.SetDisplay("D Period", "Smoothing period for %D", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_cooldownBars = Param(nameof(CooldownBars), 500)
			.SetRange(1, 1000)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_cooldown = default;
	}

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

		_cooldown = 0;

		var stochastic = new StochasticOscillator
		{
			K = { Length = KPeriod },
			D = { Length = DPeriod },
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(stochastic, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, stochastic);
			DrawOwnTrades(area);
		}
	}

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

		if (!stochValue.IsFormed)
			return;

		var stochTyped = (StochasticOscillatorValue)stochValue;

		if (stochTyped.K is not decimal kValue)
			return;

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		if (Position == 0 && kValue < 20)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		else if (Position == 0 && kValue > 80)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position > 0 && kValue > 80)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position < 0 && kValue < 20)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
	}
}