Открыть на GitHub

Многофреймовый Parabolic SAR

Стратегия объединяет сигналы Parabolic SAR с разных таймфреймов. Лонг открывается, когда цена выше выбранных уровней SAR. Шорт появляется при падении цены ниже SAR. Возможны стоп-лосс, трейлинг-стоп и тейк-профит.

Детали

  • Условия входа:
    • Лонг: цена выше SAR согласно параметру LongSource.
    • Шорт: цена ниже SAR согласно ShortSource.
  • Условия выхода:
    • Пересечение цены с противоположной стороной SAR или срабатывание защиты.
  • Индикаторы:
    • Parabolic SAR на текущем таймфрейме
    • Опционально Parabolic SAR на старшем и младшем таймфреймах
  • Стопы: опциональные стоп-лосс, трейлинг-стоп, тейк-профит через StartProtection.
  • Значения по умолчанию:
    • Acceleration = 0.02
    • MaxAcceleration = 0.2
    • StopLossPercent = 1
    • TrailingPercent = 0.5
    • TakeProfitPercent = 2
  • Фильтры:
    • Таймфреймы: основной 5м, старший 1д, младший 1м
    • Индикаторы: Parabolic SAR
    • Стопы: опционально
    • Сложность: средняя
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>
/// Parabolic SAR-style strategy using EMA and RSI.
/// </summary>
public class MultiTimeframeParabolicSarStrategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<DataType> _candleType;

	public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MultiTimeframeParabolicSarStrategy()
	{
		_emaLength = Param(nameof(EmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA", "EMA period", "Indicators");
		_rsiLength = Param(nameof(RsiLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI", "RSI period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(2).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var rsi = new RelativeStrengthIndex { Length = RsiLength };

		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(ema, rsi, (candle, emaVal, rsiVal) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!ema.IsFormed || !rsi.IsFormed)
					return;

				// Buy when price above EMA and RSI confirms uptrend
				if (candle.ClosePrice > emaVal && rsiVal > 50 && Position <= 0)
					BuyMarket();
				// Sell when price below EMA and RSI confirms downtrend
				else if (candle.ClosePrice < emaVal && rsiVal < 50 && Position > 0)
					SellMarket();
			})
			.Start();

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