GitHub で見る

Jupiter M Strategy

Grid-based strategy translated from the MetaTrader expert "Jupiter M. 4.1.1". The algorithm builds a basket of orders using a configurable step and adapts both take profit and volume as new levels are opened.

Details

  • Entry Criteria:
    • Long: price drops by the step size and (optional) CCI < -100
    • Short: price rises by the step size and (optional) CCI > 100
  • Long/Short: Both
  • Exit Criteria: Basket reaches the calculated take profit
  • Stops: Breakeven after a specified number of steps
  • Default Values:
    • TakeProfit = 10
    • FirstStep = 20
    • FirstVolume = 0.01
    • VolumeMultiplier = 2
    • CciPeriod = 50
    • CandleType = 5 minute candles
  • Filters:
    • Category: Grid, mean reversion
    • Direction: Both
    • Indicators: CCI (optional)
    • Stops: Breakeven
    • Complexity: Advanced
    • Timeframe: Intraday
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: High

Parameters

  • TakeProfit – profit target in price units for the basket.
  • UseAverageTakeProfit – calculate take profit from average price of open orders.
  • DynamicTakeProfit – reduce take profit after TpDynamicStep using TpDecreaseFactor with a floor at MinTakeProfit.
  • BreakevenClose / BreakevenStep – move target to breakeven after a number of steps.
  • FirstStep – initial distance between grid levels.
  • DynamicStep, StepIncreaseStep, StepIncreaseFactor – increase step for each additional order.
  • MaxStepsBuy / MaxStepsSell – maximum number of orders per direction.
  • FirstVolume, VolumeMultiplier, MultiplyUseStep – control volume growth in the grid.
  • CciFilter / CciPeriod – optional Commodity Channel Index filter for first order.
  • AllowBuy / AllowSell – enable trading directions.
  • CandleType – candle timeframe for calculations.

The strategy aims to capture price mean reversion by averaging into positions and closing the basket at dynamic profit targets.

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;
	}
}