Hi, I am having a real issue with the starter code. I've inputted everything from the template just to get the close to print, and even tried to debug myself but cant figure out what my issue is.
Below I have copied what my code looks like. When I keep it as is, the below happens:
import datetime
import backtrader as bt
import backtrader.feeds as btfeed
class PrintClose(bt.Strategy):
def __init__(self):
#Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close
def log(self, txt, dt=None):
dt = dt or self.datas[0].datetime.date(0)
print(f'{dt.isoformat()} {txt}')
def next(self):
self.log('Close: ', self.dataclose[0])
movedata = btfeed.GenericCSVData(
dataname='FTX_BTCMOVE2022Q3, 30_1.csv',
fromdate = datetime.datetime(2022, 1, 1),
todate = datetime.datetime(2022, 8, 13),
nullvalue = 0.0,
dtformat= ('%Y-%m-%d'),
tmformat= ('%H:%M:%S'),
datetime = 5,
time = 0,
high = 2,
low = 3,
open = 1,
close = 4,
volume = -1,
openinterest = -1,
)
#Instantiate Cerebro engine
cerebro = bt.Cerebro()
#add data to backtester
cerebro.adddata(movedata)
#Add strategy to Cerebro
cerebro.addstrategy(PrintClose)
#Run Cerebro Engine
cerebro.run()
I get the following error:
File "/Users/apple/Documents/ Algo V1", line 329, in next
self.log('Close: ', self.dataclose[0])
File "/Users/apple/Documents/Algo V1", line 326, in log
print(f'{dt.isoformat()} {txt}')
AttributeError: 'float' object has no attribute 'isoformat'
When i remove the .isoformat, dt is pulling in my close data instead of the timedate. So then i flipped the dt variable around to say dt = self.datas[0].datetime.date(0) or dt, and the code runs. However the output is:
2022-01-01 Close:
2022-01-01 Close:
For some odd reason the code is having a very hard time pulling in the date time in conjunction with the close data in my CSV below.
My CSV file looks like:
time,open,high,low,close,date
00:00:00,17433,17608,17384,17608,2022-01-01
00:30:00,17608,17608,17608,17608,2022-01-01
Any help here would be greatly appreciated. Thanks!