在 GitHub 上查看
区域MACD策略
策略概述
区域MACD策略通过比较MACD主线在一定历史窗口内的正负面积来判断市场多空优势。策略会累积所有为正的MACD数值以及所有为负数值的绝对值之和,并根据哪一侧更大来决定做多或做空。同时提供反向开关,可在需要时反向交易。
实现基于StockSharp的高级API,通过烛线订阅和指标绑定运行,仅处理已经完成的K线,并在ProcessCandle处理函数中集中所有交易逻辑。
指标与数据
- MACD指标:可配置快线、慢线以及信号线周期。
- K线数据:默认使用30分钟周期,可按需求调整。
交易规则
- 做多信号:当正向MACD面积大于负向面积的绝对值时开多;若启用反向模式则条件取反。
- 做空信号:当负向MACD面积的绝对值占优时开空;反向模式同样对调条件。
- 持仓管理:每次产生新的方向信号时会先平掉反向仓位,确保任意时刻仅持有一个方向的仓位。
风险控制
- 止损:以点值(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线 |
使用提示
- 将策略连接到具体证券和投资组合后,根据交易品种调整参数。
- 若需启用移动止损,请确保
TrailingStopPips与TrailingStepPips均大于零;否则仅使用固定止损和止盈。
- 日志会输出止损、止盈及移动止损触发信息,便于监控策略行为。
策略来源
本策略基于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);
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
from collections import deque
class area_macd_strategy(Strategy):
def __init__(self):
super(area_macd_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candles", "General")
self._fast_length = self.Param("FastLength", 12) \
.SetDisplay("Fast Length", "Fast EMA period", "Indicators")
self._slow_length = self.Param("SlowLength", 26) \
.SetDisplay("Slow Length", "Slow EMA period", "Indicators")
self._history_length = self.Param("HistoryLength", 20) \
.SetDisplay("History Length", "Area accumulation window", "Indicators")
self._diff_history = deque()
self._pos_area = 0.0
self._neg_area = 0.0
@property
def CandleType(self):
return self._candle_type.Value
@property
def FastLength(self):
return self._fast_length.Value
@property
def SlowLength(self):
return self._slow_length.Value
@property
def HistoryLength(self):
return self._history_length.Value
def OnReseted(self):
super(area_macd_strategy, self).OnReseted()
self._diff_history = deque()
self._pos_area = 0.0
self._neg_area = 0.0
def OnStarted2(self, time):
super(area_macd_strategy, self).OnStarted2(time)
self._diff_history = deque()
self._pos_area = 0.0
self._neg_area = 0.0
fast_ema = ExponentialMovingAverage()
fast_ema.Length = self.FastLength
slow_ema = ExponentialMovingAverage()
slow_ema.Length = self.SlowLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(fast_ema, slow_ema, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_ema)
self.DrawIndicator(area, slow_ema)
self.DrawOwnTrades(area)
def _on_process(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fv = float(fast_value)
sv = float(slow_value)
diff = fv - sv
self._diff_history.append(diff)
if diff > 0:
self._pos_area += diff
else:
self._neg_area += abs(diff)
if len(self._diff_history) > self.HistoryLength:
old = self._diff_history.popleft()
if old > 0:
self._pos_area -= old
else:
self._neg_area -= abs(old)
if len(self._diff_history) < self.HistoryLength:
return
if self._pos_area > self._neg_area * 1.25 and self.Position <= 0:
self.BuyMarket()
elif self._neg_area > self._pos_area * 1.25 and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return area_macd_strategy()