Открыть на GitHub

Стратегия MA With Logistic

Несмотря на историческое название, эта реализация является стратегией пересечения двух EMA и не рассчитывает логистическую модель. Пересечения на завершённых свечах открывают или разворачивают позицию, а процентные тейк-профит и стоп-лосс отсчитываются от цен исполнения.

Подробности

  • Данные: ценовые свечи.
  • Условия входа:
    • Лонг: быстрая EMA пересекает медленную снизу вверх.
    • Шорт: быстрая EMA пересекает медленную сверху вниз.
  • Условия выхода: достигнут TakeProfitPercent или StopLossPercent; противоположное пересечение разворачивает позицию.
  • Пауза: пять завершённых свечей после каждой заявки по пересечению.
  • Параметры по умолчанию:
    • FastLength = 12
    • SlowLength = 25
    • TakeProfitPercent = 8
    • StopLossPercent = 5
    • CandleType = 20 минут
  • Фильтры:
    • Категория: трендовая
    • Направление: лонг и шорт
    • Индикаторы: MA
    • Сложность: низкая
    • Уровень риска: средний
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>
/// MA crossover strategy with percent-based exits.
/// </summary>
public class MaWithLogisticStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<decimal> _takeProfitPercent;
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _fastMa;
	private ExponentialMovingAverage _slowMa;
	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _initialized;
	private int _cooldown;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public decimal TakeProfitPercent { get => _takeProfitPercent.Value; set => _takeProfitPercent.Value = value; }
	public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MaWithLogisticStrategy()
	{
		_fastLength = Param(nameof(FastLength), 12).SetGreaterThanZero()
			.SetDisplay("Fast MA", "Fast MA period", "Indicators");
		_slowLength = Param(nameof(SlowLength), 25).SetGreaterThanZero()
			.SetDisplay("Slow MA", "Slow MA period", "Indicators");
		_takeProfitPercent = Param(nameof(TakeProfitPercent), 8m).SetGreaterThanZero()
			.SetDisplay("TP %", "Take profit percent", "Risk");
		_stopLossPercent = Param(nameof(StopLossPercent), 5m).SetGreaterThanZero()
			.SetDisplay("SL %", "Stop loss percent", "Risk");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(20).TimeFrame())
			.SetDisplay("Candle Type", "Candles", "General");
	}

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

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

		_prevFast = default;
		_prevSlow = default;
		_initialized = false;
		_cooldown = default;
	}

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

		StartProtection(
			takeProfit: new Unit(TakeProfitPercent, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPercent, UnitTypes.Percent));

		_fastMa = new ExponentialMovingAverage { Length = FastLength };
		_slowMa = new ExponentialMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(_fastMa, _slowMa, ProcessCandle).Start();

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

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

		if (!_fastMa.IsFormed || !_slowMa.IsFormed)
			return;

		if (!_initialized)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_initialized = true;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevFast = fast;
			_prevSlow = slow;
			return;
		}

		// MA crossover entry
		var crossUp = _prevFast <= _prevSlow && fast > slow;
		var crossDown = _prevFast >= _prevSlow && fast < slow;

		if (crossUp && Position <= 0)
		{
			BuyMarket(Volume + Math.Abs(Position));
			_cooldown = 5;
		}
		else if (crossDown && Position >= 0)
		{
			SellMarket(Volume + Math.Abs(Position));
			_cooldown = 5;
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}