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分足
  • フィルター
    • カテゴリ:グリッド、平均回帰
    • 方向:両方
    • インジケーター:CCI(オプション)
    • ストップ:ブレークイーブン
    • 複雑さ:上級
    • 時間軸:イントラデイ
    • 季節性:いいえ
    • ニューラルネットワーク:いいえ
    • ダイバージェンス:いいえ
    • リスクレベル:高

パラメーター

  • TakeProfit – バスケットの価格単位での利益目標。
  • UseAverageTakeProfit – オープン注文の平均価格からテイクプロフィットを計算。
  • DynamicTakeProfitTpDynamicStep 後に TpDecreaseFactor を使用してテイクプロフィットを削減、MinTakeProfit が下限。
  • BreakevenClose / BreakevenStep – 指定ステップ数後に目標をブレークイーブンに移動。
  • FirstStep – グリッドレベル間の初期距離。
  • DynamicStep, StepIncreaseStep, StepIncreaseFactor – 追加注文ごとにステップを増加。
  • MaxStepsBuy / MaxStepsSell – 方向ごとの最大注文数。
  • FirstVolume, VolumeMultiplier, MultiplyUseStep – グリッドのボリューム成長を制御。
  • CciFilter / CciPeriod – 最初の注文のためのオプションCCIフィルター。
  • AllowBuy / AllowSell – 取引方向を有効化。
  • CandleType – 計算用の足の時間軸。

この戦略はポジションを平均化して動的な利益目標でバスケットを決済することにより、 価格の平均回帰を捉えることを目的としています。

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