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;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class jupiter_m_strategy(Strategy):
def __init__(self):
super(jupiter_m_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 50) \
.SetDisplay("CCI Period", "Period for CCI indicator", "Indicators")
self._buy_level = self.Param("BuyLevel", -100.0) \
.SetDisplay("Buy Level", "CCI level to buy (cross above)", "Trading")
self._sell_level = self.Param("SellLevel", 100.0) \
.SetDisplay("Sell Level", "CCI level to sell (cross below)", "Trading")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for analysis", "General")
self._prev_cci = None
@property
def cci_period(self):
return self._cci_period.Value
@property
def buy_level(self):
return self._buy_level.Value
@property
def sell_level(self):
return self._sell_level.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(jupiter_m_strategy, self).OnReseted()
self._prev_cci = None
def OnStarted2(self, time):
super(jupiter_m_strategy, self).OnStarted2(time)
self._prev_cci = None
cci = CommodityChannelIndex()
cci.Length = self.cci_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(cci, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, cci)
self.DrawOwnTrades(area)
def process_candle(self, candle, cci):
if candle.State != CandleStates.Finished:
return
cci = float(cci)
if self._prev_cci is None:
self._prev_cci = cci
return
buy_level = float(self.buy_level)
sell_level = float(self.sell_level)
if self._prev_cci <= buy_level and cci > buy_level and self.Position <= 0:
self.BuyMarket()
elif self._prev_cci >= sell_level and cci < sell_level and self.Position >= 0:
self.SellMarket()
self._prev_cci = cci
def CreateClone(self):
return jupiter_m_strategy()