在 GitHub 上查看

Color XTRIX Histogram 策略

该策略基于平滑 TRIX 指标(从对数收盘价计算的三重指数移动平均动量)的方向变化进行交易。当 TRIX 直方图在下跌后转向上升时开多仓;当其在上涨后转向下跌时开空仓。出现相反信号时反向持仓。本策略不使用止损或止盈。

细节

  • 入场条件
    • 做多TRIX 上升 && 上一周期 TRIX 下降
    • 做空TRIX 下降 && 上一周期 TRIX 上升
  • 多空方向:多空双向
  • 出场条件
    • 多头:TRIX 转向下
    • 空头:TRIX 转向上
  • 止损:无
  • 默认值
    • TRIX Length = 5
    • Smooth Length = 5
    • Momentum Period = 1
    • Candle Type = 4 小时时间框架
  • 过滤器
    • 分类:趋势跟随
    • 方向:双向
    • 指标:TRIX
    • 止损:无
    • 复杂度:低
    • 时间框架:中期
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:中
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>
/// Color XTRIX Histogram strategy.
/// Opens or closes positions when smoothed TRIX turns direction.
/// </summary>
public class ColorXtrixHistogramStrategy : Strategy
{
	private readonly StrategyParam<int> _trixLength;
	private readonly StrategyParam<int> _smoothLength;
	private readonly StrategyParam<int> _momentumPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private TripleExponentialMovingAverage _tripleEma;
	private RateOfChange _roc;
	private ExponentialMovingAverage _smoother;

	private decimal? _prev1;
	private decimal? _prev2;

	public int TrixLength { get => _trixLength.Value; set => _trixLength.Value = value; }
	public int SmoothLength { get => _smoothLength.Value; set => _smoothLength.Value = value; }
	public int MomentumPeriod { get => _momentumPeriod.Value; set => _momentumPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ColorXtrixHistogramStrategy()
	{
		_trixLength = Param(nameof(TrixLength), 5)
			.SetDisplay("TRIX Length", "Length for base triple EMA", "Indicators");

		_smoothLength = Param(nameof(SmoothLength), 5)
			.SetDisplay("Smooth Length", "Length for additional smoothing", "Indicators");

		_momentumPeriod = Param(nameof(MomentumPeriod), 1)
			.SetDisplay("Momentum Period", "Period for rate of change", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_tripleEma = null;
		_roc = null;
		_smoother = null;
		_prev1 = null;
		_prev2 = null;
	}

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

		_prev1 = null;
		_prev2 = null;

		_tripleEma = new TripleExponentialMovingAverage { Length = TrixLength };
		_roc = new RateOfChange { Length = MomentumPeriod };
		_smoother = new ExponentialMovingAverage { Length = SmoothLength };
		Indicators.Add(_tripleEma);
		Indicators.Add(_roc);
		Indicators.Add(_smoother);

		var warmup = new ExponentialMovingAverage { Length = TrixLength };
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(warmup, ProcessCandle)
			.Start();

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

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

		var t = candle.ServerTime;
		var logClose = (decimal)Math.Log((double)candle.ClosePrice);

		var emaResult = _tripleEma.Process(new DecimalIndicatorValue(_tripleEma, logClose, t) { IsFinal = true });
		if (!_tripleEma.IsFormed)
			return;

		var emaVal = emaResult.GetValue<decimal>();
		var rocResult = _roc.Process(new DecimalIndicatorValue(_roc, emaVal, t) { IsFinal = true });
		if (!_roc.IsFormed)
			return;

		var rocVal = rocResult.GetValue<decimal>();
		var smoothResult = _smoother.Process(new DecimalIndicatorValue(_smoother, rocVal, t) { IsFinal = true });
		if (!_smoother.IsFormed)
			return;

		var trix = smoothResult.GetValue<decimal>();

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prev2 = _prev1;
			_prev1 = trix;
			return;
		}

		if (_prev1 is null || _prev2 is null)
		{
			_prev2 = _prev1;
			_prev1 = trix;
			return;
		}

		var wasDown = _prev1 < _prev2;
		var isUp = trix > _prev1;
		var wasUp = _prev1 > _prev2;
		var isDown = trix < _prev1;

		if (wasDown && isUp && Position <= 0)
			BuyMarket();
		else if (wasUp && isDown && Position >= 0)
			SellMarket();

		_prev2 = _prev1;
		_prev1 = trix;
	}
}