在 GitHub 上查看

通道剥头皮策略

基于ATR的通道突破剥头皮系统。每根K线的高低价取平均得到中点,然后加减ATR乘以系数形成上下通道。当收盘价突破上一根K线的上通道时开多;跌破下通道时开空。通道随趋势移动,既作为动态止损,也用于产生反向信号。

细节

  • 入场条件
    • 买入:收盘价上穿上一根K线的上通道。
    • 卖出:收盘价下破上一根K线的下通道。
  • 多空方向:双向。
  • 出场条件
    • 价格穿越相反通道时反向。
  • 止损:通道带作为移动止损。
  • 过滤器:无。

参数

  • ATR Period – ATR计算周期。
  • ATR Multiplier – ATR通道宽度系数。
  • Candle Type – 使用的K线周期。
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>
/// StdDev based channel breakout scalping strategy.
/// </summary>
public class ChannelScalperStrategy : Strategy
{
	private readonly StrategyParam<int> _stdevPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _up;
	private decimal _down;
	private int _direction;
	private bool _isInitialized;

	public int StdevPeriod { get => _stdevPeriod.Value; set => _stdevPeriod.Value = value; }
	public decimal Multiplier { get => _multiplier.Value; set => _multiplier.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ChannelScalperStrategy()
	{
		_stdevPeriod = Param(nameof(StdevPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("StdDev Period", "Standard deviation period", "General");
		_multiplier = Param(nameof(Multiplier), 1.5m)
			.SetDisplay("Multiplier", "Channel width multiplier", "General");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle Type", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_up = 0;
		_down = 0;
		_direction = 0;
		_isInitialized = false;
	}

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

		var stdev = new StandardDeviation { Length = StdevPeriod };
		SubscribeCandles(CandleType).Bind(stdev, ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal stdevValue)
	{
		if (candle.State != CandleStates.Finished || stdevValue <= 0) return;

		var middle = (candle.HighPrice + candle.LowPrice) / 2;
		var currentUp = middle + Multiplier * stdevValue;
		var currentDown = middle - Multiplier * stdevValue;

		if (!_isInitialized)
		{
			_up = currentUp;
			_down = currentDown;
			_isInitialized = true;
			return;
		}

		if (_direction <= 0 && candle.ClosePrice > _up)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
			_direction = 1;
		}
		else if (_direction >= 0 && candle.ClosePrice < _down)
		{
			if (Position > 0) SellMarket();
			SellMarket();
			_direction = -1;
		}

		if (_direction > 0)
			currentDown = Math.Max(currentDown, _down);
		else if (_direction < 0)
			currentUp = Math.Min(currentUp, _up);

		_up = currentUp;
		_down = currentDown;
	}
}