在 GitHub 上查看

DoubleUp2 CCI MACD 策略

DoubleUp2 是一种结合商品通道指数 (CCI) 和 MACD 的马丁格尔策略。 当两个指标都处于极端正值时开空;当两个指标都处于极端负值时开多。 若上一次交易亏损,仓位规模会加倍以试图弥补损失。 当价格向盈利方向移动固定点数时,策略将平仓。

细节

  • 入场条件
    • 多头CCI < -ThresholdMACD < -Threshold
    • 空头CCI > ThresholdMACD > 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;
		}
	}
}