View on GitHub

Parabolic SAR Early Buy MA Exit Strategy

This strategy uses the Parabolic SAR indicator to enter trades when the indicator switches sides relative to price. A simple moving average provides an additional exit rule: long positions are closed when price drops below the moving average while SAR is above price.

Details

  • Entry Criteria: SAR switches sides relative to price.
  • Long/Short: Both.
  • Exit Criteria: For long positions, exit when SAR > price and price < MA.
  • Stops: Not defined.
  • Default Values:
    • Acceleration = 0.02
    • AccelerationStep = 0.02
    • MaxAcceleration = 0.2
    • MaPeriod = 11
    • CandleType = 5 minute
  • Filters:
    • Category: Trend following
    • Direction: Both
    • Indicators: Parabolic SAR, SMA
    • Stops: None
    • Complexity: Basic
    • Timeframe: Intraday
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
using System;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// EMA crossover with RSI filter strategy.
/// </summary>
public class ParabolicSarEarlyBuyMaBasedExitStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

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

	public ParabolicSarEarlyBuyMaBasedExitStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 40).SetGreaterThanZero();
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

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

		var fast = new ExponentialMovingAverage { Length = 14 };
		var slow = new ExponentialMovingAverage { Length = MaPeriod };

		var prevF = 0m;
		var prevS = 0m;
		var init = false;
		var lastSignal = DateTimeOffset.MinValue;
		var cooldown = TimeSpan.FromMinutes(360);

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, (candle, f, s) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!fast.IsFormed || !slow.IsFormed)
					return;

				if (!init)
				{
					prevF = f;
					prevS = s;
					init = true;
					return;
				}

				if (candle.OpenTime - lastSignal >= cooldown)
				{
					if (prevF <= prevS && f > s && Position <= 0)
					{
						BuyMarket();
						lastSignal = candle.OpenTime;
					}
					else if (prevF >= prevS && f < s && Position >= 0)
					{
						SellMarket();
						lastSignal = candle.OpenTime;
					}
				}

				prevF = f;
				prevS = s;
			})
			.Start();

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