在 GitHub 上查看

Parabolic SAR Flip Alert 策略 (4164)

概述

该策略在 StockSharp 框架中重现了 MetaTrader 专家顾问 pSAR_alert2。它在所选交易品种和时间框架上跟踪抛物线 SAR 指标。当 SAR 值从收盘价上方翻转到收盘价下方(或相反)时,策略会生成一条详细的提醒信息。用户也可以选择在翻转方向上自动发送市价单,从而把提醒转化为自动化入场。

交易逻辑

  1. 订阅指定的 K 线序列,并按参数设置计算抛物线 SAR 指标。
  2. 仅在每根 K 线完全收盘后处理数据,以匹配原始 EA 的触发时机。
  3. 比较指标与收盘价的相对位置:
    • 之前 SAR 位于收盘价上方,而当前值位于收盘价下方 → 看涨翻转
    • 之前 SAR 位于收盘价下方,而当前值位于收盘价上方 → 看跌翻转
  4. 对每一次翻转写入日志提醒。如果启用自动交易,会先平掉反方向持仓,再按照信号方向提交新的市价单。

参数

参数 说明
Candle Type 构建 K 线并评估抛物线 SAR 的时间框架。
SAR Step 抛物线 SAR 的初始加速因子。
SAR Max 抛物线 SAR 的最大加速因子。
Enable Auto Trading true 时在提醒出现时发送市价单;为 false 时仅记录日志。
Trade Volume 启用自动交易时使用的下单手数。

转换说明

  • 原始 MetaTrader 脚本通过 Sleep 控制执行频率。StockSharp 采用事件驱动模型,因此策略会在收到新 K 线后立即响应,无需手动延时。
  • 提醒通过 AddInfoLog 记录,复现了原策略的提示行为,同时避免依赖额外的界面组件。
  • 新增的自动交易开关使策略可以融入全自动流程;若希望保持与 MetaTrader 完全一致的行为,请关闭 Enable Auto Trading 参数。
  • 根据要求,此处未提供 Python 版本。
using System;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Parabolic SAR Flip: EMA trend following with ATR stops.
/// </summary>
public class ParabolicSarFlipAlertStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _atrLength;

	private decimal _prevClose;
	private decimal _entryPrice;

	public ParabolicSarFlipAlertStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe.", "General");
		_emaLength = Param(nameof(EmaLength), 20)
			.SetDisplay("EMA Length", "Trend filter.", "Indicators");
		_atrLength = Param(nameof(AtrLength), 14)
			.SetDisplay("ATR Length", "ATR period.", "Indicators");
	}

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

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

		_prevClose = 0; _entryPrice = 0;
	}

		protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_prevClose = 0; _entryPrice = 0;
		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var atr = new AverageTrueRange { Length = AtrLength };
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(ema, atr, ProcessCandle).Start();
		var area = CreateChartArea();
		if (area != null) { DrawCandles(area, subscription); DrawIndicator(area, ema); DrawOwnTrades(area); }
	}

	private void ProcessCandle(ICandleMessage candle, decimal emaVal, decimal atrVal)
	{
		if (candle.State != CandleStates.Finished) return;
		var close = candle.ClosePrice;
		if (_prevClose == 0 || atrVal <= 0) { _prevClose = close; return; }

		if (Position > 0)
		{
			if (close >= _entryPrice + atrVal * 2.5m || close <= _entryPrice - atrVal * 1.5m || close < emaVal) { SellMarket(); _entryPrice = 0; }
		}
		else if (Position < 0)
		{
			if (close <= _entryPrice - atrVal * 2.5m || close >= _entryPrice + atrVal * 1.5m || close > emaVal) { BuyMarket(); _entryPrice = 0; }
		}

		if (Position == 0)
		{
			if (close > emaVal && _prevClose <= emaVal) { _entryPrice = close; BuyMarket(); }
			else if (close < emaVal && _prevClose >= emaVal) { _entryPrice = close; SellMarket(); }
		}

		_prevClose = close;
	}
}