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/

    Trading Calendar Error: "searchsorted requires compatible dtype or scalar, not date"

    General Code/Help
    2
    6
    93
    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.
    • Maor Kima
      Maor Kima last edited by

      i would like to configure my app to run online on NYSE Regular Trading hours.
      so i thought that Trading Calendar will be a great help.

      fllowing the docs, i've tried to add cerebro.addcalendar('NYSE') to my multi data feed app, i get the below error:

      Exception has occurred: TypeError
      searchsorted requires compatible dtype or scalar, not date
        File "AppData\Local\Programs\Python\Python38-32\Lib\site-packages\pandas\core\indexes\datetimes.py", line 859, in searchsorted
          raise TypeError(
        File "AppData\Local\Programs\Python\Python38-32\Lib\site-packages\backtrader\tradingcal.py", line 270, in schedule
          i = self.idcache.index.searchsorted(day.date())
        File "AppData\Local\Programs\Python\Python38-32\Lib\site-packages\backtrader\feed.py", line 232, in _getnexteos
      

      any ideas on what can cause it? will the trading calendar can help with disregarding pre/post market?
      Thanks in advance!!

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Would you mind to please add your code to the question?

        1 Reply Last reply Reply Quote 0
        • Maor Kima
          Maor Kima last edited by

          Hi, Sure.
          edited the tcal-intra.py

          def runstrat(args=None):
              args = parse_args(args)
          
              cerebro = bt.Cerebro()
          
              # Data feed kwargs
              # kwargs = dict(tz='US/Eastern')
              # import pytz
              # tz = tzinput = pytz.timezone('Europe/Berlin')
             
              back=bt.feeds.GenericCSVData(name="xom_min", dataname='C:\xom_1M.csv',timeframe=bt.TimeFrame.Minutes,dtformat=('%Y%m%d    %H:%M:%S'),open=1,high=2,low=3,close=4,volume=5,openinterest=-1,reverse=True,compression=1)
                
              data1 = cerebro.replaydata(back,name="xom_min", timeframe=bt.TimeFrame.Minutes,compression=1)
              data2 = cerebro.resampledata(data1,name="xom_hour", timeframe=bt.TimeFrame.Minutes,compression=60)
              cerebro.addcalendar('NYSE')
              
              # Strategy
              cerebro.addstrategy(St, **eval('dict(' + args.strat + ')'))
          
              # Execute
              cerebro.run(**eval('dict(' + args.cerebro + ')'))
          

          resulted in the error described .

          thanks.

          1 Reply Last reply Reply Quote 0
          • Maor Kima
            Maor Kima last edited by

            I dug the code a bit and it looks like there is an issue with tardingcal.py schedule method.
            the searchsorted on line 269 should receive a datetime object, not date.

            i've tweaked the code as follows which seems to work.

                def schedule(self, day, tz=None):
                    '''
                    Returns the opening and closing times for the given ``day``. If the
                    method is called, the assumption is that ``day`` is an actual trading
                    day
            
                    The return value is a tuple with 2 components: opentime, closetime
                    '''
            
                    while True:
                        i = self.idcache.index.searchsorted(day.replace(hour=00,minute=00))
                        if i == len(self.idcache):
                            # keep a cache of 1 year to speed up searching
                            self.idcache = self._calendar.schedule(day, day + self.csize)
                            continue
            
            
            1 Reply Last reply Reply Quote 1
            • run-out
              run-out last edited by

              This is an open issue for backtrader. Would you like to have a look at it?
              https://github.com/backtrader2/backtrader/issues/18

              1 Reply Last reply Reply Quote 0
              • Maor Kima
                Maor Kima last edited by

                @run-out - thanks. that's indeed solves the problem.

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