For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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

  • 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\", line 859, in searchsorted
        raise TypeError(
      File "AppData\Local\Programs\Python\Python38-32\Lib\site-packages\backtrader\", line 270, in schedule
        i = self.idcache.index.searchsorted(
      File "AppData\Local\Programs\Python\Python38-32\Lib\site-packages\backtrader\", 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!!

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

  • Hi, Sure.
    edited the

    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)
        # Strategy
        cerebro.addstrategy(St, **eval('dict(' + args.strat + ')'))
        # Execute**eval('dict(' + args.cerebro + ')'))

    resulted in the error described .


  • I dug the code a bit and it looks like there is an issue with 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
            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)

  • This is an open issue for backtrader. Would you like to have a look at it?

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

Log in to reply