在 GitHub 上查看

Zigzag Candles

该策略根据 ZigZag 枢轴点进行交易。当出现新的低点枢轴时开多仓,当出现新的高点枢轴时开空仓。

细节

  • 入场条件:ZigZag 枢轴点。
  • 多空方向:双向。
  • 出场条件:相反的枢轴。
  • 止损:无。
  • 默认值:
    • ZigzagLength = 5
    • CandleType = TimeSpan.FromMinutes(1)
  • 过滤器:
    • 类别: 趋势
    • 方向: 双向
    • 指标: Highest, Lowest
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险水平: 中等
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>
/// Strategy trading on ZigZag pivots.
/// </summary>
public class ZigzagCandlesStrategy : Strategy
{
	private readonly StrategyParam<int> _zigzagLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _lastZigzag;
	private decimal _lastZigzagHigh;
	private decimal _lastZigzagLow;
	private int _direction;
	private bool _signalFired;

	public int ZigzagLength
	{
		get => _zigzagLength.Value;
		set => _zigzagLength.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public ZigzagCandlesStrategy()
	{
		_zigzagLength = Param(nameof(ZigzagLength), 5)
			.SetDisplay("ZigZag Length", "Lookback for pivot search", "ZigZag");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_lastZigzag = 0m;
		_lastZigzagHigh = 0m;
		_lastZigzagLow = 0m;
		_direction = 0;
		_signalFired = false;
	}

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

		_lastZigzag = 0m;
		_lastZigzagHigh = 0m;
		_lastZigzagLow = 0m;
		_direction = 0;
		_signalFired = false;

		var highest = new Highest { Length = ZigzagLength };
		var lowest = new Lowest { Length = ZigzagLength };

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

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

	private void ProcessCandle(ICandleMessage candle, decimal highest, decimal lowest)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var prevDirection = _direction;

		if (candle.HighPrice >= highest && _direction != 1)
		{
			_lastZigzag = candle.HighPrice;
			_lastZigzagHigh = candle.HighPrice;
			_direction = 1;
			_signalFired = false;
		}
		else if (candle.LowPrice <= lowest && _direction != -1)
		{
			_lastZigzag = candle.LowPrice;
			_lastZigzagLow = candle.LowPrice;
			_direction = -1;
			_signalFired = false;
		}

		if (_signalFired)
			return;

		if (_direction == -1 && prevDirection != -1 && Position <= 0)
		{
			BuyMarket();
			_signalFired = true;
		}
		else if (_direction == 1 && prevDirection != 1 && Position >= 0)
		{
			SellMarket();
			_signalFired = true;
		}
	}
}