在 GitHub 上查看

Donchian通道

Donchian通道突破策略在价格突破通道高点或低点时入场。收盘价突破上轨视为强势信号,跌破下轨则做空,价格回到中线时离场。通道基于指定周期内的最高价和最低价,突破通常意味着波动扩大。

测试表明年均收益约为 52%,该策略在加密市场表现最佳。

详情

  • 入场条件: 基于价格行为的信号
  • 多空方向: 双向
  • 退出条件: 反向信号
  • 止损: 无
  • 默认值:
    • ChannelPeriod = 20
    • CandleType = TimeSpan.FromMinutes(5)
  • 过滤器:
    • 类型: 趋势
    • 方向: 双向
    • 指标: Price Action
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内 (5m)
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中
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 based on Donchian Channel.
/// It enters long position when price breaks through the upper band and short position when price breaks through the lower band.
/// </summary>
public class DonchianChannelStrategy : Strategy
{
	private readonly StrategyParam<int> _channelPeriod;
	private readonly StrategyParam<DataType> _candleType;

	// Current state
	private decimal _prevClosePrice;
	private decimal _prevUpperBand;
	private decimal _prevLowerBand;

	/// <summary>
	/// Period for Donchian Channel.
	/// </summary>
	public int ChannelPeriod
	{
		get => _channelPeriod.Value;
		set => _channelPeriod.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initialize the Donchian Channel strategy.
	/// </summary>
	public DonchianChannelStrategy()
	{
		_channelPeriod = Param(nameof(ChannelPeriod), 1000)
			.SetDisplay("Channel Period", "Period for Donchian Channel calculation", "Indicators")
			
			.SetOptimize(10, 50, 5);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevClosePrice = default;
		_prevUpperBand = default;
		_prevLowerBand = default;

	}

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

		// Create indicators
		var donchian = new DonchianChannels { Length = ChannelPeriod };

		// Create subscription and bind indicators
		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(donchian, ProcessCandle)
			.Start();

		// Setup chart visualization if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, donchian);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue donchianValue)
	{
		// Skip unfinished candles
		if (candle.State != CandleStates.Finished)
			return;

		// Check if strategy is ready to trade
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var donchianTyped = (DonchianChannelsValue)donchianValue;
		
		if (donchianTyped.UpperBand is not decimal upperValue ||
			donchianTyped.LowerBand is not decimal lowerValue ||
			donchianTyped.Middle is not decimal midValue)
		{
			return;
		}

		// Skip the first received value for proper comparison
		if (_prevUpperBand == 0)
		{
			_prevClosePrice = candle.ClosePrice;
			_prevUpperBand = upperValue;
			_prevLowerBand = lowerValue;
			return;
		}

		// Check for breakouts
		var isUpperBreakout = candle.ClosePrice > _prevUpperBand && _prevClosePrice <= _prevUpperBand;
		var isLowerBreakout = candle.ClosePrice < _prevLowerBand && _prevClosePrice >= _prevLowerBand;

		// Entry logic - breakout reversal
		if (isUpperBreakout && Position <= 0)
		{
			BuyMarket(Volume + Math.Abs(Position));
		}
		else if (isLowerBreakout && Position >= 0)
		{
			SellMarket(Volume + Math.Abs(Position));
		}

		// Update previous values
		_prevClosePrice = candle.ClosePrice;
		_prevUpperBand = upperValue;
		_prevLowerBand = lowerValue;
	}
}