Ver en GitHub

RndTrade Strategy

Conversion of the original MQL4 "RndTrade" expert advisor into a StockSharp high-level strategy that performs fully random market entries and exits them after a fixed holding period.

Core Logic

  1. Subscribe to the configured candle type (1-minute candles by default) and wait for completed bars.
  2. Whenever the strategy is flat, generate a random number. A value above 0.5 triggers a market buy, otherwise a market sell, both using the configured trade volume.
  3. Record the candle time of the entry and keep the position open for the selected holding duration (four hours by default).
  4. After the holding timer elapses, close the entire position with the corresponding market order.

Parameters

Name Description Default
CandleType Data type of candles that fire the random decision logic. 1 minute candles
TradeVolume Volume used for every random market order. 1
HoldDuration Time span to keep any opened random position active before closing it. 4 hours

Additional Notes

  • The random generator is reseeded automatically when the strategy starts to mimic the MQL4 behavior of using the local time as a seed.
  • Only market orders are used, reflecting the original expert advisor which immediately executed trades without pending orders.
  • No additional indicators or historical buffers are required; the strategy only relies on the incoming candle timestamps and the internal timer.
using System;



using StockSharp.Algo.Indicators;

using StockSharp.Algo.Strategies;

using StockSharp.BusinessEntities;

using StockSharp.Messages;



namespace StockSharp.Samples.Strategies;



public class RndTradeRandomHoldStrategy : Strategy

{

	private readonly StrategyParam<int> _emaPeriod;

	private readonly StrategyParam<int> _momentumPeriod;

	private readonly StrategyParam<DataType> _candleType;



	private decimal _prevMom; private bool _hasPrev;

	private int _cooldown;



	public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }

	public int MomentumPeriod { get => _momentumPeriod.Value; set => _momentumPeriod.Value = value; }

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }



	public RndTradeRandomHoldStrategy()

	{

		_emaPeriod = Param(nameof(EmaPeriod), 14).SetDisplay("EMA Period", "EMA filter", "Indicators");

		_momentumPeriod = Param(nameof(MomentumPeriod), 10).SetDisplay("Momentum", "Momentum period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame()).SetDisplay("Candle Type", "Candle timeframe", "General");

	}



	/// <inheritdoc />

	protected override void OnReseted()

	{

		base.OnReseted();

		_prevMom = default;

		_hasPrev = default;

		_cooldown = default;

	}



	/// <inheritdoc />

	protected override void OnStarted2(DateTime time)

	{

		base.OnStarted2(time);

		_hasPrev = false;

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };

		var mom = new Momentum { Length = MomentumPeriod };

		var subscription = SubscribeCandles(CandleType);

		subscription.Bind(ema, mom, ProcessCandle).Start();

	}



	private void ProcessCandle(ICandleMessage candle, decimal ema, decimal mom)

	{

		if (candle.State != CandleStates.Finished) return;

		if (!IsFormedAndOnlineAndAllowTrading()) return;

		var close = candle.ClosePrice;

		if (!_hasPrev) { _prevMom = mom; _hasPrev = true; return; }

		if (_cooldown > 0)

		{

			_cooldown--;

			_prevMom = mom;

			return;

		}



		if (close > ema && _prevMom <= 0 && mom > 0 && Position <= 0)

		{

			var volume = Volume + Math.Abs(Position);

			BuyMarket(volume);

			_cooldown = 2;

		}

		else if (close < ema && _prevMom >= 0 && mom < 0 && Position >= 0)

		{

			var volume = Volume + Math.Abs(Position);

			SellMarket(volume);

			_cooldown = 2;

		}

		_prevMom = mom;

	}

}