GitHub で見る

高度なマルチ季節性戦略

事前定義された季節的期間に基づいて取引します。個別のエントリー日、保有日数、方向を持つ最大4つの期間を設定できます。

詳細

  • エントリー条件: カレンダー日付が設定済み期間と一致する
  • ロング/ショート: 両方
  • エグジット条件: 保有日数に到達
  • ストップ: なし
  • デフォルト値:
    • CandleType = 1 day
  • フィルター:
    • カテゴリ: 季節性
    • 方向: 両方
    • インジケーター: なし
    • ストップ: いいえ
    • 複雑さ: 初心者
    • 時間軸: 日足
    • 季節性: はい
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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 that trades during predefined seasonal periods.
/// Enters periodically and holds for a configured number of bars.
/// </summary>
public class AdvancedMultiSeasonalityStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _holdingBars;
	private readonly StrategyParam<int> _cooldownBars;

	private ExponentialMovingAverage _ema1;
	private ExponentialMovingAverage _ema2;
	private int _barIndex;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int HoldingBars { get => _holdingBars.Value; set => _holdingBars.Value = value; }
	public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }

	public AdvancedMultiSeasonalityStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
		_holdingBars = Param(nameof(HoldingBars), 100)
			.SetGreaterThanZero()
			.SetDisplay("Holding Bars", "Bars to hold position", "General");
		_cooldownBars = Param(nameof(CooldownBars), 50)
			.SetGreaterThanZero()
			.SetDisplay("Cooldown Bars", "Bars between trades", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_ema1 = null;
		_ema2 = null;
		_barIndex = 0;
	}

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

		_ema1 = new ExponentialMovingAverage { Length = 10 };
		_ema2 = new ExponentialMovingAverage { Length = 30 };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(_ema1, _ema2, ProcessCandle).Start();
	}

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

		_barIndex++;

		if (_barIndex > HoldingBars && Position > 0)
		{
			SellMarket();
			return;
		}

		if (Position == 0 && _barIndex > CooldownBars)
		{
			BuyMarket();
			_barIndex = 0;
		}
	}
}