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/

    Problem loading datafeed with PandasData from read_sql with SQLite3

    General Discussion
    2
    5
    24
    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.
    • I
      incabot last edited by

      Hello, as the title suggests, I am following along an online tutorial from PartTimeLarry https://www.youtube.com/watch?v=ouveMWaInn8
      where he is backtesting an opening range breakout strategy.

      My main interest is utilizing the sqlite3 database to load data for backtesting. The tutorial uses the pandas read_sql function to build the dataframe and insert into BT with PandasData bt datafeeds.

      The dataframe appears correct, however I receive this error when I run:

      (venv) griz@gemini-/backtrade/fullBT.py
      == Testing 1 ==
      open high low close volume
      datetime
      2016-02-05 09:30:00 153.2 153.67 153.0 153.66 607.0
      2016-02-05 09:31:00 153.66 153.78 153.28 153.28 85.0
      2016-02-05 09:32:00 153.23 153.24 153.0 153.04 43.0
      2016-02-05 09:33:00 153.16 153.23 152.9 152.9 35.0
      2016-02-05 09:34:00 152.9 152.94 152.76 152.82 54.0
      ... ... ... ... ... ...
      2017-01-04 14:39:00 177.99 178.0 177.99 178.0 5.0
      2017-01-04 14:40:00 178.01 178.01 177.96 177.96 23.0
      2017-01-04 14:41:00 177.95 177.95 177.9 177.9 17.0
      2017-01-04 14:42:00 177.9 177.9 177.83 177.86 17.0
      2017-01-04 14:43:00 177.87 177.94 177.87 177.87 15.0

      [90000 rows x 5 columns]
      Traceback (most recent call last):
      File "/home/griz/backtrade/fullBT.py", line 124, in <module>
      cerebro.run()
      File "/home/griz/backtrade/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run
      runstrat = self.runstrategies(iterstrat)
      File "/home/griz/backtrade/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1212, in runstrategies
      data.preload()
      File "/home/griz/backtrade/venv/lib/python3.8/site-packages/backtrader/feed.py", line 438, in preload
      while self.load():
      File "/home/griz/backtrade/venv/lib/python3.8/site-packages/backtrader/feed.py", line 479, in load
      _loadret = self._load()
      File "/home/griz/backtrade/venv/lib/python3.8/site-packages/backtrader/feeds/pandafeed.py", line 255, in _load
      line[0] = self.p.dataname.iloc[self._idx, colindex]
      File "/home/griz/backtrade/venv/lib/python3.8/site-packages/backtrader/linebuffer.py", line 222, in setitem
      self.array[self.idx + ago] = value
      TypeError: must be real number, not str

      Here is my code for reference. I have searched through other examples in the discussions. I wasn't able to find anything that works. Any help would be much appreciated, thanks for all you guys do.

      code-snapshot.png

      B 2 Replies Last reply Reply Quote 0
      • I
        incabot last edited by

        the datetime = 1 parameter was just something I was trying and forgot to remove, plz ignore it's existence as it had no effect for me..

        1 Reply Last reply Reply Quote 0
        • B
          bigdavediode @incabot last edited by

          @incabot Perhaps on the SQLLite3 side: PRAGMA table_info(stock_price);

          And see if the columns are strings

          1 Reply Last reply Reply Quote 1
          • B
            bigdavediode @incabot last edited by

            @incabot Or perhaps convert your Pandas dataframe datetime column to an actual datetime index rather than a string.

            1 Reply Last reply Reply Quote 2
            • I
              incabot last edited by

              Hey thanks. Naturally, we can just chalk this up to me being dumb. I apparently did fill my database with individually cast str() for some wild reason in the beginning. Not sure why I had to get hung up here for a day to figure it out, but seeing it finally load just now after re-doing the database just now was damn sweet. Thanks again.
              code-snapshot1.png

              (The silly error, commented out and pasted above. Cheers)

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }