在 GitHub 上查看

SAR Trailing System Strategy

该策略在固定时间间隔随机开仓多头或空头,并使用抛物线 SAR 指标管理退出。 SAR 值作为跟踪止损,当价格穿越 SAR 水平时平仓。

细节

  • 入场条件
    • 每到 TimerInterval 且没有持仓并且 UseRandomEntry 启用时,随机开多或开空。
  • 多空方向:双向
  • 出场条件:价格穿越 Parabolic SAR。
  • 止损:初始止损以 tick 为单位,结合 Parabolic SAR 跟踪退出。
  • 默认值
    • TimerInterval = 300 秒
    • StopLossTicks = 10
    • AccelerationStep = 0.02
    • AccelerationMax = 0.2
    • UseRandomEntry = true
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame()
  • 筛选
    • 类别:趋势跟踪
    • 方向:双向
    • 指标:Parabolic SAR
    • 止损:是
    • 复杂度:初级
    • 时间框架:短期
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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 that uses Parabolic SAR for trend-following entries and exits.
/// Buy when price crosses above SAR, sell when below.
/// </summary>
public class SarTrailingSystemStrategy : Strategy
{
	private readonly StrategyParam<decimal> _accelerationStep;
	private readonly StrategyParam<decimal> _accelerationMax;
	private readonly StrategyParam<DataType> _candleType;

	public decimal AccelerationStep { get => _accelerationStep.Value; set => _accelerationStep.Value = value; }
	public decimal AccelerationMax { get => _accelerationMax.Value; set => _accelerationMax.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SarTrailingSystemStrategy()
	{
		_accelerationStep = Param(nameof(AccelerationStep), 0.02m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Step", "Parabolic SAR acceleration step", "Indicators");

		_accelerationMax = Param(nameof(AccelerationMax), 0.2m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Max", "Parabolic SAR maximum acceleration", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

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

		var sar = new ParabolicSar
		{
			Acceleration = AccelerationStep,
			AccelerationStep = AccelerationStep,
			AccelerationMax = AccelerationMax
		};

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		// Price above SAR = uptrend, buy
		if (candle.ClosePrice > sarValue && Position <= 0)
			BuyMarket();
		// Price below SAR = downtrend, sell
		else if (candle.ClosePrice < sarValue && Position >= 0)
			SellMarket();
	}
}