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/

    Error with Starter Code (GenericCSV) / (Print Close)

    General Code/Help
    1
    1
    30
    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.
    • M
      moneymay7 last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors