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

calling data in int() by name / dataname

  • data by name.

    referencing data by _name

    when adding data I do have the attribute name in this example well say name = SWK with in the bt.feeds.GenericCSVData()

    Now with in def __init__(self): I am trying to call the data by name and can not seem to get it... IE
    self.data_SWK.close does not work... self.datas_SWK.close does not work either.

    referencing source data_NAME

    line 109 ` - self.dataX_name -> self.datas[X].name . But this is for an analyzer, maybe its not possible any other place.

    Maybe I am miss interpreting the documents.

  • administrators

    Yes you are misinterpreting the docs. Which means that they probably unclear. What you can reach is the name of the defined lines. A typical OHLC will have an attribute named:

    • self.lines.close

    which can be accessed as:

    • self.close

    In a Strategy or Analyzer you can reach the same as:

    • self.dataX_close

    The potential benefits (which may not fit your purpose, goal)

    • Less typing
    • Save some Python lookup cycles (one dict access versus three)

    Many will consider that a classical dot-based notation is clearer

    • self.dataX.close (and even the actual complete path self.dataX.lines.close)

  • @backtrader ok, that is more clear. is there any where to get an list of all datas available ? The issue that arrises for my use-case is that if you are loading in 100 data feeds into a master strategy, how to know if self.data23.close is symbol X or Y to pass it along to the sub-strategy class.

    potential solution could be just as simple as passing the symbol list as a param and then running something like ["foo", "bar", "baz"].index("bar") i suppose. assume backtrader loads the data in sequential order every time from the list of symbols .

  • @backtrader

    if not I believe I found a work around, by passing up through params the len() of data feeds added (IE how many) and then doing the following in int()

        datas_dic = {}
        for num in range(0,self.p.total_feeds):
            sym_name = self.datas[num]._name
            if not sym_name in datas_dic:
                datas_dic[sym_name] = ('self.datas%s' % num)

    returns a list looking similar to :

    {'WASH': 'self.datas2', 'ACN': 'self.datas1', 'SWK': 'self.datas0', 'WSBC': 'self.datas3'}


    this is needed because i am creating a large portfolio of pairs. I have a single master strategy that then creates sub strategies and I need to create a new sub strategy for every pair, and to create that sub strategy I need to know which datas it needs to run its calculations.

Log in to reply