在 GitHub 上查看

Adaptive SMI Ergodic策略

Adaptive SMI Ergodic策略利用True Strength Index (TSI)振荡器及其EMA信号线来识别超买或超卖后的反转。当TSI上穿超卖阈值并位于信号线上方时开多仓;当TSI下穿超买阈值且位于信号线下方时开空仓。

细节

  • 入场条件
    • TSI上穿超卖阈值且TSI > 信号线(多)。
    • TSI下穿超买阈值且TSI < 信号线(空)。
  • 方向:多头和空头。
  • 出场条件
    • 反向信号触发反向交易。
  • 止损:无。
  • 默认参数
    • LongLength = 12
    • ShortLength = 5
    • SignalLength = 5
    • OversoldThreshold = -0.4
    • OverboughtThreshold = 0.4
  • 过滤器
    • 类型:动量振荡器
    • 方向:多/空
    • 指标:True Strength Index、EMA
    • 止损:无
    • 复杂度:低
    • 时间框架:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:低
namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;

using Ecng.Common;

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

/// <summary>
/// Adaptive SMI Ergodic Strategy - uses True Strength Index crossovers with signal line confirmation.
/// </summary>
public class AdaptiveSmiErgodicStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _firstLength;
	private readonly StrategyParam<int> _secondLength;
	private readonly StrategyParam<int> _signalLength;
	private readonly StrategyParam<decimal> _oversoldThreshold;
	private readonly StrategyParam<decimal> _overboughtThreshold;
	private readonly StrategyParam<int> _cooldownBars;

	private decimal _previousTsi;
	private int _cooldownRemaining;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int FirstLength { get => _firstLength.Value; set => _firstLength.Value = value; }
	public int SecondLength { get => _secondLength.Value; set => _secondLength.Value = value; }
	public int SignalLength { get => _signalLength.Value; set => _signalLength.Value = value; }
	public decimal OversoldThreshold { get => _oversoldThreshold.Value; set => _oversoldThreshold.Value = value; }
	public decimal OverboughtThreshold { get => _overboughtThreshold.Value; set => _overboughtThreshold.Value = value; }
	public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }

	public AdaptiveSmiErgodicStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_firstLength = Param(nameof(FirstLength), 25)
			.SetGreaterThanZero()
			.SetDisplay("First Length", "First smoothing length for TSI", "TSI")
			.SetOptimize(10, 30, 5);

		_secondLength = Param(nameof(SecondLength), 13)
			.SetGreaterThanZero()
			.SetDisplay("Second Length", "Second smoothing length for TSI", "TSI")
			.SetOptimize(5, 20, 3);

		_signalLength = Param(nameof(SignalLength), 7)
			.SetGreaterThanZero()
			.SetDisplay("Signal Length", "Signal EMA length", "TSI")
			.SetOptimize(3, 15, 2);

		_oversoldThreshold = Param(nameof(OversoldThreshold), -10m)
			.SetDisplay("Oversold Threshold", "Oversold level for TSI", "TSI");

		_overboughtThreshold = Param(nameof(OverboughtThreshold), 10m)
			.SetDisplay("Overbought Threshold", "Overbought level for TSI", "TSI");

		_cooldownBars = Param(nameof(CooldownBars), 10)
			.SetDisplay("Cooldown Bars", "Bars between trades", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_previousTsi = 0;
		_cooldownRemaining = 0;
	}

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

		_previousTsi = 0;

		var tsi = new TrueStrengthIndex
		{
			FirstLength = FirstLength,
			SecondLength = SecondLength,
			SignalLength = SignalLength
		};

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var tv = (ITrueStrengthIndexValue)tsiValue;

		if (tv.Tsi is not decimal tsiVal || tv.Signal is not decimal signalVal)
			return;

		if (_cooldownRemaining > 0)
		{
			_cooldownRemaining--;
			_previousTsi = tsiVal;
			return;
		}

		var crossAboveOversold = _previousTsi <= OversoldThreshold && tsiVal > OversoldThreshold;
		var crossBelowOverbought = _previousTsi >= OverboughtThreshold && tsiVal < OverboughtThreshold;

		if (crossAboveOversold && tsiVal > signalVal && Position <= 0)
		{
			if (Position < 0)
				BuyMarket(Math.Abs(Position));
			BuyMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}
		else if (crossBelowOverbought && tsiVal < signalVal && Position >= 0)
		{
			if (Position > 0)
				SellMarket(Math.Abs(Position));
			SellMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}

		_previousTsi = tsiVal;
	}
}