在 GitHub 上查看

Aver4 Stoch Post ZigZag

该策略结合了四个不同周期的 Stochastic 振荡器和一个简单的 ZigZag 枢轴检测器。平均 Stochastic 用于判断超买和超卖水平,而 ZigZag 用于确认新的高点和低点。当平均 Stochastic 低于超卖阈值并出现新的 ZigZag 低点时买入;当平均 Stochastic 高于超买阈值并出现新的 ZigZag 高点时卖出。反向信号出现时会平掉相反的持仓。

细节

  • 入场条件:平均 Stochastic 穿越超买/超卖水平并出现匹配的 ZigZag 枢轴。
  • 多空方向:双向。
  • 出场条件:相反信号。
  • 止损:StartProtection 2%/2%(默认)。
  • 默认值
    • ShortLength = 26
    • MidLength1 = 72
    • MidLength2 = 144
    • LongLength = 288
    • ZigZagDepth = 14
    • Oversold = 5
    • Overbought = 95
    • CandleType = TimeSpan.FromMinutes(5)
  • 筛选条件
    • 分类:Oscillator
    • 方向:双向
    • 指标:Stochastic, ZigZag
    • 止损:是
    • 复杂度:高级
    • 时间框架:日内
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// Averaged Stochastic with ZigZag-style pivot confirmation.
/// Uses RSI as simplified oscillator and highest/lowest for pivots.
/// </summary>
public class Aver4StochPostZigZagStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<int> _pivotLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private bool _hasPrev;

	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public int PivotLength { get => _pivotLength.Value; set => _pivotLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Aver4StochPostZigZagStrategy()
	{
		_rsiLength = Param(nameof(RsiLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Length", "RSI period", "Indicators");

		_pivotLength = Param(nameof(PivotLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("Pivot Length", "Highest/Lowest period", "Indicators");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = 0;
		_hasPrev = false;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiLength };
		var highest = new Highest { Length = PivotLength };
		var lowest = new Lowest { Length = PivotLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(rsi, highest, lowest, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevRsi = rsi;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		// Near pivot low + RSI oversold -> buy
		if (close <= low * 1.001m && rsi < 30 && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Near pivot high + RSI overbought -> sell
		else if (close >= high * 0.999m && rsi > 70 && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}
		// Exit long on RSI overbought
		else if (Position > 0 && rsi > 70)
		{
			SellMarket();
		}
		// Exit short on RSI oversold
		else if (Position < 0 && rsi < 30)
		{
			BuyMarket();
		}

		_prevRsi = rsi;
	}
}