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/

    marketstore * new data feed integration

    General Code/Help
    1
    4
    1109
    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.
    • B
      blonc last edited by blonc

      i am integrating a new data feed called marketstore (both historical and real time), there is a python interface called pymarketstore I have attached a very simplified version bellow in terms of hoping someone can easily follow the code. However I seem to be running into a basic error that has me.

      the code does work when testing it outside of backtrader. the error I get is at the bottom, which to me makes no sense because because I have tested the date2num function on the data and it works fine.

      date2num(datetime.datetime.fromtimestamp(1527973080))
      output>> 736847.5819444444

      import backtrader as bt
      from backtrader import date2num
      import backtrader.feed as feed
      import datetime
      import pymarketstore as pymkts
      
      
      class MarketStore(feed.DataBase):
      
          params = (
              ('host', '127.0.0.1'),
              ('port', '5993'),
              ('symbol', None),
              ('timeframe', None),
              ('startdate', ''),
              ('high', 'High'),
              ('low', 'Low'),
              ('open', 'Open'),
              ('close', 'Close'),
              ('volume', 'Volume'),
          )
      
      
      
          def start(self):
              super(MarketStore, self).start()
      
              self.ndb = pymkts.Client('http://{host}:{port}/rpc'.format(
                  host=self.p.host,
                  port=self.p.port
              ))
      
              # The query could already consider parameters like fromdate and todate
              # to have the database skip them and not the internal code
      
              qstr = pymkts.Params(self.p.symbol, self.p.timeframe, 'OHLCV')
      
              dbars = list(self.ndb.query(qstr).first().array)
      
              self.biter = iter(dbars)
      
          def _load(self):
              try:
                  bar = next(self.biter)
              except StopIteration:
                  return False
      
              self.l.datetime[0] = date2num(datetime.datetime.fromtimestamp(bar[0]))
              self.l.open[0] = bar[1]
              self.l.high[0] = bar[2]
              self.l.low[0] = bar[3]
              self.l.close[0] = bar[4]
              self.l.volume[0] = bar[5]
      
              return True
      

      ERROR on RUN

            File "datafeed_T.py", line 47, in <module>
                     runstrat()
                   File "datafeed_T.py", line 40, in runstrat
                     results = cerebro.run()
                   File "/home/mathew/anaconda3/envs/backtrader/lib/python3.4/site-packages/backtrader/cerebro.py", line 1127, in run
                     runstrat = self.runstrategies(iterstrat)
                   File "/home/mathew/anaconda3/envs/backtrader/lib/python3.4/site-packages/backtrader/cerebro.py", line 1207, in runstrategies
                     data._start()
                   File "/home/mathew/anaconda3/envs/backtrader/lib/python3.4/site-packages/backtrader/feed.py", line 206, in _start
                     self._start_finish()
                   File "/home/mathew/anaconda3/envs/backtrader/lib/python3.4/site-packages/backtrader/feed.py", line 183, in _start_finish
                     self.fromdate = self.date2num(self.p.fromdate)
                   File "/home/mathew/anaconda3/envs/backtrader/lib/python3.4/site-packages/backtrader/feed.py", line 247, in date2num
                     return date2num(dt)
                   File "/home/mathew/anaconda3/envs/backtrader/lib/python3.4/site-packages/backtrader/utils/dateintern.py", line 216, in date2num
                     base = float(dt.toordinal())
                 AttributeError: 'str' object has no attribute 'toordinal'
      

      if needed for reference this is what the output of dbars looks like

          dbars = [(1527972900, 7635.0, 7635.01, 7635.0, 7635.01, 2.6346132199999994),
           (1527972960, 7635.0, 7635.0, 7630.0, 7630.01, 2.3528064599999996),
           (1527973020, 7630.58, 7630.58, 7627.0, 7627.0, 7.306953420000004),
           (1527973080, 7627.0, 7627.0, 7615.0, 7615.0, 15.73571176999999),
           (1527973140, 7615.0, 7615.01, 7615.0, 7615.01, 5.795549140000001),
           (1527973200, 7615.0, 7630.58, 7615.0, 7630.58, 4.835156190000001),
           (1527973260, 7630.57, 7630.57, 7626.0, 7626.01, 1.85923574),
           (1527973320, 7626.0, 7626.0, 7621.54, 7621.54, 0.7277),
           (1527973380, 7621.55, 7621.87, 7621.55, 7621.87, 1.2915719699999997),
           (1527973440, 7621.86, 7623.22, 7621.86, 7623.22, 0.5715000000000001)]
      
      1 Reply Last reply Reply Quote 0
      • B
        blonc last edited by blonc

        the issue seems to be with the date2num function . I have converted my array of epoch back into datetime objects and it seems to pass fine through the function. However, i still get the same error in backtrader. I updated the code as followed :

         self.l.datetime[0] = date2num(datetime.datetime.utcfromtimestamp(int(bar[0])))
        
        1 Reply Last reply Reply Quote 0
        • B
          blonc last edited by

          an example of it passing through function fine but with in backtrader still the error of AttributeError: 'str' object has no attribute 'toordinal'

              import math
              import datetime
              from backtrader import date2num
              
              bars = [(1527991500, 7635.41, 7635.41, 7635.41, 7635.41, 0.13048232),
               (1527991560, 7635.4, 7635.41, 7635.4, 7635.41, 0.3689),
               (1527991620, 7635.41, 7635.41, 7635.41, 7635.41, 3.5814399599999995),
               (1527991680, 7635.41, 7635.41, 7635.41, 7635.41, 1.41122419),]
              
              biter = iter(bars)
              bar = next(biter)
              
              date2num(datetime.datetime.utcfromtimestamp(int(bar[0])))
          
          1 Reply Last reply Reply Quote 0
          • B
            blonc last edited by blonc

            SOLVED

            the fromdate with in the datafeed initialize was a integer and needed to be a datetime object . IE datetime.date(2018, 6, 1)

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