The Moving Average Convergence Divergence (MACD) is a trend-following momentum indicator that shows the relationship between two exponential moving averages of a security’s price.
| Parameter | Type | Default | Description |
|---|---|---|---|
fastPeriod |
int | 12 | The period for the fast EMA |
slowPeriod |
int | 26 | The period for the slow EMA |
signalPeriod |
int | 9 | The period for the signal line EMA |
[fastPeriod, slowPeriod, signalPeriod]
Example:
[12, 26, 9]
The signal’s RawValue field contains a JSON object with the following keys:
| Key | Type | Description |
|---|---|---|
macd |
float64 | The MACD line value (fast EMA - slow EMA) |
Example:
{"macd": 2.35}
| Condition | Signal Type | Description |
|---|---|---|
| MACD > 0 | SIGNAL_TYPE_BUY_LONG |
Bullish momentum |
| MACD < 0 | SIGNAL_TYPE_SELL_SHORT |
Bearish momentum |
| MACD = 0 | SIGNAL_TYPE_NO_ACTION |
Neutral |
The indicator uses the EMA indicator internally for calculations.
func (s *MyStrategy) Initialize(_ context.Context, req *strategy.InitializeRequest) (*emptypb.Empty, error) {
api := strategy.NewStrategyApi()
// Configure MACD with default parameters
_, err := api.ConfigureIndicator(context.Background(), &strategy.ConfigureRequest{
IndicatorType: strategy.IndicatorType_INDICATOR_MACD,
Config: `[12, 26, 9]`,
})
if err != nil {
return nil, fmt.Errorf("failed to configure MACD: %w", err)
}
return &emptypb.Empty{}, nil
}
func (s *MyStrategy) ProcessData(ctx context.Context, req *strategy.ProcessDataRequest) (*emptypb.Empty, error) {
data := req.Data
api := strategy.NewStrategyApi()
// Get MACD signal
signal, err := api.GetSignal(ctx, &strategy.GetSignalRequest{
IndicatorType: strategy.IndicatorType_INDICATOR_MACD,
MarketData: data,
})
if err != nil {
return nil, fmt.Errorf("failed to get MACD signal: %w", err)
}
// Parse raw value
var macdValue struct {
MACD float64 `json:"macd"`
}
if err := json.Unmarshal([]byte(signal.RawValue), &macdValue); err != nil {
return nil, fmt.Errorf("failed to parse MACD value: %w", err)
}
// Use MACD signal and value
switch signal.Type {
case strategy.SignalType_SIGNAL_TYPE_BUY_LONG:
// MACD positive - bullish momentum
fmt.Printf("MACD bullish: %.4f\n", macdValue.MACD)
case strategy.SignalType_SIGNAL_TYPE_SELL_SHORT:
// MACD negative - bearish momentum
fmt.Printf("MACD bearish: %.4f\n", macdValue.MACD)
}
return &emptypb.Empty{}, nil
}
| Signal | Interpretation |
|---|---|
| MACD crosses above 0 | Bullish - fast EMA > slow EMA |
| MACD crosses below 0 | Bearish - fast EMA < slow EMA |
| MACD crosses above signal line | Bullish entry signal |
| MACD crosses below signal line | Bearish exit signal |
| Rising histogram | Increasing bullish momentum |
| Falling histogram | Increasing bearish momentum |
| Style | Fast | Slow | Signal | Use Case |
|---|---|---|---|---|
| Standard | 12 | 26 | 9 | General purpose |
| Fast | 8 | 17 | 9 | More responsive, short-term |
| Slow | 19 | 39 | 9 | Less noise, long-term |