在 GitHub 上查看

Jupiter M 策略

基于 MetaTrader 专家 “Jupiter M. 4.1.1” 的网格策略。 算法使用可配置的步长构建订单篮,并在开启新层时调整 每个订单的止盈和手数。

细节

  • 入场条件
    • 多头:价格下跌达到步长并且(可选)CCI < -100
    • 空头:价格上涨达到步长并且(可选)CCI > 100
  • 方向:双向
  • 出场条件:订单篮达到计算的止盈
  • 止损:在指定阶数后移动到盈亏平衡
  • 默认值
    • TakeProfit = 10
    • FirstStep = 20
    • FirstVolume = 0.01
    • VolumeMultiplier = 2
    • CciPeriod = 50
    • CandleType = 5 分钟K线
  • 过滤器
    • 分类:网格,均值回归
    • 方向:双向
    • 指标:CCI(可选)
    • 止损:盈亏平衡
    • 复杂度:高级
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:高

参数

  • TakeProfit – 订单篮的价格单位止盈目标。
  • UseAverageTakeProfit – 基于所有订单的平均价计算止盈。
  • DynamicTakeProfit – 在 TpDynamicStep 之后使用 TpDecreaseFactor 递减止盈,最低不低于 MinTakeProfit
  • BreakevenClose / BreakevenStep – 在指定阶数后把目标移动到盈亏平衡。
  • FirstStep – 网格之间的初始距离。
  • DynamicStepStepIncreaseStepStepIncreaseFactor – 为每个新增订单增加步长。
  • MaxStepsBuy / MaxStepsSell – 每个方向的最大订单数。
  • FirstVolumeVolumeMultiplierMultiplyUseStep – 控制网格中的手数增长。
  • CciFilter / CciPeriod – 第一个订单的可选 CCI 过滤。
  • AllowBuy / AllowSell – 启用交易方向。
  • CandleType – 用于计算的K线周期。

该策略通过在价格偏离时分批建仓,并在动态止盈水平处平掉整篮仓位, 以此捕捉价格向均值回归的机会。

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>
/// CCI-based grid trading strategy inspired by Jupiter M.
/// Enters on CCI level crosses, exits on opposite signal.
/// </summary>
public class JupiterMStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<decimal> _buyLevel;
	private readonly StrategyParam<decimal> _sellLevel;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevCci;

	public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
	public decimal BuyLevel { get => _buyLevel.Value; set => _buyLevel.Value = value; }
	public decimal SellLevel { get => _sellLevel.Value; set => _sellLevel.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public JupiterMStrategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 50)
			.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators")
			.SetGreaterThanZero();

		_buyLevel = Param(nameof(BuyLevel), -100m)
			.SetDisplay("Buy Level", "CCI level to buy (cross above)", "Trading");

		_sellLevel = Param(nameof(SellLevel), 100m)
			.SetDisplay("Sell Level", "CCI level to sell (cross below)", "Trading");

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

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

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

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

		_prevCci = null;

		var cci = new CommodityChannelIndex { Length = CciPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_prevCci is null)
		{
			_prevCci = cci;
			return;
		}

		// CCI crosses above buy level -> buy
		if (_prevCci <= BuyLevel && cci > BuyLevel && Position <= 0)
			BuyMarket();
		// CCI crosses below sell level -> sell
		else if (_prevCci >= SellLevel && cci < SellLevel && Position >= 0)
			SellMarket();

		_prevCci = cci;
	}
}