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/

    Import CSV into Backtrader

    General Discussion
    3
    3
    4106
    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.
    • Charles Audler
      Charles Audler last edited by Charles Audler

      Hi, I'm new to backtrader and having a hard time reading in my csv file.

      I get the error "ValueError: time data '10-18-2017T 17:59:29.5' does not match format '%m-%d-%YT%H:%M:%S.%f"

      The CSV file has the following columns

      Date Time Open High Low Close
      10-18-2017 17:59:29.5 113.084 113.092 113.062 113.063

      import backtrader as bt
      import backtrader.feeds as btfeed
      
            
      class customCSV(btfeed.GenericCSVData):
          params = (
              ('dtformat', '%m-%d-%Y'),
              ('tmformat', '%H:%M:%S.%f'),
              ('datetime', 0),
              ('time', 1),
              ('open', 2),
              ('high', 3),
              ('low', 4),
              ('close', 5),
              ('volume', -1),
              ('openinterest', -1),
          )
      
      class SmaCross(bt.SignalStrategy):
          params = (('pfast', 10), ('pslow', 30),)
      
          def __init__(self):
              sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
              self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
      
      
      cerebro = bt.Cerebro()
      
      data = customCSV(dataname='USDJPY_Backtrader2.csv')
      
      cerebro.adddata(data)
      cerebro.addstrategy(SmaCross)
      cerebro.run()
      cerebro.plot(style='candle')
      
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by ab_trader

        Just wild guess - default separator is (,), and your's is space and it is not specified.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators last edited by

          My upvote for @ab_trader shows my support of his theory

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