Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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'

    General Code/Help
    3
    4
    2964
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Nguyễn Tài Nguyên
      Nguyễn Tài Nguyên last edited by

      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()))
      P 1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        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.

        1 Reply Last reply Reply Quote 1
        • P
          Paska Houso @Nguyễn Tài Nguyên last edited by

          @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 this

          def next(self):
              self.log('Close, %.2f' % self.dataclose[0])
          1 Reply Last reply Reply Quote 1
          • Nguyễn Tài Nguyên
            Nguyễn Tài Nguyên last edited by

            Thank you @ab_trader and @Paska-Houso for replying me :)
            @Paska-Houso ,Yes, that is my wrong. I fixed it and it worked

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }