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

Cannot create a DataFeed for my CSV File



  • Hi, I have a problem creating my own DataFeed

    I used GenericCSVData to use my CSV file on Backtrader, but I get this error :

    ValueError: time data '"2019-06-30T15-10-00' does not match format '%Y-%m-%dT%H-%M-%S'

    Which doesn't make sense because it seems to be right.
    I succeeded to make it working with a CSV years-months-day only, but not with hours-minutes-seconds, I don't understand why ? I think the problem might be this " which is near the 2019 in the error message, but I don't know how to erase it.

    Here is the code :

    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath, 'backtrader/datas/yhoo.csv')
    # Create a Data Feed
    data = btfeed.GenericCSVData(
    dataname=datapath,
    
    nullvalue=0.0,
    
    dtformat=('%Y-%m-%d'),
    tmformat=('%H-%M-%S'),
    
    datetime=0,
    time=1,
    open=2,
    high=3,
    low=4,
    close=5,
    volume=6,
    openinterest=-1
    

    )

    Here is my CSV : alt text https://ibb.co/sF47WV4

    And here is the full error I get :

    Starting Portfolio Value: 10000.00
    Traceback (most recent call last):
    File "G22.py", line 151, in <module>
    cerebro.run()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/cerebro.py", line 1127, in run
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/cerebro.py", line 1212, in runstrategies
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/feed.py", line 688, in preload
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/feed.py", line 479, in load
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/feed.py", line 710, in _load
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/backtrader-1.9.70.122-py3.7.egg/backtrader/feeds/csvgeneric.py", line 114, in _loadline
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 359, in _strptime
    (data_string, format))
    ValueError: time data '"2019-06-30T15-10-00' does not match format '%Y-%m-%dT%H-%M-%S'


  • administrators

    @Garen62 said in Cannot create a DataFeed for my CSV File:

    ValueError: time data '"2019-06-30T15-10-00' does not match format '%Y-%m-%dT%H-%M-%S'
    Which doesn't make sense because it seems to be right.

    Yes it makes sense. See the " (double quote) before 2019.



  • @backtrader Yes that is the problem, I don't understand why the quote is here. I just followed the documentation of GenericCSVData, do you have an idea about why ?



  • Update : The problem with the comma was an Excel thing, when I copy pasted it on an Text Editor there were commas at each line.

    After that, I got an IndexError: array assignment index out of range, which was solved by adding more data, thanks to this thread : https://community.backtrader.com/topic/407/indexerror-array-assignment-index-out-of-range/3

    My last problem is that I don't know how to show Hours-Minutes-Seconds when I launch the script, my log function is :

    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))
    

    Backtrader shows me :
    2019-06-30, Close, 295.65
    2019-06-30, Close, 295.65
    2019-06-30, Close, 295.85
    But how can I have H-M-S too in the log function ?



  • Backtrader shows me :
    2019-06-30, Close, 295.65
    2019-06-30, Close, 295.65
    2019-06-30, Close, 295.85
    But how can I have H-M-S too in the log function ?
    

    Try changing

    dt = dt or self.datas[0].datetime.date(0)
    

    to ...

    dt = dt or self.datas[0].datetime.datetime(0)
    


  • Thank you ! The problem was half solved with your solution, it gave me 23:59:00 at each line instead of the real time, the solution was to add a :
    timeframe=bt.TimeFrame.Minutes in the GenericCSVData parameters

    Thanks again !


Log in to reply
 

});