在 GitHub 上查看

周一开盘策略

该策略在一周开始时买入,并在周二收盘时平仓,限定在指定年份范围内。

细节

  • 入场条件
    • 多头:周一开仓做多。
  • 多空方向:仅多头。
  • 出场条件
    • 周二平多头仓位。
  • 止损:无。
  • 默认值
    • StartYear = 2023。
    • EndYear = 2025。
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()。
  • 筛选
    • 类型: 季节性
    • 方向: 多头
    • 指标: 无
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日线
    • 季节性: 是
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 低
using System;
using System.Linq;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Opens a long trade at the start of the week and exits on Tuesday.
/// </summary>
public class MondayOpenStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private bool _tradeOpened;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MondayOpenStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var day = candle.OpenTime.DayOfWeek;

		if (day == DayOfWeek.Monday && !_tradeOpened && Position <= 0)
		{
			BuyMarket();
			_tradeOpened = true;
		}
		else if (day == DayOfWeek.Tuesday && _tradeOpened && Position > 0)
		{
			SellMarket();
			_tradeOpened = false;
		}
		else if (day != DayOfWeek.Monday && day != DayOfWeek.Tuesday)
		{
			_tradeOpened = false;
		}
	}
}