Datafeed: feed.csv [0_1610445720484_feed.csv](Uploading 100%)
Original timeframe of the feed is 1 minute
Objective:
- To create an indicator that uses data from two different time frames.
from backtrader import Strategy
from backtrader import Indicator
import pandas as pd
class Dummy(Indicator):
lines = ('l',)
def next(self):
# Datetime of the latest completed bars in both Time-Frames
print(self.data0.datetime.datetime(0), end=": ")
print(self.data1.datetime.datetime(0))
class TestStrategy(Strategy):
params = ()
def __init__(self):
# Initializing dummy indicator (defined above)
self.d = Dummy(self.data0, self.data1)
pass
def next(self):
# Datetime of the latest completed bars in both Time-Frames
print(self.data0.datetime.datetime(0), end=": ")
print(self.data1.datetime.datetime(0))
df = pd.read_csv('feed.csv',
index_col='timestamp',
parse_dates=True)
data = bt.feeds.PandasData(
dataname=df,
openinterest='oi',
timeframe=bt.TimeFrame.Minutes,
compression=1,
sessionstart=sessionstart,
sessionend=sessionend
)
# Creating the Cerebro instance
cerebro = bt.Cerebro(runonce=False)
# Adding Strategy: TestStrategy (defined above)
cerebro.addstrategy(TestStrategy)
# Adding feed 1 -> `1 minute` data. Smaller Time-Frame First
cerebro.adddata(data)
# Adding feed 2 -> Resampling `1 minute' to `15 minute`
cerebro.resampledata(data,
timeframe=bt.TimeFrame.Minutes,
compression=15)
# Executing
cerebro.run(stdstats=False)
cerebro.plot(style='candle', barup='green')
Output using compression 15:
2020-01-01 09:15:00: 2020-01-01 09:15:00
2020-01-01 09:15:00: 2020-01-01 09:15:00
2020-01-01 09:16:00: 2020-01-01 09:15:00
2020-01-01 09:16:00: 2020-01-01 09:15:00
2020-01-01 09:17:00: 2020-01-01 09:15:00
2020-01-01 09:17:00: 2020-01-01 09:15:00
2020-01-01 09:18:00: 2020-01-01 09:15:00
2020-01-01 09:18:00: 2020-01-01 09:15:00
2020-01-01 09:19:00: 2020-01-01 09:15:00
2020-01-01 09:19:00: 2020-01-01 09:15:00
2020-01-01 09:20:00: 2020-01-01 09:15:00
2020-01-01 09:20:00: 2020-01-01 09:15:00
2020-01-01 09:21:00: 2020-01-01 09:15:00
2020-01-01 09:21:00: 2020-01-01 09:15:00
2020-01-01 09:22:00: 2020-01-01 09:15:00
But when the compression is changed to 16, execution throws Index Error
# Adding feed 2 -> Resampling `1 minute' to `15 minute`
cerebro.resampledata(data,
timeframe=bt.TimeFrame.Minutes,
compression=16)
Output using compression 16:
File "C:\Users\PC2\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader\lineiterator.py", line 347, in nextstart
self.next()
File "C:/Users/PC2/.PyCharmCE2019.1/config/scratches/Test.py", line 10, in next
print(self.data1.datetime.datetime(0))
File "C:\Users\PC2\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader\linebuffer.py", line 387, in datetime
return num2date(self.array[self.idx + ago],
IndexError: array index out of range
Problem: Multi-Timeframe works well with compression=15 but fails with compression=16 (or any other than 15)
Observation:
self.data1.datetime.datetime(0)
works well for all Time-Frames in the next
method of Strategy
. But throws Index Error
for Time-Frames other than 15 in the next
method of Indicator
I'd be really grateful if someone can help me understand where am I going wrong.