GitHub で見る

Bitcoin イントラデイ季節性戦略

事前に定義された強いイントラデイ時間帯にBitcoinをロングする戦略。

テストでは年平均リターン約45%を示しています。暗号資産市場で最も優れたパフォーマンスを発揮します。

システムは1時間足ローソク足を監視します。選択されたUTC時間中はポートフォリオ価値にサイズが合わせられたロングポジションを維持します。その時間外は現金に移行します。最小USD値を下回る注文はスキップされます。

詳細

  • エントリー条件: 指定されたUTC時間帯にBTCロングを保持。
  • ロング/ショート: ロングのみ。
  • エグジット条件: 指定時間外に退出。
  • ストップ: いいえ。
  • デフォルト値:
    • HoursLong = [0, 1, 2, 3]
    • MinTradeUsd = 200
    • CandleType = TimeSpan.FromHours(1)
  • フィルター:
    • カテゴリ: 季節性
    • 方向: ロングのみ
    • インジケーター: なし
    • ストップ: いいえ
    • 複雑さ: 基本
    • 時間軸: イントラデイ (1h)
    • 季節性: はい
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Strategy that goes long on Bitcoin during predefined strong intraday hours.
/// </summary>
public class BitcoinIntradaySeasonalityStrategy : Strategy
{
	private readonly StrategyParam<int[]> _hoursLong;
	private readonly StrategyParam<decimal> _minUsd;
	private readonly StrategyParam<DataType> _candleType;

	/// <summary>
	/// UTC hours when the strategy holds a long position.
	/// </summary>
	public int[] HoursLong
	{
		get => _hoursLong.Value;
		set => _hoursLong.Value = value;
	}

	/// <summary>
	/// Minimum trade value in USD.
	/// </summary>
	public decimal MinTradeUsd
	{
		get => _minUsd.Value;
		set => _minUsd.Value = value;
	}

	/// <summary>
	/// The type of candles to use for strategy calculation.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	private readonly Dictionary<Security, decimal> _latestPrices = [];

	/// <summary>
	/// Initializes a new instance of <see cref="BitcoinIntradaySeasonalityStrategy"/>.
	/// </summary>
	public BitcoinIntradaySeasonalityStrategy()
	{
		// Hours to stay long (UTC).
		_hoursLong = Param(nameof(HoursLong), new[] { 0, 1, 2, 3 })
			.SetDisplay("Long Hours", "UTC hours when the strategy stays long", "General");

		// Minimum trade size.
		_minUsd = Param(nameof(MinTradeUsd), 200m)
			.SetGreaterThanZero()
			.SetDisplay("Min Trade USD", "Minimum order value in USD", "Trading");

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

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		if (Security == null)
			throw new InvalidOperationException("BTC security not set.");

		return [(Security, CandleType)];
	}

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

		_latestPrices.Clear();
	}

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

		if (HoursLong == null || HoursLong.Length == 0)
			throw new InvalidOperationException("HoursLong cannot be empty.");

		if (Security == null)
			throw new InvalidOperationException("BTC security not set.");

		SubscribeCandles(CandleType, true, Security)
			.Bind(c => ProcessCandle(c, Security))
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, Security security)
	{
		// Skip unfinished candles
		if (candle.State != CandleStates.Finished)
			return;

		// Store the latest closing price for this security
		_latestPrices[security] = candle.ClosePrice;

		OnHourClose(candle);
	}

	private void OnHourClose(ICandleMessage c)
	{
		var hour = c.OpenTime.Hour; // assume server UTC
		var inSeason = c.OpenTime.DayOfWeek == DayOfWeek.Monday && c.OpenTime.Day <= 7 && HoursLong.Contains(hour);

		var portfolioValue = Portfolio.CurrentValue ?? 0m;
		var price = GetLatestPrice(Security);

		var tgt = inSeason && price > 0 ? portfolioValue / price : 0m;
		var diff = tgt - PositionBy(Security);

		if (price <= 0 || Math.Abs(diff) * price < MinTradeUsd)
			return;

		RegisterOrder(new Order
		{
			Security = Security,
			Portfolio = Portfolio,
			Side = diff > 0 ? Sides.Buy : Sides.Sell,
			Volume = Math.Abs(diff),
			Type = OrderTypes.Market,
			Comment = "BTCSeason",
		});
	}

	private decimal GetLatestPrice(Security security)
	{
		return _latestPrices.TryGetValue(security, out var price) ? price : 0m;
	}

	private decimal PositionBy(Security s) => GetPositionValue(s, Portfolio) ?? 0;
}