在 GitHub 上查看

Robust EA 模板策略

该策略根据 MQL 的 Robust EA 模板实现,使用 CCI 和 RSI 指标生成交易信号,并应用固定的止盈与止损。

逻辑

  • 当 CCI 位于 -200..-150 或 -100..-50 且 RSI 在 0 到 25 之间时买入。
  • 当 CCI 位于 50 到 150 且 RSI 在 80 到 100 之间时卖出。
  • 止损和止盈以点数表示,并转换为价格。

参数

  • Candle Type – 蜡烛图类型。
  • CCI Period – CCI 指标周期。
  • RSI Period – RSI 指标周期。
  • Take Profit (pips) – 止盈距离。
  • Stop Loss (pips) – 止损距离。
  • Volume – 交易量。
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy using CCI and RSI indicators to generate trading signals.
/// </summary>
public class RobustEaTemplateStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<DataType> _candleType;

	public int CciPeriod
	{
		get => _cciPeriod.Value;
		set => _cciPeriod.Value = value;
	}

	public int RsiPeriod
	{
		get => _rsiPeriod.Value;
		set => _rsiPeriod.Value = value;
	}

	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

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

	public RobustEaTemplateStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");

		_cciPeriod = Param(nameof(CciPeriod), 14)
			.SetDisplay("CCI Period", "Commodity Channel Index period", "Indicators");

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetDisplay("RSI Period", "Relative Strength Index period", "Indicators");

		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
	}

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

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var cci = new CommodityChannelIndex { Length = CciPeriod };
		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx(cci, rsi, (candle, cciVal, rsiVal) =>
		{
			if (candle.State != CandleStates.Finished)
				return;

			if (!cciVal.IsFormed || !rsiVal.IsFormed)
				return;

			if (!IsFormedAndOnlineAndAllowTrading())
				return;

			var cciValue = cciVal.ToDecimal();
			var rsiValue = rsiVal.ToDecimal();

			// Wider signal conditions
			var longSignal = cciValue < -50m && rsiValue < 40m;
			var shortSignal = cciValue > 50m && rsiValue > 60m;

			if (longSignal && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (shortSignal && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}).Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, rsi);
			DrawOwnTrades(area);
		}
	}
}