Skip to content

Error in fetching MACD details in back testing #1174

@Nitin365-ai

Description

@Nitin365-ai

The code should be written to get MACD Line and Signal line crossover to trigger signal.in back testing. i have tried the same but giving me IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices
for line self.macd_line = macd['MACD_12_26_9']
self.signal_line = macd['MACDs_12_26_9']

my code is as follows:

import pandas as pd
import pandas_ta as ta
from backtesting import Backtest, Strategy
from backtesting.lib import crossover

class EMA(Strategy):
def init(self):

    self.ema_10 = self.I(ta.ema, pd.Series(self.data.Close), length=10)
    self.ema_5 = self.I(ta.ema, pd.Series(self.data.Close), length=5)
    self.ema_20 = self.I(ta.ema, pd.Series(self.data.Close), length=20)
    
    # Calculate MACD
    macd = self.I(ta.macd, pd.Series(self.data.Close), fast=12, slow=26, signal=9)
    self.macd_line = macd['MACD_12_26_9']
    self.signal_line = macd['MACDs_12_26_9']

def next(self):
   
    if self.data.Close[-1] > self.ema_5[-1] and crossover(self.macd_line, self.signal_line):
        if not self.position:
            self.buy()

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis is not a (valid) bug report

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions