View on GitHub

Fractal ADX Cloud

This strategy approximates the original MQL Fractal_ADX_Cloud expert by using the Average Directional Index indicator in StockSharp. It works on four‑hour candles and analyses the cross of the +DI and -DI components. When the bullish component (+DI) rises above the bearish one (-DI), the strategy closes any short position and may open a new long. If -DI rises above +DI the logic is mirrored for short trades.

Stop-loss and take-profit protections are applied in absolute price units. Additional parameters allow enabling or disabling opening and closing of positions in each direction.

Details

  • Entry Criteria: Cross of +DI and -DI lines from ADX.
  • Long/Short: Both directions.
  • Exit Criteria: Opposite signal or stop.
  • Stops: Yes, using absolute price distances.
  • Default Values:
    • AdxPeriod = 30
    • StopLoss = 1000m
    • TakeProfit = 2000m
    • BuyPosOpen = true
    • SellPosOpen = true
    • BuyPosClose = true
    • SellPosClose = true
    • CandleType = TimeSpan.FromHours(4)
  • Filters:
    • Category: Trend
    • Direction: Both
    • Indicators: ADX
    • Stops: Yes
    • Complexity: Basic
    • Timeframe: 4h
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: Medium
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>
/// Strategy based on crossing of +DI and -DI from the ADX indicator.
/// </summary>
public class FractalAdxCloudStrategy : Strategy
{
	private readonly StrategyParam<int> _adxPeriod;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _takeProfit;
	private readonly StrategyParam<bool> _buyPosOpen;
	private readonly StrategyParam<bool> _sellPosOpen;
	private readonly StrategyParam<bool> _buyPosClose;
	private readonly StrategyParam<bool> _sellPosClose;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevPlusDi;
	private decimal? _prevMinusDi;

	/// <summary>
	/// ADX period.
	/// </summary>
	public int AdxPeriod
	{
		get => _adxPeriod.Value;
		set => _adxPeriod.Value = value;
	}

	/// <summary>
	/// Stop loss level in price units.
	/// </summary>
	public decimal StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

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

	/// <summary>
	/// Allow opening of long positions.
	/// </summary>
	public bool BuyPosOpen
	{
		get => _buyPosOpen.Value;
		set => _buyPosOpen.Value = value;
	}

	/// <summary>
	/// Allow opening of short positions.
	/// </summary>
	public bool SellPosOpen
	{
		get => _sellPosOpen.Value;
		set => _sellPosOpen.Value = value;
	}

	/// <summary>
	/// Allow closing of long positions on opposite signal.
	/// </summary>
	public bool BuyPosClose
	{
		get => _buyPosClose.Value;
		set => _buyPosClose.Value = value;
	}

	/// <summary>
	/// Allow closing of short positions on opposite signal.
	/// </summary>
	public bool SellPosClose
	{
		get => _sellPosClose.Value;
		set => _sellPosClose.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="FractalAdxCloudStrategy"/>.
	/// </summary>
	public FractalAdxCloudStrategy()
	{
		_adxPeriod = Param(nameof(AdxPeriod), 30)
			.SetRange(5, 60)
			.SetDisplay("ADX Period", "Period for ADX calculation", "Indicators")
			;

		_stopLoss = Param(nameof(StopLoss), 1000m)
			.SetRange(100m, 5000m)
			.SetDisplay("Stop Loss", "Stop loss level", "Risk Management")
			;

		_takeProfit = Param(nameof(TakeProfit), 2000m)
			.SetRange(100m, 10000m)
			.SetDisplay("Take Profit", "Take profit level", "Risk Management")
			;

		_buyPosOpen = Param(nameof(BuyPosOpen), true)
			.SetDisplay("Buy Open", "Allow opening long positions", "Trading");

		_sellPosOpen = Param(nameof(SellPosOpen), true)
			.SetDisplay("Sell Open", "Allow opening short positions", "Trading");

		_buyPosClose = Param(nameof(BuyPosClose), true)
			.SetDisplay("Buy Close", "Allow closing long positions", "Trading");

		_sellPosClose = Param(nameof(SellPosClose), true)
			.SetDisplay("Sell Close", "Allow closing short positions", "Trading");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevPlusDi = null;
		_prevMinusDi = null;
	}

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

		var adx = new AverageDirectionalIndex { Length = AdxPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(adx, ProcessCandle)
			.Start();

		StartProtection(
			takeProfit: new Unit(TakeProfit, UnitTypes.Absolute),
			stopLoss: new Unit(StopLoss, UnitTypes.Absolute),
			useMarketOrders: true);

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var adx = (AverageDirectionalIndexValue)adxValue;
		if (adx.Dx.Plus is not decimal plusDi || adx.Dx.Minus is not decimal minusDi)
			return;

		if (_prevPlusDi is decimal prevPlus && _prevMinusDi is decimal prevMinus)
		{
			if (plusDi > minusDi)
			{
				if (SellPosClose && Position < 0)
					BuyMarket(Math.Abs(Position));

				if (BuyPosOpen && prevPlus <= prevMinus && Position <= 0)
					BuyMarket(Volume + Math.Abs(Position));
			}
			else if (minusDi > plusDi)
			{
				if (BuyPosClose && Position > 0)
					SellMarket(Math.Abs(Position));

				if (SellPosOpen && prevPlus >= prevMinus && Position >= 0)
					SellMarket(Volume + Math.Abs(Position));
			}
		}

		_prevPlusDi = plusDi;
		_prevMinusDi = minusDi;
	}
}