GitHub で見る

Snowieso戦略

この戦略は、トレンド方向を確認するために、高速・低速の線形加重移動平均 (LWMA)MACDカウフマン適応型移動平均 (KAMA) を組み合わせています。

仕組み

  1. 選択した時間軸のローソク足を購読する。
  2. Fast LWMA、Slow LWMA、MACDおよびKAMAの値を計算する。
  3. ロングエントリー: 高速LWMAが低速LWMAを上抜け、MACDヒストグラムがプラス、KAMAが上昇しているときに発生。
  4. ショートエントリー: 高速LWMAが低速LWMAを下抜け、MACDヒストグラムがマイナス、KAMAが下落しているときに発生。
  5. StartProtectionを使用して固定のストップロスとテイクプロフィットを適用。

この戦略は新規ポジションを開く前に反対方向のポジションを決済し、インジケーターと取引をチャート上に視覚化します。

パラメーター

  • FastLength – 高速LWMAの期間。
  • SlowLength – 低速LWMAの期間。
  • MacdFast, MacdSlow, MacdSignal – MACD設定。
  • KamaLength – KAMAのルックバック期間。
  • StopLossPoints – 価格ポイントでの絶対ストップロス。
  • TakeProfitPoints – 価格ポイントでの絶対テイクプロフィット。
  • CandleType – 処理するローソク足の時間軸。

使用方法

選択した銘柄に戦略をデプロイしてください。アルゴリズムは自動的にローソク足を購読し、インジケーターシグナルに基づいてポジションを管理します。データバインディングと注文執行にはハイレベルAPIが使用されます。

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>
/// Strategy based on WMA crossover with KAMA confirmation.
/// </summary>
public class SnowiesoStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _kamaLength;
	private readonly StrategyParam<DataType> _candleType;

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

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public int KamaLength { get => _kamaLength.Value; set => _kamaLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SnowiesoStrategy()
	{
		_fastLength = Param(nameof(FastLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast WMA", "Fast WMA period", "Indicators");

		_slowLength = Param(nameof(SlowLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("Slow WMA", "Slow WMA period", "Indicators");

		_kamaLength = Param(nameof(KamaLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("KAMA Length", "KAMA period", "Indicators");

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

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_prevKama = 0;
		_hasPrev = false;
	}

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

		var fast = new WeightedMovingAverage { Length = FastLength };
		var slow = new WeightedMovingAverage { Length = SlowLength };
		var kama = new KaufmanAdaptiveMovingAverage { Length = KamaLength };

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

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue, decimal kamaValue)
	{
		if (candle.State != CandleStates.Finished) return;

		if (!_hasPrev)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			_prevKama = kamaValue;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastValue > slowValue;
		var crossDown = _prevFast >= _prevSlow && fastValue < slowValue;
		var kamaRising = kamaValue > _prevKama;
		var kamaFalling = kamaValue < _prevKama;

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

		_prevFast = fastValue;
		_prevSlow = slowValue;
		_prevKama = kamaValue;
	}
}