在 GitHub 上查看

ColorJFatl Digit 策略

该策略利用 Jurik 移动平均线 (JMA) 的斜率方向来生成交易信号。JMA 近似于原始 MQL5 专家中的 "ColorJFatl_Digit" 指标。当 JMA 向上转折时开多单,向下转折时开空单。斜率反向时平掉相反头寸。

系统可双向交易,默认不使用硬性止损。适用于利用平滑自适应均线捕捉趋势变化的工具。

细节

  • 入场条件:
    • 多头: JMA 斜率从负转正。
    • 空头: JMA 斜率从正转负。
  • 多空方向: 双向。
  • 出场条件:
    • 多头: JMA 斜率转为负值。
    • 空头: JMA 斜率转为正值。
  • 止损: 默认无。
  • 默认参数:
    • JMA Length = 5
    • Timeframe = 4 小时
  • 筛选器:
    • 类别: 趋势跟随
    • 方向: 双向
    • 指标: 单一
    • 止损: 否
    • 复杂度: 简单
    • 时间框架: 中期
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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>
/// Strategy based on the slope changes of Jurik Moving Average (JMA).
/// Opens long when JMA turns up, opens short when JMA turns down.
/// </summary>
public class ColorJFatlDigitStrategy : Strategy
{
	private readonly StrategyParam<int> _jmaLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevJma;
	private decimal? _prevSlope;

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

	public ColorJFatlDigitStrategy()
	{
		_jmaLength = Param(nameof(JmaLength), 5)
			.SetGreaterThanZero()
			.SetDisplay("JMA Length", "Period for Jurik Moving Average", "Parameters");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevJma = null;
		_prevSlope = null;
	}

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

		_prevJma = null;
		_prevSlope = null;

		var jma = new JurikMovingAverage { Length = JmaLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(jma, Process)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var slope = _prevJma is decimal prev ? jmaValue - prev : (decimal?)null;

		if (slope is decimal s && _prevSlope is decimal ps)
		{
			// JMA slope turns positive -> buy
			if (ps <= 0m && s > 0m && Position <= 0)
				BuyMarket();
			// JMA slope turns negative -> sell
			else if (ps >= 0m && s < 0m && Position >= 0)
				SellMarket();
		}

		_prevSlope = slope;
		_prevJma = jmaValue;
	}
}