Thank you, it's working. Here is the code :
(no images this time )
import backtrader as bt
from trending_detect import TestStrategy2
from getting_data import getStockFromCSV
# Create a cerebro entity
cerebro = bt.Cerebro()
# Add a strategy
cerebro.addstrategy(TestStrategy2)
data = getStockFromCSV(stock_index='AAPL', period='H1', add=[])[-3000:]
df = bt.feeds.PandasData(dataname=data,
timeframe=bt.TimeFrame.Minutes,
compression=60,
openinterest=-1)
#df = PandasData(dataname=data)
cerebro.adddata(df)
cerebro.resampledata(df, timeframe=bt.TimeFrame.Days, compression=1)
# Set our desired cash start
cerebro.broker.setcash(1000000.0)
# Print out the starting conditions
print('Starting Portfolio Value: %.5f' % cerebro.broker.getvalue())
# Run over everything
cerebro.run()
# Print out the final result
print('Final Portfolio Value: %.5f' % cerebro.broker.getvalue())
class TestStrategy2(bt.Strategy):
def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose0 = self.data0.close
self.dataclose1 = self.data1.close
#print(len(self.datas))
#print(len(self.data0.close))
#print(len(self.data1.close))
def log(self, dt=None):
txt0 = 'closing: %.5f' % self.dataclose0[0]
txt1 = 'closing: %.5f' % self.dataclose1[0]
date0 = dt or self.datas[0].datetime.date(0)
time0 = self.datas[0].datetime.time(0)
date1 = dt or self.datas[1].datetime.date(0)
time1 = self.datas[1].datetime.time(0)
print('%s,%s, %s' % (date0.isoformat(),time0.isoformat(), txt0))
print('%s,%s, %s' % (date1.isoformat(),time1.isoformat(), txt1))
def next(self):
#print('datas', len(self.datas))
print('close0', self.dataclose0[0])
print('close1', self.dataclose1[0])
self.log()
But the output is a litlle weird...
close0 132.91
close1 133.72
2020-12-31,21:00:00, closing: 132.91000
2020-12-30,23:59:59.999989, closing: 133.72000
close0 132.03
close1 132.91
2021-01-04,16:00:00, closing: 132.03000
2020-12-31,23:59:59.999989, closing: 132.91000
close0 131.02
close1 132.91
2021-01-04,17:00:00, closing: 131.02000
2020-12-31,23:59:59.999989, closing: 132.91000
close0 131.02
close1 131.02
2021-01-04,17:00:00, closing: 131.02000
2021-01-04,23:59:59.999989, closing: 131.02000
Final Portfolio Value: 1000000.00000
why do I have a few milliseconds difference between the two dates?