Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in fetching MACD details in back testing #1174

Open
Nitin365-ai opened this issue Oct 3, 2024 · 0 comments
Open

Error in fetching MACD details in back testing #1174

Nitin365-ai opened this issue Oct 3, 2024 · 0 comments

Comments

@Nitin365-ai
Copy link

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant