Открыть на GitHub

Стратегия Robust EA Template

Стратегия реализует шаблон робота MQL с индикаторами 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);
		}
	}
}