For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Beginner- the date data format Error

  • I'm a beginner and i have problem to add data to Cerebra engine. The error part of code is:

    fromdate = datetime(2012,1,1)
    todate = datetime(2012,1,5)
    datapath = 'G:/python/EURUSD.m1.csv'
    data0 = bt.feeds.GenericCSVData(
       dtformat=('%Y.%m.%d %H:%M:%S'),
       openinterest=-1 #-1 means not used

    And my data field in csv file is :
    2017.07.31 21:17:00,1.18410,1.18410,1.18401,1.18404,28

    And this is the Error :

    Traceback (most recent call last):
     File "G:/python/", line 48, in <module>
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\site-packages\backtrader\", line 1127, in run
       runstrat = self.runstrategies(iterstrat)
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\site-packages\backtrader\", line 1212, in runstrategies
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\site-packages\backtrader\", line 685, in preload
       while self.load():
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\site-packages\backtrader\", line 476, in load
       _loadret = self._load()
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\site-packages\backtrader\", line 707, in _load
       return self._loadline(linetokens)
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\site-packages\backtrader\feeds\", line 114, in _loadline
       dt = datetime.strptime(dtfield, dtformat)
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\", line 565, in _strptime_datetime
       tt, fraction = _strptime(data_string, format)
     File "C:\Users\asus\AppData\Local\Programs\Python\Python36\lib\", line 362, in _strptime
       (data_string, format))
    ValueError: time data '"2017.08.07 14:16:00"' does not match format '%Y.%m.%d %H:%M:%S'

    *** Please give me some help. ***bolded text

  • administrators

    @siavash said in Beginner:

    Please give me some help.

    You want it, you get it. Look at the top of the forum for how to format your code and output. Re-post and when people are actually able to see what you have posted, they may be able to help.

    This is not intended to talk you down or patronize you. It is meant as real help for a beginner: learn to see what you have before your eyes, be it code, be it how to post to a forum to ask for help or exchange code.

  • I had a similar issue and couldn't find what was wrong for the life of me. Instead I used the following class and it worked like a charm

    class custom_csv(btfeed.GenericCSVData):
        params = (
            ('datetime', 0),
            ('time', -1),
            ('open', 1),
            ('high', 2),
            ('low', 3),
            ('close', 4),
            ('volume', 5),
            ('openinterest', -1),
            ('timeframe', bt.TimeFrame.Minutes),
            ('compression', 1),

    and to actually use it you can do

    data = custom_csv(dataname='some_csvf_file')

    if you have found another solution for your problem please post it here so others can use it.

  • Thanks Mr.AliAskari for your reply. I've solved this problem. It was because of my data file's format.
    I would like to send you a message to share our experience about working with backtrader, but I dont know where and how to send a private message.

Log in to reply