pandas_ta for backtesting.py returning Null using bbands

196 views Asked by At

I am trying to backtest using backtesting.py and pandas_ta for a simple bollinger bands strategy. When I init the strategy i am getting an empty shape.Please note I'm not concerned about the next portion atm

import pandas as pd
import numpy as np
import pandas_ta as ta
from backtesting import Backtest, Strategy
from backtesting.test import GOOG
import matplotlib.pyplot as plt

class BBandsStrat(Strategy):
    time_period = 20
    dev = 2

    def init(self): 
        print(self.data.Close) 
        self.bbands = self.I(ta.bbands, self.data.Close, length=self.time_period, std=self.dev,append=False)

    def next(self):
        lower_band = self.bbands['BBL_20_2.0']
        upper_band = self.bbands['BBU_20_2.0']

        if self.position:
            if self.data.Close[-1] > upper_band[-1]:
                self.position.close()
        else:
            print(self.data.Close[-1], lower_band[-1])
            if self.data.Close[-1] < lower_band[-1]:
                self.buy()
cash = 10_000
bt = Backtest(GOOG, BBandsStrat, cash=cash)
stats = bt.run()

When i run this i get the following error:

ValueError: Indicators must return (optionally a tuple of) numpy.arrays of same length as `data` (data shape: (2148,); indicator "bbands(C,20,2,False)"shape: , returned value: None)

I have confirmed that self.data.Close in the init function returns values as well as confirming that I get an output when i run:

bbands = ta.bbands(GOOG.Close, length=20, std=2, append=False)

At this stage I am unsure as to what I am needing to do to fix this. Thank you

1

There are 1 answers

0
K3---rnc On

Following published source code, the value might be None if np.asarray(bbands) raises.