GitHub で見る

オプション満期週戦略

このPython戦略は、オプション満期週のみに株式ETFを買い持ちします。毎月第3金曜日の前の月曜日から ETF を買い、金曜日の引け値でポジションをクローズします。このアイデアは、満期週に頻繁に観察される短期的な強さを利用します。

この期間外はポートフォリオを現金で保持します。日足ローソク足を使用し、取引は1日1回の成行注文として送信されます。

詳細

  • 銘柄: 単一の株式ETF。
  • シグナル: 第3金曜日に終わる週のカレンダールール。
  • 保有期間: 満期週の月曜日始値から金曜日終値まで。
  • ポジショニング: 期間中は完全投資、それ以外はフラット。
  • リスク管理: 注文価値が MinTradeUsd を下回る場合は取引をスキップ。
// OptionExpirationWeekStrategy.cs — candle-triggered
// Long ETF only during option‑expiration week (ending 3rd Friday).
// Trigger: daily candle close.
// Date: 2 Aug 2025

using System;
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>
/// Goes long the specified ETF only during option‑expiration week.
/// </summary>
public class OptionExpirationWeekStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	/// <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 = [];
	private int _enteredMonthKey;
	private int _exitedMonthKey;

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

	public override IEnumerable<(Security, DataType)> GetWorkingSecurities()
	{
		if (Security == null)
			throw new InvalidOperationException("ETF not set.");

		return [(Security, CandleType)];
	}

	
	protected override void OnReseted()
	{
		base.OnReseted();

		_latestPrices.Clear();
		_enteredMonthKey = 0;
		_exitedMonthKey = 0;
	}

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

		if (Security == null)
			throw new InvalidOperationException("ETF cannot be null.");

		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;

		OnDaily(candle.OpenTime.Date);
	}

	private void OnDaily(DateTime d)
	{
		var monthKey = (d.Year * 100) + d.Month;
		bool inExp = IsOptionExpWeek(d);

		if (inExp && Position == 0 && _enteredMonthKey != monthKey)
		{
			BuyMarket();
			_enteredMonthKey = monthKey;
			_exitedMonthKey = 0;
		}
		else if (!inExp && Position > 0 && _enteredMonthKey == monthKey && _exitedMonthKey != monthKey)
		{
			SellMarket(Position);
			_exitedMonthKey = monthKey;
		}
	}

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

	private bool IsOptionExpWeek(DateTime d)
	{
		// find third Friday
		var third = new DateTime(d.Year, d.Month, 1);
		while (third.DayOfWeek != DayOfWeek.Friday)
			third = third.AddDays(1);
		third = third.AddDays(14);
		return d >= third.AddDays(-4) && d <= third;
	}
}