Ver en GitHub

Ichimoku Cloud Retrace Strategy

This strategy is a StockSharp port of the MetaTrader expert "ichimok2005". It looks for pullbacks into the Ichimoku cloud and trades in the direction of the prevailing kumo slope. Signals are evaluated only on completed candles.

Overview

  • Works on any instrument and timeframe that provides candle data.
  • Uses the standard Ichimoku settings (9/26/52) by default but they are fully configurable.
  • Trades both long and short. Position size is defined by the strategy Volume property.
  • Optional stop-loss and take-profit can be configured in absolute price units.

Indicators and Parameters

  • Ichimoku: Tenkan, Kijun, and Senkou Span B lengths are exposed via parameters.
  • Candle Type: choose any aggregated candle type supported by the connection (default: 1 hour time frame).
  • Stop Loss Offset: optional distance below/above the entry price that forces an exit. Set to 0 to disable.
  • Take Profit Offset: optional profit target distance from the entry price. Set to 0 to disable.

Entry Rules

Long Setup

  1. Senkou Span A is above Senkou Span B, signalling a bullish cloud.
  2. The current finished candle is bullish (Close > Open).
  3. The candle closes inside the cloud (Close is between the two spans).
  4. When all conditions align and the strategy is flat or short, it sends a market buy order sized to both close any short exposure and open a new long.

Short Setup

  1. Senkou Span B is above Senkou Span A, signalling a bearish cloud.
  2. The current finished candle is bearish (Open > Close).
  3. The candle closes inside the cloud (Close is between the two spans).
  4. When the conditions align and the strategy is flat or long, it sends a market sell order sized to both close any long exposure and open a new short.

Exit Rules

  • Opposite signals automatically reverse the position by combining the close and the new entry into a single market order.
  • When enabled, Stop Loss Offset exits at EntryPrice - Offset for longs and EntryPrice + Offset for shorts, using the candle close price.
  • When enabled, Take Profit Offset exits at EntryPrice + Offset for longs and EntryPrice - Offset for shorts.
  • Manual flatting (closing the strategy) also resets the internal entry price tracker.

Risk Management Notes

  • Offsets are expressed in absolute price units. Convert pip or tick distances to price before configuring them.
  • Because the strategy uses candle close prices for risk checks, consider tighter offsets for lower time frames.
  • No trailing or partial exits are implemented; the strategy always exits the whole position.

Additional Implementation Details

  • The strategy subscribes to candles through the high-level API and binds the Ichimoku indicator with BindEx.
  • Only finished candles trigger logic; intermediate updates are ignored.
  • A chart area is created automatically (when available) to display price, the Ichimoku cloud, and the executed trades.
  • ManageRisk is executed before looking for new entries so that protective exits have priority.
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>
/// Ichimoku cloud retrace strategy.
/// Takes trades when price pulls back inside the cloud in the direction of the kumo slope.
/// Uses optional fixed offsets for stop-loss and take-profit management.
/// </summary>
public class IchimokuCloudRetraceStrategy : Strategy
{
	private readonly StrategyParam<int> _tenkanPeriod;
	private readonly StrategyParam<int> _kijunPeriod;
	private readonly StrategyParam<int> _senkouSpanBPeriod;
	private readonly StrategyParam<decimal> _stopLossOffset;
	private readonly StrategyParam<decimal> _takeProfitOffset;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;

	/// <summary>
	/// Tenkan-sen period.
	/// </summary>
	public int TenkanPeriod
	{
		get => _tenkanPeriod.Value;
		set => _tenkanPeriod.Value = value;
	}

	/// <summary>
	/// Kijun-sen period.
	/// </summary>
	public int KijunPeriod
	{
		get => _kijunPeriod.Value;
		set => _kijunPeriod.Value = value;
	}

	/// <summary>
	/// Senkou Span B period.
	/// </summary>
	public int SenkouSpanBPeriod
	{
		get => _senkouSpanBPeriod.Value;
		set => _senkouSpanBPeriod.Value = value;
	}

	/// <summary>
	/// Stop-loss offset in price units. Set to zero to disable.
	/// </summary>
	public decimal StopLossOffset
	{
		get => _stopLossOffset.Value;
		set => _stopLossOffset.Value = value;
	}

	/// <summary>
	/// Take-profit offset in price units. Set to zero to disable.
	/// </summary>
	public decimal TakeProfitOffset
	{
		get => _takeProfitOffset.Value;
		set => _takeProfitOffset.Value = value;
	}

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

	/// <summary>
	/// Initializes strategy parameters.
	/// </summary>
	public IchimokuCloudRetraceStrategy()
	{
		_tenkanPeriod = Param(nameof(TenkanPeriod), 9)
			.SetGreaterThanZero()
			.SetDisplay("Tenkan Period", "Tenkan-sen length", "Ichimoku Settings")
			
			.SetOptimize(5, 15, 1);

		_kijunPeriod = Param(nameof(KijunPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Kijun Period", "Kijun-sen length", "Ichimoku Settings")
			
			.SetOptimize(20, 40, 2);

		_senkouSpanBPeriod = Param(nameof(SenkouSpanBPeriod), 52)
			.SetGreaterThanZero()
			.SetDisplay("Senkou Span B Period", "Senkou Span B length", "Ichimoku Settings")
			
			.SetOptimize(40, 70, 5);

		_stopLossOffset = Param(nameof(StopLossOffset), 0m)
			.SetDisplay("Stop Loss Offset", "Distance from entry for stop-loss (price units)", "Risk Management");

		_takeProfitOffset = Param(nameof(TakeProfitOffset), 0m)
			.SetDisplay("Take Profit Offset", "Distance from entry for take-profit (price units)", "Risk Management");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for analysis", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		// Reset internal state values.
		_entryPrice = 0m;
	}

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

		// Prepare Ichimoku indicator with user-defined lengths.
		var ichimoku = new Ichimoku
		{
			Tenkan = { Length = TenkanPeriod },
			Kijun = { Length = KijunPeriod },
			SenkouB = { Length = SenkouSpanBPeriod }
		};

		// Subscribe to candle data and bind the indicator for processing.
		var subscription = SubscribeCandles(CandleType);

		subscription
			.BindEx(ichimoku, ProcessCandle)
			.Start();

		// Draw helper visuals if a chart is available.
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, ichimoku);
			DrawOwnTrades(area);
		}
	}

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

		if (!ichimokuValue.IsFinal)
			return;

		// Manage open positions using the latest close before looking for new entries.
		ManageRisk(candle);

		if (Position == 0)
			_entryPrice = 0m;

		var ichimoku = (IchimokuValue)ichimokuValue;

		if (ichimoku.SenkouA is not decimal senkouA ||
			ichimoku.SenkouB is not decimal senkouB)
			return;

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

		var lowerSpan = Math.Min(senkouA, senkouB);
		var upperSpan = Math.Max(senkouA, senkouB);

		var priceInsideCloud = close > lowerSpan && close < upperSpan;

		var bullishCloud = senkouA > senkouB;
		var bearishCloud = senkouB > senkouA;

		var shouldBuy = bullishCloud && close > open && priceInsideCloud;
		var shouldSell = bearishCloud && open > close && priceInsideCloud;

		if (shouldBuy && Position <= 0)
		{
			// Combine reversal and new entry volume in a single market order.
			var volume = Volume + (Position < 0 ? Math.Abs(Position) : 0m);

			if (volume > 0)
			{
				_entryPrice = close;
				BuyMarket(volume);
			}
		}
		else if (shouldSell && Position >= 0)
		{
			// Combine reversal and new entry volume in a single market order.
			var volume = Volume + (Position > 0 ? Math.Abs(Position) : 0m);

			if (volume > 0)
			{
				_entryPrice = close;
				SellMarket(volume);
			}
		}
	}

	private void ManageRisk(ICandleMessage candle)
	{
		if (_entryPrice == 0m)
			return;

		var close = candle.ClosePrice;

		if (Position > 0)
		{
			if (StopLossOffset > 0m && close <= _entryPrice - StopLossOffset)
			{
				var volumeToClose = Math.Abs(Position);

				if (volumeToClose > 0m)
				{
					SellMarket(volumeToClose);
					_entryPrice = 0m;
					return;
				}
			}

			if (TakeProfitOffset > 0m && close >= _entryPrice + TakeProfitOffset)
			{
				var volumeToClose = Math.Abs(Position);

				if (volumeToClose > 0m)
				{
					SellMarket(volumeToClose);
					_entryPrice = 0m;
				}
			}
		}
		else if (Position < 0)
		{
			if (StopLossOffset > 0m && close >= _entryPrice + StopLossOffset)
			{
				var volumeToClose = Math.Abs(Position);

				if (volumeToClose > 0m)
				{
					BuyMarket(volumeToClose);
					_entryPrice = 0m;
					return;
				}
			}

			if (TakeProfitOffset > 0m && close <= _entryPrice - TakeProfitOffset)
			{
				var volumeToClose = Math.Abs(Position);

				if (volumeToClose > 0m)
				{
					BuyMarket(volumeToClose);
					_entryPrice = 0m;
				}
			}
		}
	}
}