在 GitHub 上查看

Cheduecoglioni 交替策略

概述

该策略是 MQL5 专家顾问“cheduecoglioni”的 StockSharp 版本。它始终保持账户在市,通过固定顺序交替做空和做多。每笔交易都携带固定的止盈和止损距离,这些距离以点值表示,并根据标的物精度转换为绝对价格。

交易逻辑

  • 策略订阅所配置的蜡烛序列(默认 1 分钟),仅在蜡烛完全收盘后执行逻辑,用来替代原始 EA 中基于 Tick 的循环。
  • 当没有持仓并且不存在等待成交的市价单时,按照 _nextSide 状态记录的方向发送市价单。启动后的第一笔交易为卖出,与 MQL5 实现保持一致。
  • 持仓被激活后,算法等待仓位通过保护性订单或手工操作离场。一旦仓位恢复为零,下一笔交易方向立即翻转,从而始终开立与上一次相反的仓位。
  • 通过 StartProtection 自动附加止盈与止损,确保每笔交易都具有预期的风险回报距离。

参数

  • Trade Volume – 每笔市价单的下单量,对应原始输入 InpLots
  • Take Profit (pips) – 止盈点数。策略会根据点值换算成绝对价格偏移。
  • Stop Loss (pips) – 止损点数,使用相同的点值逻辑进行转换。
  • Candle Type – 触发决策循环的蜡烛类型,可设置任意支持的 DataType

实现细节

  • 点值来自 Security.PriceStep。若品种为 3 位或 5 位小数的外汇报价,会额外乘以 10,使分数点还原为标准点,复现 MQL 脚本中的调整。
  • 使用等待标志防止在上一笔市价单尚未成交时重复下单;若经纪商拒绝订单,OnOrderFailed 会清除标志,下一根蜡烛即可重试。
  • OnPositionChanged 追踪当前仓位方向,并在仓位清零后切换 _nextSide,实现与原版 EA 相同的交替开仓行为。
  • StartProtection 以市价退出方式管理保护性订单,等价于原脚本在下单时立即设置止盈止损。

注意事项

  • 目前尚未创建 Python 版本。
  • 本策略不会修改任何单元测试。
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;
		}

	}

}