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/

    Could not convert string to float: '2021-04-09' when loading from another CSV file

    General Code/Help
    3
    9
    651
    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.
    • P
      punajunior last edited by

      As the title suggests, I am having problems loading raw csv into my code. It is giving ValueError, but I don't know why. I tried to turn my date column in numbers and it still gives me the same error
      Here is my code:
      if name == 'main':
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy)
      cerebro.broker.setcommission(commission=0.001)

      # Create a Data Feed
      data = bt.feeds.GenericCSVData(
          dataname='12 Apr 2021 _Raw.csv',
          fromdate=datetime.datetime(2021, 1, 1),
          todate=datetime.datetime(2021, 4, 5),
          nullvalue = 0.0,
          dtformat=('%Y-%m-%d'),
          datetime = 1,
          )
      
      print(data)
      cerebro.adddata(data)
      cerebro.broker.setcash(100000.0)
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.plot()
      

      Any help is appreciated. Thank you so much!

      R 1 Reply Last reply Reply Quote 0
      • R
        rajanprabu @punajunior last edited by

        @punajunior

        Please share few lines if your csv file..

        P 1 Reply Last reply Reply Quote 1
        • P
          punajunior @rajanprabu last edited by

          @rajanprabu

          Thanks , so my csv compose of below

          83f42b44-27ea-4385-aa7b-245c7650ba48-image.png

          D 1 Reply Last reply Reply Quote 0
          • D
            davidavr @punajunior last edited by

            @punajunior It looks like the date format in your file is "%m/%d/%Y" but you specified "%Y-%m-%d" in your script. Try changing the dtformat argument of GenericCSVData.

            P 1 Reply Last reply Reply Quote 2
            • P
              punajunior @davidavr last edited by

              @davidavr
              So i printed my DF , my date column format is "%Y-%m-%d"
              9ce0d07b-c5b2-4b68-8f46-009f0437fae4-image.png

              I also tried changing the Dtformat to "%m/%d/%Y" but it gives me the same error
              9bd0e661-9478-4623-a3f9-0c2fcdc265f2-image.png

              P 1 Reply Last reply Reply Quote 0
              • P
                punajunior @punajunior last edited by

                @punajunior

                if i updated back to "%Y-%m-%d" it gives me the same error couldnt covert string to float.
                452c8623-0167-4112-b066-653c5c51c397-image.png

                D 1 Reply Last reply Reply Quote 0
                • D
                  davidavr @punajunior last edited by

                  @punajunior Can you just print the first few lines of the actual CSV file, not the output of Pandas (not even sure where you got it into a DF as you aren't using a Pandas data feed)? Just type head "12 Apr 2021 _Raw.csv" at the command line.

                  Those last two errors you posted are for different issues. The first is that it's trying to parse the string '2021-04-09' as a date using the format '%m/%d/%Y' (which you fixed to use the correct format), but the second error is that it's trying to convert the string '2021-04-09' to a float. It makes me think in the second case your date is no longer in the second column (as you specified with date time = 1 in the GenericCSVData call) and it thinks that column is one of the price fields.

                  P 1 Reply Last reply Reply Quote 0
                  • P
                    punajunior @davidavr last edited by

                    @davidavr
                    head "12 Apr 2021 _Raw.csv" <- this doesnt work.
                    so I just use texteditor to open my csv , so it goes like below
                    53729095-dd5a-43a1-a098-86cf6bdeb36f-image.png

                    Date should be second column thus i am using "1" and i've also set the dtformat to match but it prompted the below
                    408df639-944c-4e28-ad7b-3eea5c9de6d0-image.png

                    D 1 Reply Last reply Reply Quote 0
                    • D
                      davidavr @punajunior last edited by

                      @punajunior My guess is that this error is thrown when it's trying to parse the "Open" field which defaults to column 1, so after parsing the datetime correctly, it's then trying to parse that same field as a float. Try adding explicit column indicators for all fields in your call to GenericCSVData:
                      datetime=1,open=2,high=3,low=4,close=5,volume=6,openinterest=-1,

                      BTW, it looks like you created the CSV file using Pandas .to_csv() method without the index=False parameter. That's why it has that unnamed first column. If you can recreate the file without the index, this probably would have just worked.

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