在 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 – 网格之间的初始距离。
DynamicStep、StepIncreaseStep、StepIncreaseFactor – 为每个新增订单增加步长。
MaxStepsBuy / MaxStepsSell – 每个方向的最大订单数。
FirstVolume、VolumeMultiplier、MultiplyUseStep – 控制网格中的手数增长。
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;
}
}
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()