Ver en GitHub

Day Trading Indicator Fusion Strategy

The strategy trades 5-minute candles using Parabolic SAR, MACD (12,26,9), Stochastic Oscillator (5,3,3) and Momentum (14). It requires all indicators to align before entering a position.

  • Long entry: SAR below price with previous SAR above current, Momentum < 100, MACD line below signal line, Stochastic %K < 35.
  • Short entry: SAR above price with previous SAR below current, Momentum > 100, MACD line above signal line, Stochastic %K > 60.

Positions are closed when the opposite conditions occur. Risk management uses a trailing stop and optional take profit.

Parameters

  • Volume – order volume.
  • Take Profit – target profit in points.
  • Trailing Stop – trailing stop distance in points.
  • Candle Type – candle subscription type (default 5-minute).
using System;
using System.Linq;
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>
/// Day trading strategy using Parabolic SAR, MACD, Stochastic, and Momentum indicators.
/// Enters long when multiple indicators confirm upward move and short when they confirm downward move.
/// Applies trailing stop and take profit.
/// </summary>
public class DayTradingIndicatorFusionStrategy : Strategy
{
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<decimal> _trailingStop;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSar;

	/// <summary>
	/// Order volume.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set => _tradeVolume.Value = value;
	}

	/// <summary>
	/// Take profit in points.
	/// </summary>
	public decimal TakeProfit
	{
		get => _takeProfit.Value;
		set => _takeProfit.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in points.
	/// </summary>
	public decimal TrailingStop
	{
		get => _trailingStop.Value;
		set => _trailingStop.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of <see cref="DayTradingIndicatorFusionStrategy"/>.
	/// </summary>
	public DayTradingIndicatorFusionStrategy()
	{
		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Volume", "Order volume", "General");

		_takeProfit = Param(nameof(TakeProfit), 50m)
			.SetDisplay("Take Profit", "Take profit in points", "Risk");

		_trailingStop = Param(nameof(TrailingStop), 25m)
			.SetDisplay("Trailing Stop", "Trailing stop in points", "Risk");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSar = 0m;
	}

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

		var macd = new MovingAverageConvergenceDivergenceSignal();
		macd.Macd.ShortMa.Length = 12;
		macd.Macd.LongMa.Length = 26;
		macd.SignalMa.Length = 9;

		var stochastic = new StochasticOscillator();
		stochastic.K.Length = 5;
		stochastic.D.Length = 3;

		var parabolicSar = new ParabolicSar
		{
			Acceleration = 0.02m,
			AccelerationMax = 0.2m
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(macd, stochastic, parabolicSar, ProcessCandle)
			.Start();

		var pip = Security?.PriceStep ?? 1m;
		StartProtection(
			takeProfit: TakeProfit > 0 ? new Unit(TakeProfit * pip, UnitTypes.Absolute) : default,
			stopLoss: TrailingStop > 0 ? new Unit(TrailingStop * pip, UnitTypes.Absolute) : default,
			isStopTrailing: TrailingStop > 0
		);

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

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue macdValue, IIndicatorValue stochValue, IIndicatorValue sarValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var macdTyped = (MovingAverageConvergenceDivergenceSignalValue)macdValue;
		if (macdTyped.Macd is not decimal macd || macdTyped.Signal is not decimal macdSignal)
			return;

		var stochTyped = (StochasticOscillatorValue)stochValue;
		if (stochTyped.K is not decimal stochK || stochTyped.D is not decimal stochD)
			return;

		var sar = sarValue.ToDecimal();

		var isBuying = sar <= candle.ClosePrice && _prevSar > sar && macd < macdSignal && stochK < 35m;
		var isSelling = sar >= candle.ClosePrice && _prevSar < sar && macd > macdSignal && stochK > 60m;

		_prevSar = sar;

		if (isBuying && Position <= 0)
		{
			BuyMarket();
		}
		else if (isSelling && Position >= 0)
		{
			SellMarket();
		}
	}
}