在 GitHub 上查看

EF Distance Reversal 策略

该策略是 MetaTrader "Exp_EF_distance" 顾问在 StockSharp 平台上的改写。原始的 EF Distance 和 Flat-Trend 指标被简单移动平均线(SMA)和平均真实波幅(ATR)过滤器所取代,用于寻找市场的转折点。算法观察三个连续的 SMA 值以识别局部极值。当 SMA 形成局部底部且波动性超过阈值时开多仓;当出现相反形态时开空仓。头寸在出现反向信号或触及止损/止盈水平时关闭。

详情

  • 入场条件
    • 多头SMA(t-1) < SMA(t-2)SMA(t) > SMA(t-1)ATR(t) ≥ AtrThreshold
    • 空头SMA(t-1) > SMA(t-2)SMA(t) < SMA(t-1)ATR(t) ≥ AtrThreshold
  • 方向:双向。
  • 出场条件
    • 出现相反方向的信号。
    • 触发止损或止盈。
  • 指标
    • 简单移动平均线(SMA)——近似原 EF Distance。
    • 平均真实波幅(ATR)——波动性过滤器。
  • 默认值
    • SMA period = 10。
    • ATR period = 20。
    • ATR threshold = 1。
    • StopLoss = 100。
    • TakeProfit = 200。
  • 过滤器
    • 类别:反转
    • 方向:双向
    • 指标数量:两个
    • 止损:是
    • 复杂度:中等
    • 时间框架:可配置
    • 季节性:否
    • 神经网络:否
    • 背离:是(使用转折点)
    • 风险水平:中等
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>
/// EF Distance reversal strategy.
/// Uses a smoothed price series and ATR filter to trade turning points.
/// </summary>
public class EfDistanceStrategy : Strategy
{
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _atrMultiplier;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prev;
	private decimal? _prev2;

	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
	public decimal AtrMultiplier { get => _atrMultiplier.Value; set => _atrMultiplier.Value = value; }
	public decimal StopLossPct { get => _stopLossPct.Value; set => _stopLossPct.Value = value; }
	public decimal TakeProfitPct { get => _takeProfitPct.Value; set => _takeProfitPct.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public EfDistanceStrategy()
	{
		_smaPeriod = Param(nameof(SmaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "Period for the smoothing moving average", "Indicator");

		_atrPeriod = Param(nameof(AtrPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "Period for the volatility filter", "Indicator");

		_atrMultiplier = Param(nameof(AtrMultiplier), 0.5m)
			.SetDisplay("ATR Multiplier", "Minimum ATR relative to price", "Indicator");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");

		_takeProfitPct = Param(nameof(TakeProfitPct), 4m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prev = null;
		_prev2 = null;
	}

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

		var ema = new ExponentialMovingAverage { Length = SmaPeriod };
		var atr = new AverageTrueRange { Length = AtrPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx(ema, atr, ProcessCandle).Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

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

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue emaVal, IIndicatorValue atrVal)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!emaVal.IsFormed || !atrVal.IsFormed)
			return;

		var smaValue = emaVal.ToDecimal();
		var atrValue = atrVal.ToDecimal();

		if (_prev is null || _prev2 is null)
		{
			_prev2 = _prev;
			_prev = smaValue;
			return;
		}

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var atrEnough = atrValue >= AtrMultiplier / 100m * candle.ClosePrice;

		if (atrEnough)
		{
			if (_prev < _prev2 && smaValue > _prev && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (_prev > _prev2 && smaValue < _prev && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prev2 = _prev;
		_prev = smaValue;
	}
}