GitHub で見る

Raymond Cloudy Day Strategy

Overview

Raymond Cloudy Day is a breakout-following strategy that reconstructs the trading logic of the original "Raymond Cloudy Day for EA" MQL5 expert advisor. The algorithm derives a set of reference levels from a higher timeframe candle and uses them to detect momentum resumption on the execution timeframe. The StockSharp port keeps the original trading rules while exposing each component as configurable strategy parameters.

Market Data

  • Signal candles – the timeframe on which trades are executed. The strategy subscribes to this series for entry signals and position management.
  • Pivot candles – the higher timeframe used to compute Raymond levels. By default this is a daily candle, reproducing the MQL5 input RayMondTimeframe.

Both subscriptions are automatically registered through GetWorkingSecurities, so the strategy requests the required data streams as soon as it is started.

Raymond Level Calculation

For every finished pivot candle the strategy stores the four core levels defined by the original EA:

[ \beginTradeSS &= \frac{High + Low + Open + Close}{4} \ PivotRange &= High - Low \ ETB &= TradeSS + 0.382 \times PivotRange \ ETS &= TradeSS - 0.382 \times PivotRange \ TPB1 &= TradeSS + 0.618 \times PivotRange \ TPS1 &= TradeSS - 0.618 \times PivotRange \ TPB2 &= TradeSS + PivotRange \ TPS2 &= TradeSS - PivotRange \end]

The StockSharp implementation maintains the most recent snapshot of these values and logs every update, allowing the user to monitor how levels evolve over time.

Entry Logic

Once the Raymond levels are available, the strategy evaluates each finished signal candle:

  1. Long setup – If the candle’s low dips below TPS1 and the close returns above the level, the strategy enters a long position. This mirrors the EA condition Low[1] < TPS1 && Close[1] > TPS1 and captures bullish rejection of the level.
  2. Short setup – If the candle remains fully above TPS1 but closes below it, the strategy opens a short position (matching the original albeit asymmetric rule).

Before placing a new order the algorithm cancels any outstanding orders and, if necessary, closes the opposite position so that only one directional trade remains active.

Risk Management

Raymond Cloudy Day uses symmetric protective offsets measured in ticks:

  • Stop-loss – positioned ProtectiveOffsetTicks below the long entry (or above the short entry).
  • Take-profit – positioned ProtectiveOffsetTicks above the long entry (or below the short entry).

The offsets are multiplied by the instrument’s PriceStep to convert ticks into absolute price distances. Each completed signal candle triggers a check that closes the position when either protective level is hit. When the strategy is flat the internal protection state is reset to avoid stale levels.

Parameters

Name Description Default Notes
TradeVolume Order volume used for every entry. 1 Synchronized with the Volume property on start.
ProtectiveOffsetTicks Distance in ticks for both stop-loss and take-profit. 500 Multiplied by PriceStep to obtain absolute prices.
SignalCandleType Candle type that produces trade signals. 1 hour time frame May be set to any DataType representing candles.
PivotCandleType Higher timeframe for Raymond level calculations. 1 day time frame Matches the RayMondTimeframe input from the MQL EA.

All parameters support optimization ranges and descriptive metadata for StockSharp Designer.

Additional Notes

  • The strategy requires PriceStep to be defined by the connected security. If it is missing, trade entries are skipped and a warning is logged.
  • Chart visualization adds the execution candles together with executed trades. Additional custom drawing can be added if desired.
  • The implementation avoids direct indicator value polling and processes only finished candles, adhering to the project guidelines in AGENTS.md.

Original EA Specifics Preserved

  • Raymond level formulas and multipliers (0.382, 0.618, 1.0).
  • Entry logic based on the first sell take-profit (TPS1).
  • Symmetric 500-point stop-loss and take-profit offsets converted to ticks in the StockSharp environment.

With these components the StockSharp strategy behaves identically to the source EA while providing rich configuration and logging suitable for further research and automation.

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>
/// Raymond Cloudy Day strategy.
/// Computes Raymond levels from a higher timeframe and trades pullbacks around the first sell take-profit level.
/// </summary>
public class RaymondCloudyDayStrategy : Strategy
{
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<int> _protectiveOffsetTicks;
	private readonly StrategyParam<DataType> _signalCandleType;
	private readonly StrategyParam<DataType> _pivotCandleType;

	private decimal? _tradeSessionLevel;
	private decimal? _extendedBuyLevel;
	private decimal? _extendedSellLevel;
	private decimal? _takeProfitBuyLevel;
	private decimal? _takeProfitSellLevel;
	private decimal? _takeProfitBuyLevel2;
	private decimal? _takeProfitSellLevel2;

	private decimal? _entryPrice;
	private decimal? _takePrice;
	private decimal? _stopPrice;

	/// <summary>
	/// Trade volume used for new positions.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set => _tradeVolume.Value = value;
	}

	/// <summary>
	/// Distance in ticks used to build stop-loss and take-profit levels around the entry price.
	/// </summary>
	public int ProtectiveOffsetTicks
	{
		get => _protectiveOffsetTicks.Value;
		set => _protectiveOffsetTicks.Value = value;
	}

	/// <summary>
	/// Candle type that triggers trade signals.
	/// </summary>
	public DataType SignalCandleType
	{
		get => _signalCandleType.Value;
		set => _signalCandleType.Value = value;
	}

	/// <summary>
	/// Higher timeframe candle type used to compute Raymond levels.
	/// </summary>
	public DataType PivotCandleType
	{
		get => _pivotCandleType.Value;
		set => _pivotCandleType.Value = value;
	}

	/// <summary>
	/// Initializes strategy parameters.
	/// </summary>
	public RaymondCloudyDayStrategy()
	{
		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Trade Volume", "Order volume used for entries", "Trading")
			
			.SetOptimize(0.1m, 5m, 0.1m);

		_protectiveOffsetTicks = Param(nameof(ProtectiveOffsetTicks), 500)
			.SetGreaterThanZero()
			.SetDisplay("Protective Offset (ticks)", "Distance in ticks for stop-loss and take-profit", "Risk Management")
			
			.SetOptimize(50, 1000, 50);

		_signalCandleType = Param(nameof(SignalCandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Signal Candle Type", "Candle type used for trade signals", "Data");

		_pivotCandleType = Param(nameof(PivotCandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Pivot Candle Type", "Higher timeframe used to compute Raymond levels", "Data");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, SignalCandleType);
	}

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

		_tradeSessionLevel = null;
		_extendedBuyLevel = null;
		_extendedSellLevel = null;
		_takeProfitBuyLevel = null;
		_takeProfitSellLevel = null;
		_takeProfitBuyLevel2 = null;
		_takeProfitSellLevel2 = null;

		ResetProtection();
	}

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

		Volume = TradeVolume;

		var signalSubscription = SubscribeCandles(SignalCandleType);
		signalSubscription
			.Bind(ProcessBothCandle)
			.Start();

		var priceArea = CreateChartArea();
		if (priceArea != null)
		{
			DrawCandles(priceArea, signalSubscription);
			DrawOwnTrades(priceArea);
		}
	}

	private void ProcessBothCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished)
			return;
		ProcessPivotCandle(candle);
		ProcessSignalCandle(candle);
	}

	private void ProcessPivotCandle(ICandleMessage candle)
	{
		// Skip unfinished candles to keep the level calculation consistent.
		if (candle.State != CandleStates.Finished)
			return;

		var high = candle.HighPrice;
		var low = candle.LowPrice;
		var open = candle.OpenPrice;
		var close = candle.ClosePrice;

		var tradeSession = (high + low + open + close) / 4m;
		var pivotRange = high - low;

		_tradeSessionLevel = tradeSession;
		_extendedBuyLevel = tradeSession + 0.382m * pivotRange;
		_extendedSellLevel = tradeSession - 0.382m * pivotRange;
		_takeProfitBuyLevel = tradeSession + 0.618m * pivotRange;
		_takeProfitSellLevel = tradeSession - 0.618m * pivotRange;
		_takeProfitBuyLevel2 = tradeSession + pivotRange;
		_takeProfitSellLevel2 = tradeSession - pivotRange;

		LogInfo($"Updated Raymond levels from {candle.OpenTime:u}. TradeSS={tradeSession}, ETB={_extendedBuyLevel}, ETS={_extendedSellLevel}, TPB1={_takeProfitBuyLevel}, TPS1={_takeProfitSellLevel}.");
	}

	private void ProcessSignalCandle(ICandleMessage candle)
	{
		// Manage exits first so protective logic reacts even when trading is disabled.
		if (candle.State != CandleStates.Finished)
			return;

		ManageOpenPosition(candle);

		//if (!IsFormedAndOnlineAndAllowTrading())
		//	return;

		if (_takeProfitSellLevel is not decimal triggerLevel)
			return;

		var low = candle.LowPrice;
		var close = candle.ClosePrice;

		// Replicate the original EA condition around the TPS1 level.
		if (Position <= 0 && low < triggerLevel && close > triggerLevel)
		{
			EnterLong(close);
		}
		else if (Position >= 0 && low > triggerLevel && close < triggerLevel)
		{
			EnterShort(close);
		}
	}

	private void EnterLong(decimal closePrice)
	{
		var priceStep = Security?.PriceStep ?? 1m;
		if (priceStep <= 0m)
			priceStep = 1m;

		CancelActiveOrders();

		var volume = TradeVolume + Math.Max(0m, -Position);
		BuyMarket(volume);

		var offset = priceStep * ProtectiveOffsetTicks;
		_entryPrice = closePrice;
		_takePrice = closePrice + offset;
		_stopPrice = closePrice - offset;

		LogInfo($"Opened long position at {closePrice}. TP={_takePrice}, SL={_stopPrice}.");
	}

	private void EnterShort(decimal closePrice)
	{
		var priceStep = Security?.PriceStep ?? 1m;
		if (priceStep <= 0m)
			priceStep = 1m;

		CancelActiveOrders();

		var volume = TradeVolume + Math.Max(0m, Position);
		SellMarket(volume);

		var offset = priceStep * ProtectiveOffsetTicks;
		_entryPrice = closePrice;
		_takePrice = closePrice - offset;
		_stopPrice = closePrice + offset;

		LogInfo($"Opened short position at {closePrice}. TP={_takePrice}, SL={_stopPrice}.");
	}

	private void ManageOpenPosition(ICandleMessage candle)
	{
		if (Position == 0)
		{
			ResetProtection();
			return;
		}

		if (_entryPrice is not decimal entry || _takePrice is not decimal take || _stopPrice is not decimal stop)
			return;

		if (Position > 0)
		{
			// Close the long position if price breaches the protective levels.
			if (candle.LowPrice <= stop)
			{
				SellMarket(Position);
				ResetProtection();
				LogInfo($"Long stop-loss triggered at {stop}.");
				return;
			}

			if (candle.HighPrice >= take)
			{
				SellMarket(Position);
				ResetProtection();
				LogInfo($"Long take-profit triggered at {take}.");
				return;
			}
		}
		else
		{
			var volume = Math.Abs(Position);

			// Close the short position when stop or take-profit is hit.
			if (candle.HighPrice >= stop)
			{
				BuyMarket(volume);
				ResetProtection();
				LogInfo($"Short stop-loss triggered at {stop}.");
				return;
			}

			if (candle.LowPrice <= take)
			{
				BuyMarket(volume);
				ResetProtection();
				LogInfo($"Short take-profit triggered at {take}.");
			}
		}
	}

	private void ResetProtection()
	{
		_entryPrice = null;
		_takePrice = null;
		_stopPrice = null;
	}
}