在 GitHub 上查看

Color JSatl Digit 策略

本策略将 MQL5 专家顾问 "Exp_ColorJSatl_Digit" 转换为 StockSharp 实现。系统使用 Jurik 移动平均线 (JMA) 的斜率并将其数字化为上升或下降状态。当状态从 0 变为 1 时表示上升趋势开始,从 1 变为 0 时表示下降趋势开始。

算法订阅所选周期的K线并绑定 JMA 指标。当 JMA 向上转折时,策略开多并关闭空头;当 JMA 向下转折时,策略开空并关闭多头。DirectMode 参数可反向信号以进行逆势交易。

仓位通过百分比止损和止盈保护。所有参数均通过 StrategyParam 定义并可用于优化。

细节

  • 入场条件
    • 多头:JMA 向上转折(prev > prevPrevcurrent >= prev)且 DirectMode 为真;在反向模式下,向下转折触发多头。
    • 空头:JMA 向下转折(prev < prevPrevcurrent <= prev)且 DirectMode 为真;在反向模式下,向上转折触发空头。
  • 出场条件:相反信号立即在另一方向开仓。保护性订单也可能平仓。
  • 止损:通过 StartProtection 设置百分比止损和止盈。
  • 默认值
    • JMA Length = 30
    • Candle Type = 4 小时K线
    • Stop Loss % = 1
    • Take Profit % = 2
    • Direct Mode = true
  • 过滤器
    • 类别:趋势跟随
    • 方向:双向(可逆)
    • 指标:Jurik 移动平均
    • 止损:是
    • 复杂度:中等
    • 周期:中期
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险:中等
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>
/// Trend-following strategy based on the slope of Jurik Moving Average.
/// Opens long when the JMA turns up and short when it turns down.
/// </summary>
public class ColorJsatlDigitStrategy : Strategy
{
	private readonly StrategyParam<int> _jmaLength;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<bool> _directMode;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<decimal> _takeProfit;

	private decimal? _prevJma;
	private decimal? _prevPrevJma;

	/// <summary>
	/// JMA period length.
	/// </summary>
	public int JmaLength
	{
		get => _jmaLength.Value;
		set => _jmaLength.Value = value;
	}

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

	/// <summary>
	/// Trade in direction of the signal.
	/// </summary>
	public bool DirectMode
	{
		get => _directMode.Value;
		set => _directMode.Value = value;
	}

	/// <summary>
	/// Stop loss percent.
	/// </summary>
	public decimal StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

	/// <summary>
	/// Take profit percent.
	/// </summary>
	public decimal TakeProfit
	{
		get => _takeProfit.Value;
		set => _takeProfit.Value = value;
	}

	public ColorJsatlDigitStrategy()
	{
		_jmaLength = Param(nameof(JmaLength), 30)
			.SetGreaterThanZero()
			.SetDisplay("JMA Length", "JMA period length", "Parameters")
			
			.SetOptimize(10, 60, 5);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Indicator timeframe", "Parameters");

		_directMode = Param(nameof(DirectMode), true)
			.SetDisplay("Direct Mode", "Trade in direction of signal", "Parameters");

		_stopLoss = Param(nameof(StopLoss), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss %", "Stop loss percent", "Risk Management")
			
			.SetOptimize(0.5m, 5m, 0.5m);

		_takeProfit = Param(nameof(TakeProfit), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit %", "Take profit percent", "Risk Management")
			
			.SetOptimize(1m, 10m, 1m);
	}

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

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

		_prevJma = null;
		_prevPrevJma = null;
	}

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

		_prevJma = null;
		_prevPrevJma = null;

		var jma = new JurikMovingAverage { Length = JmaLength };

		var sub = SubscribeCandles(CandleType);
		sub.Bind(jma, ProcessCandle).Start();

		StartProtection(
			takeProfit: new Unit(TakeProfit * 100m, UnitTypes.Percent),
			stopLoss: new Unit(StopLoss * 100m, UnitTypes.Percent));
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevPrevJma = _prevJma;
			_prevJma = jmaValue;
			return;
		}

		if (_prevJma is decimal prev && _prevPrevJma is decimal prev2)
		{
			var turnUp = prev > prev2 && jmaValue >= prev;
			var turnDown = prev < prev2 && jmaValue <= prev;

			if (DirectMode)
			{
				if (turnUp && Position <= 0)
					BuyMarket();
				else if (turnDown && Position >= 0)
					SellMarket();
			}
			else
			{
				if (turnDown && Position <= 0)
					BuyMarket();
				else if (turnUp && Position >= 0)
					SellMarket();
			}
		}

		_prevPrevJma = _prevJma;
		_prevJma = jmaValue;
	}
}