GitHub で見る

Trigger Line Strategy

The Trigger Line strategy combines a weighted trend line with a least squares moving average (LSMA). A long position is opened when the weighted trend line crosses above the LSMA, while a short position is opened when it crosses below.

How It Works

  • Long Entry: weighted trend line crosses above LSMA.
  • Long Exit: weighted trend line crosses below LSMA.
  • Short Entry: weighted trend line crosses below LSMA.
  • Short Exit: weighted trend line crosses above LSMA.
  • Indicators: Weighted Moving Average, Linear Regression (LSMA).

Parameters

  • WT Period – lookback for the weighted trend line.
  • LSMA Period – smoothing period for LSMA.
  • Candle Type – timeframe of candles used for calculations.
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>
/// Trigger Line cross strategy based on weighted trend line and LSMA.
/// </summary>
public class TriggerLineStrategy : Strategy
{
	private readonly StrategyParam<int> _wmaPeriod;
	private readonly StrategyParam<int> _lsmaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private WeightedMovingAverage _wma;
	private LinearReg _lsma;

	private bool _initialized;
	private decimal _prevLine;
	private decimal _prevSignal;

	public TriggerLineStrategy()
	{
		_wmaPeriod = Param(nameof(WmaPeriod), 24)
			.SetDisplay("WT Period", "Period for weighted trend line", "Trigger Line");

		_lsmaPeriod = Param(nameof(LsmaPeriod), 6)
			.SetDisplay("LSMA Period", "Period for least squares moving average", "Trigger Line");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle type used for the strategy", "General");
	}

	public int WmaPeriod
	{
		get => _wmaPeriod.Value;
		set => _wmaPeriod.Value = value;
	}

	public int LsmaPeriod
	{
		get => _lsmaPeriod.Value;
		set => _lsmaPeriod.Value = value;
	}

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

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

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

		_initialized = false;
		_prevLine = 0m;
		_prevSignal = 0m;
	}

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

		_initialized = false;
		_prevLine = 0m;
		_prevSignal = 0m;

		_wma = new WeightedMovingAverage { Length = WmaPeriod };
		_lsma = new LinearReg { Length = LsmaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_wma, _lsma, ProcessCandle)
			.Start();

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

	private void ProcessCandle(ICandleMessage candle, decimal wmaValue, decimal lsmaValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_initialized)
		{
			_prevLine = wmaValue;
			_prevSignal = lsmaValue;
			_initialized = true;
			return;
		}

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevLine = wmaValue;
			_prevSignal = lsmaValue;
			return;
		}

		var crossUp = _prevLine <= _prevSignal && wmaValue > lsmaValue;
		var crossDown = _prevLine >= _prevSignal && wmaValue < lsmaValue;

		if (crossUp && Position <= 0)
			BuyMarket();
		else if (crossDown && Position >= 0)
			SellMarket();

		_prevLine = wmaValue;
		_prevSignal = lsmaValue;
	}
}