For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
AttributeError: 'float' object has no attribute 'isoformat'
-
I got this error when running below code. How to fix it ? Thank you
File "C:/Users/PC/PycharmProjects/trading/scr/test.py", line 22, in next self.log('Close, %.2f', self.dataclose[0]) File "C:/Users/PC/PycharmProjects/trading/scr/test.py", line 16, in log print('%s, %s' % (dt.isoformat(), txt)) AttributeError: 'float' object has no attribute 'isoformat'
from __future__ import (absolute_import, division, print_function, unicode_literals) from datetime import datetime import os.path # To manage paths import sys # To find out the script name (in argv[0]) # Import the backtrader platform import backtrader as bt #Create a Strategy class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): self.dataclose = self.datas[0].close def next(self): self.log('Close, %.2f', self.dataclose[0]) if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(TestStrategy) data = bt.feeds.YahooFinanceData( dataname='AAPL', fromdate=datetime(2017, 8, 8), todate=datetime(2017, 10, 10), buffered=True ) cerebro.adddata(data) cerebro.broker.setcash(10000) print('Starting Portfolio Value: {}'.format(cerebro.broker.getvalue())) cerebro.run() print('Final value: {}'.format(cerebro.broker.getvalue()))
-
Can you run the code with different data source?
Yahoo data is really unstable now, I will not be surprised that it can cause an error. -
@Nguyễn-Tài-Nguyên said in AttributeError: 'float' object has no attribute 'isoformat':
def next(self): self.log('Close, %.2f', self.dataclose[0])
You pass a
float
and you get a float error. Fix it by not passing a float. You probably wanted to do thisdef next(self): self.log('Close, %.2f' % self.dataclose[0])
-
Thank you @ab_trader and @Paska-Houso for replying me :)
@Paska-Houso ,Yes, that is my wrong. I fixed it and it worked