Ver en GitHub

TMA Breakout

This strategy exploits breakouts relative to a Triangular Moving Average (TMA). It watches a configurable candle series and compares the previous candle's close to the TMA value plus or minus user defined offsets. A long position is opened when the previous close is above TMA + UpLevel, and a short position is opened when it is below TMA - DownLevel. Opposite signals reverse the position.

Parameters

  • TMA Length – period used to calculate the Triangular Moving Average.
  • Upper Level – price offset added to the TMA to detect long signals.
  • Lower Level – price offset subtracted from the TMA to detect short signals.
  • Candle Type – timeframe of candles used by the strategy.

How it works

  1. Subscribes to the selected candle series.
  2. Binds a Triangular Moving Average indicator to the candles.
  3. On each finished candle it:
    • Stores the previous TMA and close values.
    • Checks if the previous close exceeded the upper or lower level.
    • Sends market orders to open or reverse positions accordingly.
  4. Charts candles, indicator line and own trades for visual analysis.

Notes

The strategy uses market orders without stop-loss or take-profit management. It is intended for educational purposes and should be extended with proper risk controls before live trading.

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 that enters when price breaks the Triangular Moving Average by configurable offsets.
/// </summary>
public class TmaBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<decimal> _upLevel;
	private readonly StrategyParam<decimal> _downLevel;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevTma;
	private decimal? _prevClose;

	/// <summary>
	/// Period for the Triangular Moving Average.
	/// </summary>
	public int Length
	{
		get => _length.Value;
		set => _length.Value = value;
	}

	/// <summary>
	/// Offset above the TMA to trigger a long entry.
	/// </summary>
	public decimal UpLevel
	{
		get => _upLevel.Value;
		set => _upLevel.Value = value;
	}

	/// <summary>
	/// Offset below the TMA to trigger a short entry.
	/// </summary>
	public decimal DownLevel
	{
		get => _downLevel.Value;
		set => _downLevel.Value = value;
	}

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

	/// <summary>
	/// Initialize the TMA breakout strategy.
	/// </summary>
	public TmaBreakoutStrategy()
	{
		_length = Param(nameof(Length), 30)
			.SetDisplay("TMA Length", "Period for the Triangular Moving Average", "Parameters")
			
			.SetOptimize(10, 60, 10);

		_upLevel = Param(nameof(UpLevel), 300m)
			.SetDisplay("Upper Level", "Offset above TMA in price units", "Parameters")
			
			.SetOptimize(100m, 500m, 100m);

		_downLevel = Param(nameof(DownLevel), 300m)
			.SetDisplay("Lower Level", "Offset below TMA in price units", "Parameters")
			
			.SetOptimize(100m, 500m, 100m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).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();
		_prevTma = _prevClose = null;
	}

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

		var tma = new ExponentialMovingAverage { Length = Length };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var prevTma = _prevTma;
		var prevClose = _prevClose;

		if (prevTma is null || prevClose is null)
		{
			_prevTma = tmaValue;
			_prevClose = candle.ClosePrice;
			return;
		}

		var signalUp = prevClose > prevTma + UpLevel;
		var signalDn = prevClose < prevTma - DownLevel;

		if (signalUp && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		else if (signalDn && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		_prevTma = tmaValue;
		_prevClose = candle.ClosePrice;
	}
}