Открыть на GitHub

Стратегия Overnight Gap

Overnight Gap используется при сильных гэпах на открытии по сравнению с предыдущим закрытием, вызванных новостями или послеторговой активностью. Крупные гэпы часто частично закрываются, когда участники осмысливают движение.

Тестирование показывает среднегодичную доходность около 124%. Стратегию лучше запускать на рынке Форекс.

Стратегия торгует против чрезмерных разрывов, входя вскоре после открытия в противоположную сторону и закрываясь до конца сессии.

Стопы ставятся на процентную величину за пределами экстремумов гэпа, чтобы управлять риском, если движение продолжится.

Детали

  • Критерий входа: сигнал индикатора
  • Длинная/короткая сторона: обе
  • Критерий выхода: стоп-лосс или противоположный сигнал
  • Стопы: да, процентные
  • Значения по умолчанию:
    • CandleType = 15 минут
    • StopLoss = 2%
  • Фильтры:
    • Категория: Gap
    • Направление: обе
    • Индикаторы: Gap
    • Стопы: да
    • Сложность: средняя
    • Таймфрейм: внутридневной
    • Сезонность: нет
    • Нейросети: нет
    • Дивергенция: нет
    • Уровень риска: средний
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>
/// Implementation of Overnight Gap trading strategy.
/// Trades on gaps between current open and previous close, using MA trend filter.
/// </summary>
public class OvernightGapStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private SimpleMovingAverage _ma;

	private decimal _prevClose;
	private int _cooldown;

	/// <summary>
	/// Moving average period.
	/// </summary>
	public int MaPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	/// <summary>
	/// Candle type for strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Cooldown bars between trades.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="OvernightGapStrategy"/>.
	/// </summary>
	public OvernightGapStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Moving average period", "Strategy");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for strategy", "Strategy");

		_cooldownBars = Param(nameof(CooldownBars), 30)
			.SetDisplay("Cooldown Bars", "Bars between trades", "General")
			.SetRange(5, 500);
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_ma = default;
		_prevClose = 0;
		_cooldown = 0;
	}

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

		_ma = new SimpleMovingAverage { Length = MaPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var close = candle.ClosePrice;
		var open = candle.OpenPrice;

		if (_prevClose == 0)
		{
			_prevClose = close;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevClose = close;
			return;
		}

		// Calculate gap
		var gap = open - _prevClose;

		// Gap up + above MA = Buy
		if (gap > 0 && open > maValue && Position == 0)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		// Gap down + below MA = Sell short
		else if (gap < 0 && open < maValue && Position == 0)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}

		// Exit: gap fill or MA cross
		if (Position > 0 && close < maValue)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position < 0 && close > maValue)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}

		_prevClose = close;
	}
}