Ver no GitHub

Cheduecoglioni Alternating Strategy

Overview

This strategy is a StockSharp port of the MQL5 expert advisor "cheduecoglioni". It always keeps the trader in the market by alternating between short and long positions. Every entry is protected with fixed take-profit and stop-loss levels that are defined in pips and converted to price offsets according to the instrument precision.

Trading Rules

  • The strategy listens to the configured candle series (1 minute by default) and only reacts once a candle is fully closed. This event replaces the tick-based loop of the original expert advisor.
  • When there is no open position and no market order awaiting execution, the strategy sends a market order in the direction stored in the _nextSide state. The very first trade after start is a sell, matching the MQL5 implementation.
  • As soon as a position becomes active, the algorithm waits for it to close either by the protective orders or manual intervention. Once the position returns to zero, the next direction flips, so the following trade will be in the opposite direction.
  • Stop-loss and take-profit distances are applied automatically by StartProtection, ensuring that every trade carries the configured risk-reward distances.

Parameters

  • Trade Volume – volume used for each market entry. This mirrors the InpLots input.
  • Take Profit (pips) – distance in pips for the take-profit order. The strategy converts it to absolute price distance using the detected pip size.
  • Stop Loss (pips) – distance in pips for the protective stop loss, converted with the same pip size logic.
  • Candle Type – timeframe of the candles that drive the decision loop. Any supported DataType can be supplied.

Implementation Details

  • The pip size is derived from Security.PriceStep. For 3- or 5-digit FX symbols the value is multiplied by 10 to move from fractional pip to standard pip, replicating the MQL adjustment.
  • A waiting flag prevents duplicate market orders while a previous order is awaiting execution. If the broker rejects the order, OnOrderFailed clears the flag so the next candle can retry.
  • OnPositionChanged keeps track of the side of the active position and toggles _nextSide after each flat state. This mirrors the MQL logic that opened the opposite side after every exit.
  • Protective orders are managed by StartProtection with market exits, matching the immediate stop-loss and take-profit assignment that the expert advisor performed on order placement.

Notes

  • The Python version is intentionally not created yet.
  • The strategy does not modify unit tests.
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;

using StockSharp.Algo;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Alternates buy and sell market orders with fixed stop loss and take profit distances.
/// </summary>
public class CheduecoglioniAlternatingStrategy : Strategy
{
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<decimal> _takeProfitPips;
	private readonly StrategyParam<decimal> _stopLossPips;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _pipSize;
	private Sides _nextSide;
	private Sides? _activeSide;

	/// <summary>
	/// Initializes a new instance of the <see cref="CheduecoglioniAlternatingStrategy"/> class.
	/// </summary>
	public CheduecoglioniAlternatingStrategy()
	{
		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetDisplay("Trade Volume", "Volume per trade", "General")
			.SetGreaterThanZero();

		_takeProfitPips = Param(nameof(TakeProfitPips), 10m)
			.SetDisplay("Take Profit (pips)", "Distance to take profit", "Risk")
			.SetGreaterThanZero();

		_stopLossPips = Param(nameof(StopLossPips), 10m)
			.SetDisplay("Stop Loss (pips)", "Distance to stop loss", "Risk")
			.SetGreaterThanZero();

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

	/// <summary>
	/// Volume used for each market order.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set => _tradeVolume.Value = value;
	}

	/// <summary>
	/// Take profit distance expressed in pips.
	/// </summary>
	public decimal TakeProfitPips
	{
		get => _takeProfitPips.Value;
		set => _takeProfitPips.Value = value;
	}

	/// <summary>
	/// Stop loss distance expressed in pips.
	/// </summary>
	public decimal StopLossPips
	{
		get => _stopLossPips.Value;
		set => _stopLossPips.Value = value;
	}

	/// <summary>
	/// Candle type that triggers trading decisions.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

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

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

		Volume = TradeVolume;
		_nextSide = Sides.Sell;
		_activeSide = null;
		_pipSize = 0m;
	}

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

		Volume = TradeVolume; // Align the base volume with the strategy parameter.

		var priceStep = Security?.PriceStep ?? 0m;
		if (priceStep <= 0m)
		{
			var decimals = Security?.Decimals ?? 4;
			priceStep = (decimal)Math.Pow(10, -decimals);
		}

		_pipSize = priceStep;

		var secDecimals = Security?.Decimals;
		if (secDecimals is int digits && (digits == 3 || digits == 5))
		{
			_pipSize *= 10m; // Convert from fractional pip to full pip for FX symbols.
		}

		if (_pipSize <= 0m)
		{
			_pipSize = 1m; // Fallback to a neutral value if the instrument metadata is missing.
		}

		StartProtection(
			takeProfit: new Unit(TakeProfitPips * _pipSize, UnitTypes.Absolute),
			stopLoss: new Unit(StopLossPips * _pipSize, UnitTypes.Absolute),
			useMarketOrders: true);

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ProcessCandle)
			.Start();
	}

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

		// Strategy has no bound indicators, always allow trading.

		if (Position != 0)
			return; // Skip if a position exists.

		var volume = TradeVolume;
		if (volume <= 0m)
			return;

		if (_nextSide == Sides.Buy)
			BuyMarket();
		else
			SellMarket();

	}

	/// <inheritdoc />
	protected override void OnPositionReceived(Position position)
	{
		base.OnPositionReceived(position);

		if (Position > 0)
		{
			_activeSide = Sides.Buy;
			return;
		}

		if (Position < 0)
		{
			_activeSide = Sides.Sell;
			return;
		}

		if (_activeSide.HasValue)
		{
			_nextSide = _activeSide == Sides.Buy ? Sides.Sell : Sides.Buy; // Alternate direction after a flat position.
			_activeSide = null;
		}

	}

}