在 GitHub 上查看

MA2CCI Classic 策略

MA2CCI 策略移植自 MetaTrader 顾问,核心由两条简单移动平均线(SMA)与商品通道指数(CCI)组合而成。CCI 的零轴过滤用于确认方向,平均真实波幅(ATR)提供保护性止损距离,因此策略保持趋势跟随特性并能快速响应反转。

StockSharp 版本在保留原始入场和出场条件的同时,将资金管理改写为 .NET 风格。头寸规模采用“每千单位风险”规则,并在连续亏损后按线性比例削减仓位。每次开仓都会根据最新 ATR 设置一倍 ATR 的波动止损,以模拟 MQL 中的行为。

交易逻辑

  • 指标
    • 快速 SMA,默认周期 4。
    • 慢速 SMA,默认周期 8。
    • CCI 过滤,默认周期 4。
    • ATR 周期 4,用于止损距离。
  • 入场条件
    • 做多:快速 SMA 向上穿越慢速 SMA,且前一根已完成 K 线中的 CCI 由负转正。
    • 做空:快速 SMA 向下穿越慢速 SMA,且前一根 K 线中的 CCI 由正转负。
  • 出场条件
    • 反向 SMA 交叉立即平仓,即便没有新方向的信号。
    • ATR 止损:多单跌至 entry - ATR 平仓,空单涨至 entry + ATR 平仓。

风险控制

  • 基础下单量可配置,默认值为 0.1(按交易所合约最小单位转换)。
    • 若投资组合提供资金数据,可根据 可用资金 * MaxRiskPerThousand / 1000 动态放大仓位。
  • 当连续亏损次数大于 1 时,仓位按 losses / DecreaseFactor 的比例削减。
  • 波动止损基于收盘价评估;当盘中触碰止损区间时,策略会在下一次处理时以市价平仓。

参数

名称 说明 默认值
CandleType 执行信号与指标的工作周期。 1 小时 K 线
OrderVolume 无法计算风险仓位时使用的最小下单量。 0.1
FastMaPeriod 快速 SMA 周期。 4
SlowMaPeriod 慢速 SMA 周期。 8
CciPeriod CCI 滤波周期。 4
AtrPeriod ATR 止损周期。 4
MaxRiskPerThousand 每千单位资金用于单笔交易的风险比例。 0.02
DecreaseFactor 连续亏损后的仓位缩减因子。 3

说明

  1. 策略仅在 K 线收盘时计算信号,相当于原版使用 Volume[0] > 1 的机制,避免同一根 K 线重复执行。
  2. 止损在策略内部模拟,通过提交市价单离场,与原始 EA 的行为一致。
  3. 在 StockSharp Designer 中启用图表可视化,方便查看 SMA、CCI 与成交记录。
using System;
using System.Collections.Generic;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// MA2CCI Classic strategy - dual SMA crossover with CCI zero-line filter.
/// Buys when fast SMA crosses above slow SMA and CCI above zero.
/// Sells when fast SMA crosses below slow SMA and CCI below zero.
/// </summary>
public class Ma2CciClassicStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Ma2CciClassicStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetDisplay("Fast SMA", "Fast SMA period", "Indicators");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetDisplay("Slow SMA", "Slow SMA period", "Indicators");
		_cciPeriod = Param(nameof(CciPeriod), 14)
			.SetDisplay("CCI Period", "CCI lookback", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
	protected override void OnReseted() { base.OnReseted(); _prevFast = 0m; _prevSlow = 0m; _hasPrev = false; }

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_hasPrev = false;

		var fast = new SimpleMovingAverage { Length = FastPeriod };
		var slow = new SimpleMovingAverage { Length = SlowPeriod };
		var cci = new CommodityChannelIndex { Length = CciPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(fast, slow, cci, ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow, decimal cci)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_hasPrev) { _prevFast = fast; _prevSlow = slow; _hasPrev = true; return; }

		if (_prevFast <= _prevSlow && fast > slow && cci > 0 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (_prevFast >= _prevSlow && fast < slow && cci < 0 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
		_prevFast = fast; _prevSlow = slow;
	}
}