View on GitHub

News Trading EA Strategy

Time-based straddle strategy designed for trading around economic news releases. At a scheduled time the strategy places symmetric buy stop and sell stop orders at a fixed distance from the current price. Orders are updated every candle during the activation window to follow market price. If a position is opened the opposite pending order is canceled and optional take-profit and stop-loss levels manage exits.

Details

  • Entry Criteria:
    • During the straddle window, place buy stop at close + Distance * step and sell stop at close - Distance * step.
  • Long/Short: Both
  • Exit Criteria: Opposite stop, take-profit/stop-loss or order expiration
  • Stops: Fixed stop loss and take profit
  • Default Values:
    • StartDateTime = DateTime.Now
    • StartStraddle = 0
    • StopStraddle = 15
    • Volume = 0.01m
    • Distance = 55m
    • TakeProfit = 30m
    • StopLoss = 30m
    • Expiration = 20
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame()
  • Filters:
    • Category: News
    • Direction: Both
    • Indicators: None
    • Stops: Yes
    • Complexity: Beginner
    • Timeframe: Event
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: High
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>
/// Volatility breakout strategy using StdDev for big candle detection.
/// </summary>
public class NewsTradingEaStrategy : Strategy
{
	private readonly StrategyParam<int> _stdDevPeriod;
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int StdDevPeriod { get => _stdDevPeriod.Value; set => _stdDevPeriod.Value = value; }
	public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public NewsTradingEaStrategy()
	{
		_stdDevPeriod = Param(nameof(StdDevPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("StdDev Period", "Volatility period", "Indicators");

		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "EMA period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = 12 };
		var slow = new ExponentialMovingAverage { Length = EmaPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}