I am trying use the backtesting.py package. I had difficulty with talib installation. So using pandas_ta instead.
from backtesting import Backtest, Strategy
import yfinance as yf
import pandas as pd
import pandas_ta as ta
from backtesting.lib import crossover
TICKER = 'AAPL'
START = '2020-01-01'
df = pd.DataFrame()
df = yf.download(TICKER, START)
class RsiOscillator(Strategy):
upper_bound = 70
lower_bound = 30
def init(self):
self.rsi = self.I(ta.rsi, self.data.Close, 14)
# I --> how we are going to build indicators within the framework
def next(self):
if crossover(self.rsi, self.upper_bound):
self.position.close()
elif crossover(self.lower_bound, self.rsi):
self.buy()
bt = Backtest(df, RsiOscillator, cash = 10000)
stats = bt.run()
This code is available here. But returns Error:
ValueError: Indicators must return (optionally a tuple of)
numpy.arrays of same length asdata
(data shape: (1105,); indicator
“rsi(C,14)”shape: , returned value: None)
What am I doing wrong?