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(self.data.close, period=self.params.period) def next(self): if not self.position: if self.rsi < 30: self.buy(size=100) else: if self.rsi > 70: self.sell(size=100) # 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( dataname='ETHUSDH2.csv', fromdate=datetime(2019, 1, 1, 0, 0), todate=datetime(2020, 1, 1, 0, 0), headers=False, separator=",", volume=6, openinterest=-1, dtformat=('%Y.%m.%d %H:%M'), ) # Add the data to Cerebro cerebro.adddata(data) # Set our desired cash start cerebro.broker.setcash(startcash) # Run over everything cerebro.run() # Get final portfolio Value portvalue = cerebro.broker.getvalue() pnl = portvalue - startcash # Print out the final result print('Final Portfolio Value: ${}'.format(portvalue)) print('P/L: ${}'.format(pnl)) # Finally plot the end results cerebro.plot(style='candlestick')
This is the CSV file: https://pastebin.com/GMBpdV2P
-
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!