GitHub で見る

DiNapoli Stochastic戦略

この戦略はDiNapoli Stochasticオシレーターに基づくトレードシステムを実装しています。ストキャスティクス指標の%Kラインと%Dラインのクロスに反応します。

戦略のロジック

  1. 選択した時間軸のローソク足を購読する。
  2. スムージング期間を持つ標準的なStochasticオシレーターを使用してDiNapoli Stochasticの値を計算する。
  3. 前の%Kが%Dより上にあった場合、ショートポジションを決済する。
  4. 前の%Kが%Dより下にあった場合、ロングポジションを決済する。
  5. %Kが%Dを下から上にクロスし、ロング取引が許可されている場合にロングポジションを開く。
  6. %Kが%Dを上から下にクロスし、ショート取引が許可されている場合にショートポジションを開く。

パラメーター

  • FastK – %Kの基本期間。
  • SlowK – %Kのスムージング期間。
  • SlowD – %Dのスムージング期間。
  • BuyOpen – ロングエントリーの有効・無効。
  • SellOpen – ショートエントリーの有効・無効。
  • BuyClose – ロングポジション決済の有効・無効。
  • SellClose – ショートポジション決済の有効・無効。
  • CandleType – 計算に使用するローソク足の時間軸。

注意事項

この戦略はStockSharpの高レベルAPIを使用し、完成したローソク足のみを処理します。インジケーターの値は履歴値のリクエストを使用せずにBindExを通じて取得されます。

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>
/// DiNapoli Stochastic cross strategy.
/// Opens a long position when the %K line crosses above %D and
/// a short position when %K crosses below %D.
/// </summary>
public class DiNapoliStochasticStrategy : Strategy
{
	private readonly StrategyParam<int> _fastK;
	private readonly StrategyParam<int> _slowD;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevK;
	private decimal _prevD;
	private bool _prevReady;

	public int FastK { get => _fastK.Value; set => _fastK.Value = value; }
	public int SlowD { get => _slowD.Value; set => _slowD.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public DiNapoliStochasticStrategy()
	{
		_fastK = Param(nameof(FastK), 8)
			.SetGreaterThanZero()
			.SetDisplay("Fast %K", "Base period for %K", "DiNapoli");

		_slowD = Param(nameof(SlowD), 3)
			.SetGreaterThanZero()
			.SetDisplay("Slow %D", "%D smoothing period", "DiNapoli");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(6).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for calculation", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevK = 0m;
		_prevD = 0m;
		_prevReady = false;
	}

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

		StartProtection(null, null);

		var stochastic = new StochasticOscillator();
		stochastic.K.Length = FastK;
		stochastic.D.Length = SlowD;

		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;

		var stoch = (IStochasticOscillatorValue)stochValue;

		if (stoch.K is not decimal k || stoch.D is not decimal d)
			return;

		if (!_prevReady)
		{
			_prevK = k;
			_prevD = d;
			_prevReady = true;
			return;
		}

		// %K crosses above %D - buy signal
		if (_prevK <= _prevD && k > d && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// %K crosses below %D - sell signal
		else if (_prevK >= _prevD && k < d && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		_prevK = k;
		_prevD = d;
	}
}