GitHub で見る

DoubleUp2 CCI MACD 戦略

DoubleUp2 は Commodity Channel Index (CCI) と MACD を組み合わせたマーチンゲール型の戦略です。 両方のインジケーターが極端な正の値を示すとショートポジションを開き、両方が極端に負の場合はロングポジションを開きます。 負けトレードの後はポジションサイズが倍増し、以前の損失を取り戻そうとします。 利益の出たトレードは価格が固定のポイント数だけ進んだ時点で閉じられます。

詳細

  • エントリー条件:
    • ロング: CCI < -Threshold かつ MACD < -Threshold
    • ショート: CCI > Threshold かつ MACD > Threshold
  • ロング/ショート: 両方。
  • エグジット条件:
    • 反対シグナルまたは価格が利益方向に ExitDistance ポイント動く。
  • ストップ: 明示的なストップロスなし。
  • デフォルト値:
    • CCI Period = 8
    • MACD Fast = 13
    • MACD Slow = 33
    • MACD Signal = 2
    • Threshold = 230
    • Base Volume = 0.1
    • ExitDistance = 120 * price step
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: CCI, MACD
    • ストップ: いいえ
    • 複雑さ: 中程度
    • 時間軸: 短期
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 高
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// DoubleUp2 strategy combining CCI and MACD with volume doubling (martingale).
/// </summary>
public class DoubleUp2Strategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _macdFastPeriod;
	private readonly StrategyParam<int> _macdSlowPeriod;
	private readonly StrategyParam<decimal> _threshold;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;
	private int _martingaleStep;

	/// <summary>
	/// CCI period.
	/// </summary>
	public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }

	/// <summary>
	/// MACD fast EMA period.
	/// </summary>
	public int MacdFastPeriod { get => _macdFastPeriod.Value; set => _macdFastPeriod.Value = value; }

	/// <summary>
	/// MACD slow EMA period.
	/// </summary>
	public int MacdSlowPeriod { get => _macdSlowPeriod.Value; set => _macdSlowPeriod.Value = value; }

	/// <summary>
	/// Threshold for CCI and MACD signals.
	/// </summary>
	public decimal Threshold { get => _threshold.Value; set => _threshold.Value = value; }

	/// <summary>
	/// Candle type for calculations.
	/// </summary>
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	/// <summary>
	/// Constructor.
	/// </summary>
	public DoubleUp2Strategy()
	{
		_cciPeriod = Param(nameof(CciPeriod), 8)
			.SetDisplay("CCI Period", "Averaging period for CCI", "Indicators")
			.SetOptimize(4, 20, 1);

		_macdFastPeriod = Param(nameof(MacdFastPeriod), 13)
			.SetDisplay("MACD Fast", "Fast EMA period for MACD", "Indicators")
			.SetOptimize(5, 20, 1);

		_macdSlowPeriod = Param(nameof(MacdSlowPeriod), 33)
			.SetDisplay("MACD Slow", "Slow EMA period for MACD", "Indicators")
			.SetOptimize(20, 50, 1);

		_threshold = Param(nameof(Threshold), 70m)
			.SetDisplay("Threshold", "RSI extreme level", "Strategy")
			.SetOptimize(55m, 85m, 5m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles used for calculations", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0m;
		_martingaleStep = 0;
	}

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

		var rsi = new RelativeStrengthIndex { Length = CciPeriod };
		var macd = new MovingAverageConvergenceDivergence(
			new ExponentialMovingAverage { Length = MacdSlowPeriod },
			new ExponentialMovingAverage { Length = MacdFastPeriod });

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

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

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

		var step = Security?.PriceStep ?? 1m;
		var lowThreshold = 100m - Threshold;

		// Short entry condition
		if (rsiValue > Threshold && macdValue > 0m)
		{
			if (Position > 0)
			{
				var profit = candle.ClosePrice - _entryPrice;
				_martingaleStep = profit > 0m ? 0 : _martingaleStep + 1;
			}

			SellMarket();
			_entryPrice = candle.ClosePrice;
			return;
		}

		// Long entry condition
		if (rsiValue < lowThreshold && macdValue < 0m)
		{
			if (Position < 0)
			{
				var profit = _entryPrice - candle.ClosePrice;
				_martingaleStep = profit > 0m ? 0 : _martingaleStep + 1;
			}

			BuyMarket();
			_entryPrice = candle.ClosePrice;
			return;
		}

		// Exit profitable long position
		if (Position > 0 && candle.ClosePrice - _entryPrice > 120m * step)
		{
			SellMarket();
			_martingaleStep += 2;
			return;
		}

		// Exit profitable short position
		if (Position < 0 && _entryPrice - candle.ClosePrice > 120m * step)
		{
			BuyMarket();
			_martingaleStep += 2;
		}
	}
}