Открыть на GitHub

Стратегия Slime Mold RSI

Прямая конверсия советника MQL4 «Slime_Mold_RSI_v1.1». Стратегия формирует один персептрон из четырёх значений RSI (12, 36, 108 и 324), рассчитанных по медианной цене свечи. Каждое значение RSI нормируется из диапазона 0–100 в интервал -1…+1 и умножается на настраиваемый вес. Пересечение суммы через ноль приводит к развороту позиции.

Как это работает

  • Для каждой завершённой свечи вычисляется медианная цена и подаётся в четыре индикатора Relative Strength Index с периодами 12, 36, 108 и 324.
  • Каждое значение RSI нормируется в диапазон -1…+1 и умножается на соответствующий вес. Значение по умолчанию (-100) воспроизводит исходные коэффициенты персептрона (x - 100).
  • Взвешенные значения суммируются, формируя текущее значение персептрона.
  • Полученное значение сравнивается с предыдущим значением персептрона для выявления пересечений нуля и генерации сигналов.

Торговые правила

  • Вход в лонг: предыдущий персептрон ниже нуля, а текущий поднимается выше нуля. Стратегия закрывает шорт и открывает лонг объёмом Volume.
  • Вход в шорт: предыдущий персептрон выше нуля, а текущий опускается ниже нуля. Стратегия закрывает лонг и открывает шорт объёмом Volume.
  • Управление позицией: явных тейк-профитов и стоп-лоссов нет. Позиция изменяется только при новом пересечении нуля.

Параметры

  • Weight1 – коэффициент для нормированного RSI с периодом 12.
  • Weight2 – коэффициент для нормированного RSI с периодом 36.
  • Weight3 – коэффициент для нормированного RSI с периодом 108.
  • Weight4 – коэффициент для нормированного RSI с периодом 324.
  • CandleType – таймфрейм свечей, используемых стратегией. По умолчанию — часовые свечи.

Детали

  • Критерий входа: пересечение нуля взвешенным персептроном RSI.
  • Лонг/Шорт: оба направления (после первого сигнала стратегия всегда в позиции).
  • Критерий выхода: противоположное пересечение нуля разворачивает позицию.
  • Стопы: отсутствуют.
  • Значения по умолчанию:
    • Weight1 = -100
    • Weight2 = -100
    • Weight3 = -100
    • Weight4 = -100
    • CandleType = часовые свечи
  • Фильтры:
    • Категория: Персептрон / осциллятор
    • Направление: Двухсторонняя торговля
    • Индикаторы: RSI (медианная цена)
    • Стопы: Нет
    • Сложность: Средняя (четыре индикатора с большими периодами)
    • Таймфрейм: Настраиваемый (по умолчанию внутридневной часовой)
    • Сезонность: Нет
    • Нейросети: Линейный персептрон
    • Дивергенция: Нет
    • Уровень риска: Зависит от выбранного объёма и весов

Примечания

  • Даже при отключённой торговле стратегия обновляет значение персептрона, чтобы сохранить целостность состояния при возобновлении работы.
  • Использование медианной цены соответствует параметру PRICE_MEDIAN в исходном скрипте MetaTrader.
  • Позиции разворачиваются мгновенно, поэтому учитывайте возможное проскальзывание при подборе весов и объёма.
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;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Slime Mold RSI perceptron strategy converted from MQL4.
/// The strategy sums weighted RSI inputs to generate zero-crossing signals.
/// </summary>
public class SlimeMoldRsiStrategy : Strategy
{
	private readonly StrategyParam<decimal> _weight1;
	private readonly StrategyParam<decimal> _weight2;
	private readonly StrategyParam<decimal> _weight3;
	private readonly StrategyParam<decimal> _weight4;
	private readonly StrategyParam<DataType> _candleType;

	private RelativeStrengthIndex _rsi12 = null!;
	private RelativeStrengthIndex _rsi36 = null!;
	private RelativeStrengthIndex _rsi108 = null!;
	private RelativeStrengthIndex _rsi324 = null!;

	private decimal? _previousPerceptron;

	/// <summary>
	/// Weight applied to the 12-period RSI input.
	/// </summary>
	public decimal Weight1
	{
		get => _weight1.Value;
		set => _weight1.Value = value;
	}

	/// <summary>
	/// Weight applied to the 36-period RSI input.
	/// </summary>
	public decimal Weight2
	{
		get => _weight2.Value;
		set => _weight2.Value = value;
	}

	/// <summary>
	/// Weight applied to the 108-period RSI input.
	/// </summary>
	public decimal Weight3
	{
		get => _weight3.Value;
		set => _weight3.Value = value;
	}

	/// <summary>
	/// Weight applied to the 324-period RSI input.
	/// </summary>
	public decimal Weight4
	{
		get => _weight4.Value;
		set => _weight4.Value = value;
	}

	/// <summary>
	/// Candle type used for RSI calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="SlimeMoldRsiStrategy"/> class.
	/// </summary>
	public SlimeMoldRsiStrategy()
	{
		_weight1 = Param(nameof(Weight1), -100m)
			.SetDisplay("Weight 1", "Weight applied to the 12-period RSI input", "Perceptron")
			
			.SetOptimize(-200m, 200m, 10m);

		_weight2 = Param(nameof(Weight2), -100m)
			.SetDisplay("Weight 2", "Weight applied to the 36-period RSI input", "Perceptron")
			
			.SetOptimize(-200m, 200m, 10m);

		_weight3 = Param(nameof(Weight3), -100m)
			.SetDisplay("Weight 3", "Weight applied to the 108-period RSI input", "Perceptron")
			
			.SetOptimize(-200m, 200m, 10m);

		_weight4 = Param(nameof(Weight4), -100m)
			.SetDisplay("Weight 4", "Weight applied to the 324-period RSI input", "Perceptron")
			
			.SetOptimize(-200m, 200m, 10m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for candles used in calculations", "General");
	}

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

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

		// Drop cached indicator instances and perceptron history.
		_rsi12 = null!;
		_rsi36 = null!;
		_rsi108 = null!;
		_rsi324 = null!;
		_previousPerceptron = null;
	}

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

		// Create RSI indicators for each horizon used by the original perceptron.
		_rsi12 = new RelativeStrengthIndex { Length = 12 };
		_rsi36 = new RelativeStrengthIndex { Length = 36 };
		_rsi108 = new RelativeStrengthIndex { Length = 108 };
		_rsi324 = new RelativeStrengthIndex { Length = 324 };

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

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

		if (_rsi12 is null || _rsi36 is null || _rsi108 is null || _rsi324 is null)
			return;

		// Median price replicates PRICE_MEDIAN used in the original script.
		var medianPrice = (candle.HighPrice + candle.LowPrice) / 2m;

		var input = new DecimalIndicatorValue(_rsi12, medianPrice, candle.ServerTime) { IsFinal = true };
		_rsi12.Process(input);
		_rsi36.Process(new DecimalIndicatorValue(_rsi36, medianPrice, candle.ServerTime) { IsFinal = true });
		_rsi108.Process(new DecimalIndicatorValue(_rsi108, medianPrice, candle.ServerTime) { IsFinal = true });
		_rsi324.Process(new DecimalIndicatorValue(_rsi324, medianPrice, candle.ServerTime) { IsFinal = true });

		// Wait until every RSI is fully formed before evaluating signals.
		if (!_rsi12.IsFormed || !_rsi36.IsFormed || !_rsi108.IsFormed || !_rsi324.IsFormed)
			return;

		var rsi12Value = _rsi12.GetCurrentValue();
		var rsi36Value = _rsi36.GetCurrentValue();
		var rsi108Value = _rsi108.GetCurrentValue();
		var rsi324Value = _rsi324.GetCurrentValue();

		var currentPerceptron =
			(Weight1 * NormalizeRsi(rsi12Value)) +
			(Weight2 * NormalizeRsi(rsi36Value)) +
			(Weight3 * NormalizeRsi(rsi108Value)) +
			(Weight4 * NormalizeRsi(rsi324Value));

		// Initialize the history with the first complete value.
		if (_previousPerceptron is null)
		{
			_previousPerceptron = currentPerceptron;
			return;
		}

		var previousPerceptron = _previousPerceptron.Value;

		// Even if trading is disabled, keep the state in sync with the incoming data.
		// indicators already checked above via IsFormed

		// Zero-crossing from negative to positive triggers a long entry.
		if (previousPerceptron < 0m && currentPerceptron > 0m && Position <= 0m)
		{
				BuyMarket();
				LogInfo($"Long entry. Previous perceptron: {previousPerceptron:F2}, current: {currentPerceptron:F2}");
		}
		// Zero-crossing from positive to negative triggers a short entry.
		else if (previousPerceptron > 0m && currentPerceptron < 0m && Position >= 0m)
		{
				SellMarket();
				LogInfo($"Short entry. Previous perceptron: {previousPerceptron:F2}, current: {currentPerceptron:F2}");
		}

		// Store the latest perceptron value for the next signal evaluation.
		_previousPerceptron = currentPerceptron;
	}

	private static decimal NormalizeRsi(decimal rsiValue)
	{
		// Transform RSI from [0,100] into [-1,+1] as in the original script.
		return ((rsiValue / 100m) - 0.5m) * 2m;
	}
}