在 GitHub 上查看

线性交叉交易策略

该策略根据成交量计算价格的线性回归以得到预测价格。当预测价格上穿其加权移动平均且MACD线向上穿越信号线时建立多头;当MACD线下穿信号线且最近低点不断降低时建立空头。

细节

  • 入场条件
    • 多头:预测价格上穿WMA且MACD线上升并高于信号线。
    • 空头:MACD线下降并低于信号线,同时低点持续降低。
  • 多空方向:双向。
  • 出场条件:无;持仓在相反信号时反转。
  • 止损:无。
  • 默认值
    • Length = 21。
    • LinearLength = 9。
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()。
  • 筛选
    • 类型: 趋势
    • 方向: 双向
    • 指标: 线性回归, WMA, MACD
    • 止损: 无
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中
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>
/// Strategy that uses linear regression slope crossover with MACD confirmation.
/// Goes long when regression slope turns positive and MACD above signal.
/// Goes short when regression slope turns negative and MACD below signal.
/// </summary>
public class LinearCrossTradingStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<decimal> _slopeThresholdPercent;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSlope;
	private bool _prevSlopeSet;
	private int _barsFromSignal;

	public int Length
	{
		get => _length.Value;
		set => _length.Value = value;
	}

	public decimal SlopeThresholdPercent
	{
		get => _slopeThresholdPercent.Value;
		set => _slopeThresholdPercent.Value = value;
	}

	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

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

	public LinearCrossTradingStrategy()
	{
		_length = Param(nameof(Length), 21)
			.SetGreaterThanZero()
			.SetDisplay("Regression Length", "Number of bars for linear regression", "Indicator");

		_slopeThresholdPercent = Param(nameof(SlopeThresholdPercent), 0.02m)
			.SetGreaterThanZero()
			.SetDisplay("Slope Threshold %", "Minimum normalized slope for signals", "Indicator");

		_cooldownBars = Param(nameof(CooldownBars), 60)
			.SetGreaterThanZero()
			.SetDisplay("Cooldown Bars", "Minimum bars between entries", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(10).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles for strategy", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_prevSlope = 0m;
		_prevSlopeSet = false;
		_barsFromSignal = int.MaxValue;
	}

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

		var linReg = new LinearReg { Length = Length };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(linReg, OnProcess).Start();

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

	private void OnProcess(ICandleMessage candle, decimal linRegValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var closePrice = candle.ClosePrice;
		if (closePrice <= 0)
			return;

		var slope = (closePrice - linRegValue) / closePrice * 100m;

		if (!_prevSlopeSet)
		{
			_prevSlope = slope;
			_prevSlopeSet = true;
			return;
		}

		_barsFromSignal++;

		if (_barsFromSignal >= CooldownBars)
		{
			if (_prevSlope <= SlopeThresholdPercent && slope > SlopeThresholdPercent && Position <= 0)
			{
				BuyMarket();
				_barsFromSignal = 0;
			}
			else if (_prevSlope >= -SlopeThresholdPercent && slope < -SlopeThresholdPercent && Position >= 0)
			{
				SellMarket();
				_barsFromSignal = 0;
			}
		}

		_prevSlope = slope;
	}
}