I'm trying to resample from 1 minute to 2, 5 and 10 minutes. The only problem I'm having is when resampling, the first OHLC data I'm logging of the 2,5 and 10 minute is exactly the same as the first 1 minute OHLC data. after that it resamples properly, however it then is out of sync.
an example will make it more clear when sampling 1 minute to 2 minute data:
Output in the log:
Starting Portfolio Value: 1000000.00
2020-08-12 00:00, O: 1.17383, H: 1.17391, L: 1.17372, C: 1.17391
2020-08-12 00:02, O: 1.17391, H: 1.17408, L: 1.17384, C: 1.17384
2020-08-12 00:04, O: 1.17384, H: 1.17384, L: 1.17377, C: 1.17377
2020-08-12 00:06, O: 1.17377, H: 1.17389, L: 1.17376, C: 1.17386
dataframe info:
Open High Low Close Volume
Datetime
2020-08-12 00:00:00 1.17383 1.17391 1.17372 1.17391 23
2020-08-12 00:01:00 1.17391 1.17408 1.17390 1.17390 18
2020-08-12 00:02:00 1.17390 1.17390 1.17384 1.17384 33
2020-08-12 00:03:00 1.17384 1.17384 1.17378 1.17378 22
2020-08-12 00:04:00 1.17378 1.17378 1.17377 1.17377 25
2020-08-12 00:05:00 1.17377 1.17382 1.17376 1.17382 39
2020-08-12 00:06:00 1.17382 1.17389 1.17382 1.17386 36
The first candle should be O:1.7383 H:1.17408 L:1.17372 C: 1.17390 but for some reason it always takes over the very first minute candle which causes all the rest of the candles to be out of sync.
my code below:
class Resampling_test(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function for this strategy'''
dt = dt or self.datas[0].datetime.datetime(0)
print('%s, %s' % (dt.strftime("%Y-%m-%d %H:%M"), txt))
def __init__(self):
pass
def next(self):
self.log('O: %.5f, H: %.5f, L: %.5f, C: %.5f' %(self.datas[0].open[0], self.datas[0].high[0], self.datas[0].low[0], self.datas[0].close[0]))
if __name__ == '__main__':
cerebro = bt.Cerebro()
# Add a strategy
cerebro.addstrategy(Resampling_test)
# Create a Data Feed
data = bt.feeds.PandasData(dataname=df2020)
two_minute = cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=2)
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
cerebro.run()
# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
many thanks