在 GitHub 上查看

NY Opening Range Breakout - MA Stop 策略

该策略在纽约时间 9:30-9:45 的开盘区间突破后入场,可选择使用均线作为止盈/过滤。若上一根K线突破区间且未超过截止时间,并且符合均线过滤,则在下一根K线上入场。

详情

  • 入场条件
    • 前一根K线收盘价突破区间高点(做多)或低点(做空),且在截止时间之前。
    • 当前K线为突破后的第一根,并在启用时满足均线过滤。
  • 多空方向:通过 TradeDirection 配置。
  • 出场条件
    • 止损放在开盘区间的另一侧。
    • 止盈根据 TakeProfitType:固定风险回报、均线反向或两者同时。
  • 止损:有,位于区间边界。
  • 默认值
    • CutoffHour = 12
    • CutoffMinute = 0
    • TradeDirection = LongOnly
    • TakeProfitType = FixedRiskReward
    • TpRatio = 2.5
    • MaType = SMA
    • MaLength = 100
    • CandleType = TimeSpan.FromMinutes(1)
  • 过滤器
    • 分类:Breakout
    • 方向:可配置
    • 指标:移动平均线
    • 止损:有
    • 复杂度:中等
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
using System;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

public class NyOpeningRangeBreakoutMaStopStrategy : Strategy
{
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _dayHigh;
	private decimal _dayLow;
	private DateTime _currentDay;
	private bool _rangeSet;
	private bool _tradedToday;

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

	public NyOpeningRangeBreakoutMaStopStrategy()
	{
		_maLength = Param(nameof(MaLength), 50).SetGreaterThanZero();
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_dayHigh = 0m;
		_dayLow = 0m;
		_currentDay = default;
		_rangeSet = false;
		_tradedToday = false;
	}

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

		_dayHigh = 0m;
		_dayLow = 0m;
		_currentDay = default;
		_rangeSet = false;
		_tradedToday = false;

		var ma = new SimpleMovingAverage { Length = MaLength };

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

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);
			DrawIndicator(area, ma);
		}
	}

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

		var day = candle.OpenTime.Date;

		// New day
		if (day != _currentDay)
		{
			_currentDay = day;
			if (Position > 0) SellMarket();
			else if (Position < 0) BuyMarket();
			_dayHigh = candle.HighPrice;
			_dayLow = candle.LowPrice;
			_rangeSet = true;
			_tradedToday = false;
			return;
		}

		if (!_rangeSet || _tradedToday)
			return;

		// Exit on MA cross
		if (Position > 0 && candle.ClosePrice < maValue)
		{
			SellMarket();
			_tradedToday = true;
			return;
		}

		if (Position < 0 && candle.ClosePrice > maValue)
		{
			BuyMarket();
			_tradedToday = true;
			return;
		}

		// Entry: breakout above first candle high
		if (Position <= 0 && candle.ClosePrice > _dayHigh && candle.ClosePrice > maValue)
		{
			BuyMarket();
			_tradedToday = true;
		}
		else if (Position >= 0 && candle.ClosePrice < _dayLow && candle.ClosePrice < maValue)
		{
			SellMarket();
			_tradedToday = true;
		}
	}
}