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

loading 'string' custom data

  • Hi,

    I want to laod string data into backtrader (eg. the industry for each stock', I am trying to do so using CustomBTStockLoader(btfeeds.PandasData) however I get a float error:

    Traceback (most recent call last):
      File "T:/Google Drive/PyCharm/Modules/", line 683, in <module>
        timer = RunStrategy(timer,minholddays=vars.minholddays,buyscore=vars.buyscore,mode=mode)
      File "T:/Google Drive/PyCharm/Modules/", line 619, in RunStrategy
        results =, writer=True, quicknotify=False)
      File "C:\Program Files\Anaconda3\lib\site-packages\backtrader\", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\Program Files\Anaconda3\lib\site-packages\backtrader\", line 1212, in runstrategies
      File "C:\Program Files\Anaconda3\lib\site-packages\backtrader\", line 435, in preload
        while self.load():
      File "C:\Program Files\Anaconda3\lib\site-packages\backtrader\", line 476, in load
        _loadret = self._load()
      File "C:\Program Files\Anaconda3\lib\site-packages\backtrader\feeds\", line 255, in _load
        line[0] = self.p.dataname.iloc[self._idx, colindex]
      File "C:\Program Files\Anaconda3\lib\site-packages\backtrader\", line 222, in __setitem__
        self.array[self.idx + ago] = value
    TypeError: a float is required

    Is their any way I can load string data as a line? My current step to load is as follows:

    class CustomBTStockLoader(btfeeds.PandasData):
        #  None : column not present
        #  -1 : autodetect position or case-wise equal name
        #  >= 0 : numeric index to the colum in the pandas dataframe
        #  string : column name (as index) in the pandas dataframe
        # Force OPEN = HIGH = LOW = CLOSE_ADJ... because Morningstar's OHL data is shit
        params = (
                     ('openinterest', None),
                     ('open', 'Close_adj'),
                     ('high', 'Close_adj'),
                     ('low', 'Close_adj'),
                 ) \
                 + tuple((e,-1) for e in vars.analysiscols)   #<--- this loads my custom data columns.
        if vars.Custom_Alg == True:
            lines = tuple(vars.analysiscols) #('Opportunity',)
            datafields = btfeeds.PandasData.datafields + vars.analysiscols
            datafields = btfeeds.PandasData.datafields

    vars.analysiscols is a list like so: analysiscols = ['Close_adj','GICSsector','blacklist']

  • administrators

    Sorry, but the lines in each object do only support float values.

Log in to reply