在 GitHub 上查看

MACD 1 MIN SCALPER 策略

该策略是 MetaTrader 专家顾问 “MACD 1 MIN SCALPER” 的 C# 移植版本。它在进场前联合使用加权移动平均线、多周期 MACD 过滤以及动量强度检测,目的是当多个周期的趋势一致并且价格动能充足时顺势交易。

交易逻辑

  1. 基础周期 – 可配置,默认 M1。利用两个周期分别为 50 与 200 的加权移动平均线(WMA),按典型价 (高+低+收)/3 计算,用于判定短期趋势。
  2. 高周期趋势过滤 – 在 H1 周期上计算相同参数的 WMA。做多需要两个高周期 WMA 均在慢线之上,做空则需要位于下方。如果工作周期已是 H1,则直接复用基础 WMA。
  3. MACD 确认 – MACD(12,26,9) 的主线必须同时在基础周期、H1 周期以及约 43200 分钟的月度周期上位于信号线之上才允许做多。做空则要求三组 MACD 均位于信号线下方。
  4. 动量过滤 – 动量指标周期为 14,运行在根据 MetaTrader 规则推导的更高周期上(例如 M1→M15,M5→M30 等)。最近三个已完成柱中至少有一个的动量绝对偏离 100 的数值需大于设定阈值。
  5. 入场规则 – 当所有做多条件满足且当前没有多头敞口时买入。做空采用镜像条件。若存在反向持仓,订单数量会自动包含平仓所需的手数。
  6. 风险控制 – 可选的止损/止盈距离以点数(pip)表示,在启动时转换为交易品种的最小价格波动单位。原始脚本中的移动止损、保本与资金管理模块在该高阶版本中未实现。

参数说明

参数 说明
CandleType 基础指标所使用的周期。
OrderVolume 每次市价单使用的手数,同时用于反向翻仓。
FastMaPeriod / SlowMaPeriod 快慢加权移动平均的长度。
MacdFastPeriod / MacdSlowPeriod / MacdSignalPeriod MACD 的快速、慢速与信号 EMA 周期。
MomentumPeriod 动量指标的周期。
MomentumThreshold 动量绝对偏离 100 的最小阈值。
TakeProfitPips / StopLossPips 可选的止盈 / 止损距离(点数)。

实现细节

  • 策略完全依赖 StockSharp 的高级蜡烛订阅(SubscribeCandles)与指标绑定(Bind/BindEx),不手动保存历史或自行计算指标。
  • 动量使用的周期来源于 MetaTrader 常见周期序列 [1,5,15,30,60,240,1440,10080,43200]。若基础周期不在列表中,则退回为原周期的四倍。
  • 仅当止损或止盈大于零时才会启动 StartProtection。当前版本不包含移动止损实现。
  • 为了便于调试,默认会在图表上绘制基础蜡烛、两条 WMA 以及 MACD。

使用建议

  • 根据交易品种的最小交易单位设置 OrderVolume。策略会自动将下单手数调整到合规的步长范围内。
  • 确保数据源提供 H1 与月度蜡烛,否则由于缺少确认信号策略不会开仓。
  • MomentumThreshold 越高,需要的动量越强,信号越少;阈值越低则更频繁地产生交易。
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;

public class Macd1MinScalperStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public Macd1MinScalperStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}