Auf GitHub ansehen

Multi Time-Frame Regression Strategy

A multi-timeframe strategy that combines linear regression channels on M1, M5, and H1 candles. The regression slope from the H1 channel defines the dominant trend, while the M5 and M1 channels provide precise entry locations near support and resistance.

Trading logic

  • Data feeds: nine timeframes of standard candles (M1, M5, M15, M30, H1, H4, D1, W1, MN1).
  • Indicators: each feed is processed by a linear regression channel of configurable length. The channel provides a center line and symmetric upper/lower bands based on the maximum deviation of recent closes.
  • Trend filter: the strategy only considers short trades when the H1 channel slope is negative and long trades when it is positive.
  • Entry:
    • Short – the latest M5 high and M1 high both pierce their upper channel bands while the H1 slope is negative.
    • Long – the latest M5 low and M1 low both reach their lower channel bands while the H1 slope is positive.
  • Order handling: entries are executed with market orders using the configured volume. Stop-loss and take-profit targets are derived from the M5 channel half-width and center line respectively.
  • Exit: positions are closed on the M1 candles when the price hits the protective stop or the center line target.
  • Position management: at most one market position is open at any time.

Parameters

Name Description
EnableTrading Allows the strategy to place orders when enabled.
BarsToCount Number of bars used in every regression channel (default 50).
Volume Market order volume in lots.

Notes

  • Longer regression windows provide smoother channel slopes but slower reactions.
  • The multi-timeframe slope display is useful for monitoring alignment across higher intervals even though only the H1 slope gates entries.
  • Protective levels are recalculated each time a new M5 candle forms; frequent recalibration keeps risk tightly coupled to the current channel geometry.
using System;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Linear regression channel strategy.
/// Uses LinearReg as the center line with Highest/Lowest to form a channel.
/// Sells at upper channel, buys at lower channel, with trend filter from regression slope.
/// </summary>
public class MultiTimeFrameRegressionStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _regressionLength;
	private readonly StrategyParam<int> _channelLength;

	private decimal _prevLrValue;
	private bool _hasPrev;

	public MultiTimeFrameRegressionStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for analysis.", "General");

		_regressionLength = Param(nameof(RegressionLength), 20)
			.SetDisplay("Regression Length", "Period for linear regression.", "Indicators");

		_channelLength = Param(nameof(ChannelLength), 20)
			.SetDisplay("Channel Length", "Period for highest/lowest channel.", "Indicators");
	}

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

	public int RegressionLength
	{
		get => _regressionLength.Value;
		set => _regressionLength.Value = value;
	}

	public int ChannelLength
	{
		get => _channelLength.Value;
		set => _channelLength.Value = value;
	}

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

		_prevLrValue = 0;
		_hasPrev = false;
	}

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

		_prevLrValue = 0;
		_hasPrev = false;

		var lr = new LinearReg { Length = RegressionLength };
		var highest = new Highest { Length = ChannelLength };
		var lowest = new Lowest { Length = ChannelLength };

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

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

	private void ProcessCandle(ICandleMessage candle, decimal lrValue, decimal highestValue, decimal lowestValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var close = candle.ClosePrice;

		// Determine slope direction from regression
		var slope = _hasPrev ? lrValue - _prevLrValue : 0m;

		// Channel boundaries
		var channelMid = (highestValue + lowestValue) / 2m;
		var channelWidth = highestValue - lowestValue;

		if (channelWidth <= 0)
		{
			_prevLrValue = lrValue;
			_hasPrev = true;
			return;
		}

		// Upper/lower thresholds
		var upperThreshold = channelMid + channelWidth * 0.4m;
		var lowerThreshold = channelMid - channelWidth * 0.4m;

		// Exit conditions
		if (Position > 0 && (close >= upperThreshold || slope < 0))
		{
			SellMarket();
		}
		else if (Position < 0 && (close <= lowerThreshold || slope > 0))
		{
			BuyMarket();
		}

		// Entry conditions
		if (Position == 0)
		{
			if (close <= lowerThreshold && slope >= 0)
			{
				// Price near lower channel with flat/rising regression
				BuyMarket();
			}
			else if (close >= upperThreshold && slope <= 0)
			{
				// Price near upper channel with flat/falling regression
				SellMarket();
			}
		}

		_prevLrValue = lrValue;
		_hasPrev = true;
	}
}