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

Error with MT5 CSV file read

  • Hi,

    I'm trying to implement a strategy I took from Backtest Rookies to work with a MT5 CSV file, but it seems that it was unable to break the CSV into columns.

    I get the error ValueError: time data 'ÿþ2\x000\x001\x008\x00.\x000\x001\x00.\x000\x001\x00 \x000\x000\x00:\x000\x000\x00' does not match format '%Y.%m.%d %H:%M', even tough the dtformat is right.

    This is the code that I'm trying to run:

    import backtrader as bt
    from datetime import datetime
    class firstStrategy(bt.Strategy):
        params = (
            ('period', 21),
        def __init__(self):
            self.rsi = bt.indicators.RSI_SMA(, period=self.params.period)
        def next(self):
            if not self.position:
                if self.rsi < 30:
                if self.rsi > 70:
    # Variable for our starting cash
    startcash = 10000
    # Create an instance of cerebro
    cerebro = bt.Cerebro()
    # Add our strategy
    cerebro.addstrategy(firstStrategy, period=14)
    data = bt.feeds.GenericCSVData(
        fromdate=datetime(2019, 1, 1, 0, 0),
        todate=datetime(2020, 1, 1, 0, 0),
        dtformat=('%Y.%m.%d %H:%M'),
    # Add the data to Cerebro
    # Set our desired cash start
    # Run over everything
    # Get final portfolio Value
    portvalue =
    pnl = portvalue - startcash
    # Print out the final result
    print('Final Portfolio Value: ${}'.format(portvalue))
    print('P/L: ${}'.format(pnl))
    # Finally plot the end results

    This is the CSV file:

  • Tried to run your code and it seems to work just fine with the csv file you've provided.

    How did you download the file - there are several options ( raw, download, ...) - I was using just 'download'

  • I generated the file with MetaTrader 5.
    Now I tested what you did, downloading the file and it worked (???).
    Maybe I can't use the original MT5 file because of protection issues, but I'm not certain. Copying the text and placing it into another file seems to fix it.
    Anyway, it's working now. Thanks!

Log in to reply