在 GitHub 上查看

MA2CCI Adaptive Volume 策略

概述

MA2CCI 策略直接移植自 MetaTrader 4 智能交易系统“MA2CCI.mq4”。该系统利用快慢两条简单移动平均线 (SMA) 的金叉/死叉,并要求商品通道指数 (CCI) 同时穿越零轴作为确认信号。每次满足条件时都会开立单向市价仓位,并根据平均真实波幅 (ATR) 设置波动性止损。仓位管理遵循原始脚本的逻辑:按账户权益动态调整下单手数,并在连续亏损后削减仓位。

指标与数据

  • 快/慢 SMA:在所选周期内侦测趋势变化。
  • CCI:使用同一价格序列确认动量方向是否越过零轴。
  • ATR:衡量近期波动率,从而确定止损距离。
  • K 线:默认使用 15 分钟周期,为所有指标提供输入数据。

交易规则

  • 做多:快线从下往上穿越慢线,CCI 同时由负转正,且当前无持仓。立即买入,并将止损设置为 收盘价 − ATR × AtrMultiplier
  • 做空:快线从上往下跌破慢线,CCI 同时由正转负,且当前无持仓。立即卖出,并将止损设置为 收盘价 + ATR × AtrMultiplier
  • 多头平仓:当快线重新跌破慢线时,市价平掉所有多头,并撤销保护性止损。
  • 空头平仓:当快线重新上穿慢线时,市价平掉所有空头,并撤销保护性止损。
  • 止损:每次新开仓都会重新创建一个条件止损单,仓位关闭或反向时会自动取消,避免遗留挂单。

仓位管理

  • 基础下单手数由 BaseVolume 参数控制(默认 0.1 手)。
  • RiskFraction 大于 0 时,会额外计算 equity × RiskFraction / 1000 的手数,模拟 MQL4 中 AccountFreeMargin 的计算方式,并取基础手数与风险手数中的较大者。
  • 如果出现两次及以上的连续亏损,手数会按 volume × losses / DecreaseFactor 减少,对应原策略的 DcF 机制。
  • 最终手数会按合约的 VolumeStep 自动对齐。

参数

名称 默认值 说明
FastMaPeriod 4 快速 SMA 周期。
SlowMaPeriod 8 慢速 SMA 周期。
CciPeriod 4 CCI 计算周期。
AtrPeriod 4 ATR 周期,用于确定止损距离。
AtrMultiplier 1.0 ATR 止损距离的乘数。
BaseVolume 0.1 风险调整前的最小手数。
RiskFraction 0.02 参与风险公式的权益比例(每 1000 货币单位)。
DecreaseFactor 3 连续亏损后缩减手数的除数。
CandleType 15 分钟 K 线 指标与信号使用的时间框架。

说明

  • 原脚本中的邮件通知 (SndMl) 未被实现。
  • 策略始终只会持有一个方向的仓位,与源代码保持一致。
  • 当仓位关闭或反向时会重新生成/取消止损单,避免残留未使用的订单。
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 Adaptive Volume strategy - dual EMA crossover with CCI filter.
/// Buys when fast EMA crosses above slow EMA and CCI is above zero.
/// Sells when fast EMA crosses below slow EMA and CCI is below zero.
/// </summary>
public class Ma2CciAdaptiveVolumeStrategy : 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 Ma2CciAdaptiveVolumeStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 8)
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
		_slowPeriod = Param(nameof(SlowPeriod), 21)
			.SetDisplay("Slow EMA", "Slow EMA 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 ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { 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;
	}
}