GitHub で見る

Karacatica 戦略

概要

Karacatica戦略は、価格アクションと平均方向性指数(ADX)を組み合わせたトレンドフォローアプローチです。現在の終値が指定した本数前のローソク足の終値より高いまたは低い状況を探し、+DIまたは-DIラインの優勢で動きを確認します。

インジケーター

  • Average Directional Index (ADX) – トレンドの強さを測定し、+DIと-DIのコンポーネントを提供します。
  • 価格比較 – 最新の終値がPeriod本前の終値より上か下かを確認します。

パラメーター

  • Period – ADX計算と価格比較のルックバックの両方に使用するローソク足数。デフォルトは70。
  • TakeProfitPercent – エントリー価格のパーセントで表したテイクプロフィット。デフォルトは2%。
  • StopLossPercent – エントリー価格のパーセントで表したストップロス。デフォルトは1%。
  • CandleType – サブスクライブするローソク足の時間軸。デフォルトは1時間。

取引ロジック

  • ロングエントリー: Close > Close[Period] かつ +DI > -DI で既存のロングシグナルがない場合。ショートポジションをクローズしてロングをオープン。
  • ショートエントリー: Close < Close[Period] かつ -DI > +DI で既存のショートシグナルがない場合。ロングポジションをクローズしてショートをオープン。
  • ポジション保護: StartProtection がテイクプロフィットとストップロスの両パーセントを適用します。

使用上の注意

  • StockSharpの高レベルAPIで設計され、ローソク足をサブスクライブしてADXインジケーターをバインドします。
  • 新しいシグナルが現れると、戦略は自動的に逆方向のポジションをクローズします。
  • 現時点ではPython実装は提供されていません。

免責事項

この例は教育目的のみであり、利益を保証するものではありません。取引には重大なリスクが伴います。ライブ市場に展開する前に、必ず戦略を徹底的にテストしてください。

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>
/// Karacatica strategy uses ADX to determine trend direction and compares
/// current close price with the close price from a specified period ago.
/// It goes long when an uptrend is detected and price is rising, and
/// goes short when a downtrend is detected and price is falling.
/// </summary>
public class KaracaticaStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private AverageDirectionalIndex _adx;
	private readonly Queue<decimal> _closeQueue = new();
	private int _lastSignal;

	/// <summary>
	/// Indicator period used for ADX and price comparison.
	/// </summary>
	public int Period
	{
		get => _period.Value;
		set => _period.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public KaracaticaStrategy()
	{
		_period = Param(nameof(Period), 30)
			.SetGreaterThanZero()
			.SetDisplay("Period", "ADX period and lookback for close comparison", "Indicators")
			.SetOptimize(20, 50, 10);

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_adx = null;
		_closeQueue.Clear();
		_lastSignal = 0;
	}

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

		_adx = new AverageDirectionalIndex { Length = Period };

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

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

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

		_closeQueue.Enqueue(candle.ClosePrice);
		if (_closeQueue.Count > Period + 1)
			_closeQueue.Dequeue();

		if (_closeQueue.Count <= Period)
			return;

		if (!adxValue.IsFormed)
			return;

		var pastClose = _closeQueue.Peek();

		var adxTyped = adxValue as IAverageDirectionalIndexValue;
		if (adxTyped?.Dx is not IDirectionalIndexValue dxVal)
			return;

		var plusDi = dxVal.Plus;
		var minusDi = dxVal.Minus;

		if (plusDi is null || minusDi is null)
			return;

		var buySignal = candle.ClosePrice > pastClose && plusDi > minusDi && _lastSignal != 1;
		var sellSignal = candle.ClosePrice < pastClose && minusDi > plusDi && _lastSignal != -1;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

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