Backtrader 指標指南

Backtrader MACD Momentum Trading Strategy

indicatorData.macd.description

bt.indicators.MACDMomentum Trading Strategies

indicatorData.macd.description

indicatorData.macd.strategyContext

了解更多關於 Momentum Trading Strategies →
Pythonbacktrader
import backtrader as bt

class MACDMomentum(bt.Strategy):
    params = (('fast', 12), ('slow', 26), ('signal', 9))

    def __init__(self):
        self.macd = bt.indicators.MACD(self.data.close,
                                        period_me1=self.p.fast,
                                        period_me2=self.p.slow,
                                        period_signal=self.p.signal)

    def next(self):
        # MACD line crosses above signal line = bullish momentum
        if self.macd.macd[0] > self.macd.signal[0] and self.macd.macd[-1] <= self.macd.signal[-1]:
            self.buy()
        # MACD line crosses below signal line = bearish momentum
        elif self.macd.macd[0] < self.macd.signal[0] and self.macd.macd[-1] >= self.macd.signal[-1]:
            self.sell()
參數默認值描述
period_me112indicatorData.macd.params.period_me1
period_me226indicatorData.macd.params.period_me2
period_signal9indicatorData.macd.params.period_signal