在 GitHub 上查看

区域MACD策略

策略概述

区域MACD策略通过比较MACD主线在一定历史窗口内的正负面积来判断市场多空优势。策略会累积所有为正的MACD数值以及所有为负数值的绝对值之和,并根据哪一侧更大来决定做多或做空。同时提供反向开关,可在需要时反向交易。

实现基于StockSharp的高级API,通过烛线订阅和指标绑定运行,仅处理已经完成的K线,并在ProcessCandle处理函数中集中所有交易逻辑。

指标与数据

  • MACD指标:可配置快线、慢线以及信号线周期。
  • K线数据:默认使用30分钟周期,可按需求调整。

交易规则

  1. 做多信号:当正向MACD面积大于负向面积的绝对值时开多;若启用反向模式则条件取反。
  2. 做空信号:当负向MACD面积的绝对值占优时开空;反向模式同样对调条件。
  3. 持仓管理:每次产生新的方向信号时会先平掉反向仓位,确保任意时刻仅持有一个方向的仓位。

风险控制

  • 止损:以点值(pip)表示的固定距离,从入场价计算,并根据标的的价格最小变动单位自动换算成实际价格。
  • 止盈:同样以点值设置固定利润目标。
  • 移动止损:当持仓盈利超过TrailingStopPips + TrailingStepPips时激活。随后止损价位保持与价格相隔TrailingStopPips点,并且只有当价格继续前进至少TrailingStepPips点时才会重新上移。要启用该功能,两个参数都必须大于零。

参数说明

参数 说明 默认值
OrderVolume 每次下单的数量。 1
HistoryLength 用于统计MACD面积的K线数量。 60
MacdFastLength MACD快线周期。 12
MacdSlowLength MACD慢线周期。 26
MacdSignalLength MACD信号线周期。 9
ReverseSignals 启用后反转多空条件。 false
StopLossPips 止损距离(点)。 100
TakeProfitPips 止盈距离(点)。 150
TrailingStopPips 移动止损距离(点)。设置为0可关闭。 5
TrailingStepPips 每次更新移动止损所需的额外前进距离。设置为0可关闭。 5
CandleType 使用的K线周期。 30分钟K线

使用提示

  1. 将策略连接到具体证券和投资组合后,根据交易品种调整参数。
  2. 若需启用移动止损,请确保TrailingStopPipsTrailingStepPips均大于零;否则仅使用固定止损和止盈。
  3. 日志会输出止损、止盈及移动止损触发信息,便于监控策略行为。

策略来源

本策略基于MetaTrader 5的“Area MACD”专家顾问改写,保留了比较MACD面积的核心思想,并使用StockSharp框架实现风控与指标处理。

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>
/// Area MACD strategy (simplified). Tracks cumulative positive/negative
/// areas of fast-slow EMA difference to determine trend direction.
/// </summary>
public class AreaMacdStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _historyLength;

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public int FastLength
	{
		get => _fastLength.Value;
		set => _fastLength.Value = value;
	}

	public int SlowLength
	{
		get => _slowLength.Value;
		set => _slowLength.Value = value;
	}

	public int HistoryLength
	{
		get => _historyLength.Value;
		set => _historyLength.Value = value;
	}

	public AreaMacdStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candles", "General");

		_fastLength = Param(nameof(FastLength), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Length", "Fast EMA period", "Indicators");

		_slowLength = Param(nameof(SlowLength), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Length", "Slow EMA period", "Indicators");

		_historyLength = Param(nameof(HistoryLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("History Length", "Area accumulation window", "Indicators");
	}

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

		var fastEma = new ExponentialMovingAverage { Length = FastLength };
		var slowEma = new ExponentialMovingAverage { Length = SlowLength };

		var diffHistory = new Queue<decimal>();
		decimal posArea = 0, negArea = 0;

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastEma, slowEma, (ICandleMessage candle, decimal fastValue, decimal slowValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				var diff = fastValue - slowValue;

				diffHistory.Enqueue(diff);
				if (diff > 0) posArea += diff;
				else negArea += Math.Abs(diff);

				if (diffHistory.Count > HistoryLength)
				{
					var old = diffHistory.Dequeue();
					if (old > 0) posArea -= old;
					else negArea -= Math.Abs(old);
				}

				if (!IsFormedAndOnlineAndAllowTrading())
					return;

				if (diffHistory.Count < HistoryLength)
					return;

				// Bullish area dominates
				if (posArea > negArea * 1.25m && Position <= 0)
					BuyMarket();
				// Bearish area dominates
				else if (negArea > posArea * 1.25m && Position >= 0)
					SellMarket();
			})
			.Start();

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