GitHub で見る

非季節調整ATRと予測戦略

概要

この戦略は、直近ローソク足の平均取引レンジを分析し、線形トレンド回帰を使用して次のレンジを予測します。取引は行わず、手動による意思決定に使用できる統計情報を表示します。

パラメーター

  • SampleSize – 計算に使用する直近ローソク足の本数。
  • DesiredRange – 信頼区間の推定に使用する目標レンジ。
  • CandleType – 分析するローソク足シリーズ。

インジケーター

  • SimpleMovingAverage – 平均レンジの計算に使用。
  • StandardDeviation – レンジのボラティリティを測定。
  • 線形回帰(カスタム) – 次のレンジとMAPEを予測。

動作

完了した各ローソク足に対して、戦略は以下を実行します:

  1. レンジ(高値マイナス安値)を計算し、平均と標準偏差を更新する。
  2. 目的のレンジに対する信頼区間を推定する。
  3. レンジの線形トレンドを構築し、次を予測する。
  4. 予測の平均絶対パーセント誤差(MAPE)を評価する。

値は戦略の出力にログ記録され、チャートで視覚化できます。

注意事項

  • この戦略は情報提供を目的としており、注文を実行しません。
  • レンジは価格単位で測定されます。使用する銘柄に合わせてDesiredRangeパラメーターを調整してください。
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>
/// EMA crossover strategy with volatility awareness.
/// </summary>
public class UnseasonalisedAtrForecastStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

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

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public UnseasonalisedAtrForecastStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Parameters");
		_slowPeriod = Param(nameof(SlowPeriod), 36)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Parameters");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

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

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

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

	private void ProcessCandle(ICandleMessage candle, decimal fastVal, decimal slowVal)
	{
		if (candle.State != CandleStates.Finished) return;

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

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

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}