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/

    Error with MT5 CSV file read

    General Code/Help
    mt5 csv
    2
    3
    188
    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.
    • Eduardo Menges Mattje
      Eduardo Menges Mattje last edited by

      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

      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        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'

        1 Reply Last reply Reply Quote 1
        • Eduardo Menges Mattje
          Eduardo Menges Mattje last edited by

          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!

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