在 GitHub 上查看

TTM 网格策略

该策略基于高低价的 EMA 计算简单的 TTM 状态,并据此建立买卖网格。状态改变时重置网格,价格触及网格线即下单。

详情

  • 入场条件: 价格触及对应 TTM 状态的网格价位
  • 多空方向: 双向
  • 退出条件: 无(持仓累积)
  • 止损: 无
  • 默认值:
    • TtmPeriod = 6
    • GridLevels = 5
    • GridSpacing = 0.01m
    • CandleType = TimeSpan.FromMinutes(5)
  • 过滤器:
    • 类型: 网格
    • 方向: 双向
    • 指标: EMA
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内 (5m)
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中
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>
/// TTM-based grid trading strategy.
/// Uses fast and slow EMA with RSI momentum for directional trading.
/// </summary>
public class TTMGridStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _gridLevels;
	private readonly StrategyParam<decimal> _gridSpacing;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private decimal _prevFast;
	private decimal _prevSlow;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int GridLevels { get => _gridLevels.Value; set => _gridLevels.Value = value; }
	public decimal GridSpacing { get => _gridSpacing.Value; set => _gridSpacing.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public TTMGridStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 8)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 21)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicators");

		_gridLevels = Param(nameof(GridLevels), 14)
			.SetDisplay("RSI Period", "RSI period for momentum", "Strategy");

		_gridSpacing = Param(nameof(GridSpacing), 0.005m)
			.SetDisplay("Grid Spacing", "Distance between grid levels (fraction)", "Strategy");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = 0;
		_prevFast = 0;
		_prevSlow = 0;
		_cooldown = 0;
	}

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

		var fastEma = new ExponentialMovingAverage { Length = FastPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowPeriod };
		var rsi = new RelativeStrengthIndex { Length = GridLevels };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(fastEma, slowEma, rsi, OnProcess).Start();

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

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

		if (_prevRsi == 0 || _prevFast == 0 || _prevSlow == 0)
		{
			_prevRsi = rsiVal;
			_prevFast = fastMa;
			_prevSlow = slowMa;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevRsi = rsiVal;
			_prevFast = fastMa;
			_prevSlow = slowMa;
			return;
		}

		// EMA histogram
		var hist = fastMa - slowMa;
		var histUp = hist > 0m;
		var histDown = hist < 0m;

		// RSI cross 50
		var rsiCrossUp = _prevRsi <= 50m && rsiVal > 50m;
		var rsiCrossDown = _prevRsi >= 50m && rsiVal < 50m;

		// Exit
		if (Position > 0 && rsiCrossDown)
		{
			SellMarket();
			_cooldown = 80;
		}
		else if (Position < 0 && rsiCrossUp)
		{
			BuyMarket();
			_cooldown = 80;
		}

		// Entry
		if (Position == 0)
		{
			if (rsiCrossUp && histUp)
			{
				BuyMarket();
				_cooldown = 80;
			}
			else if (rsiCrossDown && histDown)
			{
				SellMarket();
				_cooldown = 80;
			}
		}

		_prevRsi = rsiVal;
		_prevFast = fastMa;
		_prevSlow = slowMa;
	}
}