在 GitHub 上查看

Stochastic Three Periods

Stochastic Three Periods 策略在三个时间框架上对齐随机指标信号。 当快速随机指标发生交叉且两个更高时间框架同向时开仓。

细节

  • 入场条件:快速 %K 向上/下穿越 %D,且在 ShiftEntrance 根K线前出现相反关系;两个更高时间框架的 %K 均在 %D 之上或之下;收盘价配合信号方向。
  • 多空方向:双向。
  • 出场条件:上一根K线的快速随机指标出现反向交叉。
  • 止损止盈:通过 StartProtection 以点数固定。
  • 默认值
    • CandleType1 = 5m
    • CandleType2 = 15m
    • CandleType3 = 30m
    • KPeriod1 = 5
    • KPeriod2 = 5
    • KPeriod3 = 5
    • KExitPeriod = 5
    • ShiftEntrance = 3
    • TakeProfitPoints = 30
    • StopLossPoints = 10
  • 筛选
    • 类型: 振荡指标
    • 方向: 双向
    • 指标: Stochastic
    • 止损: 是
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Stochastic alignment strategy using fast and slow stochastic oscillators.
/// Enters when both stochastics agree on direction.
/// </summary>
public class StochasticThreePeriodsStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private RelativeStrengthIndex _slowRsi;
	private decimal _prevSlow;
	private int _lastSignal;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public StochasticThreePeriodsStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 5)
			.SetGreaterThanZero()
			.SetDisplay("Fast K", "Fast stochastic K period", "Parameters");

		_slowPeriod = Param(nameof(SlowPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Slow K", "Slow stochastic K period", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Working timeframe", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevSlow = 0m;
		_lastSignal = 0;
	}

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

		var fastRsi = new RelativeStrengthIndex { Length = FastPeriod };
		_slowRsi = new RelativeStrengthIndex { Length = SlowPeriod };
		_prevSlow = 0m;

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastRsi, (candle, fastValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				var slowResult = _slowRsi.Process(candle.ClosePrice, candle.CloseTime, true);
				if (!_slowRsi.IsFormed || slowResult.IsEmpty)
					return;
				var slowValue = slowResult.ToDecimal();

				if (fastValue > slowValue && fastValue > 55m && slowValue > 50m && slowValue > _prevSlow && _lastSignal != 1 && Position <= 0)
				{
					BuyMarket();
					_lastSignal = 1;
				}
				else if (fastValue < slowValue && fastValue < 45m && slowValue < 50m && slowValue < _prevSlow && _lastSignal != -1 && Position >= 0)
				{
					SellMarket();
					_lastSignal = -1;
				}

				_prevSlow = slowValue;
			})
			.Start();

		StartProtection(
			new Unit(2000m, UnitTypes.Absolute),
			new Unit(1000m, UnitTypes.Absolute));
	}
}