Auf GitHub ansehen

Zigzag Candles

Simple strategy that reacts to ZigZag pivot points. A long position is opened when a new low pivot forms, while a short position is taken at new high pivots.

Details

  • Entry Criteria: Pivot highs and lows from ZigZag.
  • Long/Short: Both directions.
  • Exit Criteria: Opposite pivot.
  • Stops: No.
  • Default Values:
    • ZigzagLength = 5
    • CandleType = TimeSpan.FromMinutes(1)
  • Filters:
    • Category: Trend
    • Direction: Both
    • Indicators: Highest, Lowest
    • Stops: No
    • Complexity: Basic
    • Timeframe: Intraday
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: Medium
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;
		}
	}
}