Can you tell me why an error occurs when establish a coupling between indicators that operate on datas with different timeframes
class MyStrategy(bt.Strategy):
params = dict(period=20)
def __init__(self):
# data0 is a daily data
sma0 = btind.SMA(self.data, period=10) # sma from the minutes
# data1 is a weekly data
sma1 = btind.SMA(self.data1, period=10) # sma from the daily timeframe
self.buysig = sma0 > sma1() # minute sma greater than day sma1
def next(self):
if self.buysig[0]:
print('daily sma is greater than weekly sma1')
datafile = "GAZP-1.csv"
current_dir = os.path.dirname(__file__)
file_path = os.path.join(
current_dir, "historical_data", datafile
)
datafile1 = "GAZP-24.csv"
file_path1 = os.path.join(
current_dir, "historical_data", datafile1
)
if __name__ == "__main__":
start_time = time.time()
# Create a cerebro entity
cerebro = bt.Cerebro(runonce=True)
cerebro.addstrategy(MyStrategy)
data = bt.feeds.GenericCSVData(
dataname=file_path,
separator="t",
dtformat="%d.%m.%Y %H:%M:%S",
openinterest=-1,
fromdate=datetime(2023, 6, 10),
todate=datetime(2024, 6, 28),
)
data1 = bt.feeds.GenericCSVData(
dataname=file_path1,
separator="t",
dtformat="%d.%m.%Y %H:%M:%S",
openinterest=-1,
fromdate=datetime(2023, 6, 10),
todate=datetime(2024, 6, 28),
)
cerebro.adddata(data)
cerebro.adddata(data1)
cerebro.broker.setcash(100000.0)
print("Starting Portfolio Value: %.2f" % cerebro.broker.getvalue())
cerebro.run()
print("Final Portfolio Value: %.2f" % cerebro.broker.getvalue())
I get the following error
dst[i] = op(srca[i], srcb[i])
~~~~^^^
IndexError: array index out of range
I’m following the instructions https://www.backtrader.com/docu/concepts/#lines-delayed-indexing
explain my mistake, the most important thing is to understand the cause. Thank you
Sergey Tunkin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.