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/

    Indicator on multi data feed

    General Code/Help
    4
    9
    1817
    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.
    • A
      anfei last edited by

      Hi everyone,
      I have a question regarding calculating indicator with multi data feed, say I have 3 lines of data added onto strategy, when calculating sma, should I just loop through those 3 data object, create sma series one by one? and end up with a list of sma objects? Then how I can easily access them? I tried
      strategy.sma[0][0], doesn't work. I think strategy.sma[0] returns one of sma series, correct?
      Appreciate your help.

      FAN

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @anfei last edited by

        @anfei said in Indicator on multi data feed:

        should I just loop through those 3 data object, create sma series one by one? and end up with a list of sma objects

        Yes

        @anfei said in Indicator on multi data feed:

        Then how I can easily access them?

        That depends on how you store them.

        @anfei said in Indicator on multi data feed:

        I tried
        strategy.sma[0][0], doesn't work. I think strategy.sma[0] returns one of sma series, correct?

        Again, accessing your own data structure is something nobody can tell you how to do. Unless you show what you tried, there is no way to tell what you did and why your attempts fail.

        1 Reply Last reply Reply Quote 0
        • A
          anfei last edited by

          Thanks for your help and apologies for lack of details of my question.
          in strategy class, under init, I put all SMA objects into a list, adding one element a time when looping through datas

              self.smas = []
              for dd in self.datas:
                  self.smas.append(bt.indicators.SimpleMovingAverage(
                          dd, period=self.params.maperiod))
          

          then I struggle to get the right syntax to access to it

                  if self.datas[0].close[0] > self.smas[0][0]:
          

          this doesn't work. Please help. Or any other way to store and call them? Appreciate your help.

          Also I want to make sure SMA is aligned with data series by finding the position in the list using list.index:

          for i in firstStrategy.datas:
          print(i)
          print(firstStrategy.datas.index(i))

          but it shows an error on calling datas[1].

          How can I make sure calling SMA[1] when working with datas[1]?

          thanks

          FAN

          1 Reply Last reply Reply Quote 0
          • A
            anfei last edited by

            I have partially fixed the issue, current value of sma can be accessed via:

            self.smas[0].sma[0]
            or self.smas[0].lines.sma[0]

            might not be the decent way, happy to learn if any better ones.

            but still have not figure out how to match the right position in list of SMAs with list of datas, sams[1] matching datas[1].

            Thanks

            FAN

            1 Reply Last reply Reply Quote 0
            • A
              ab_trader last edited by

              Can be an option to hold all indicators in dictionaries and use data names as keys.

              Also couple directions -

              Multi Example

              Search forum using the word multi

              1 Reply Last reply Reply Quote 0
              • A
                anfei last edited by

                Thanks for your help.

                Can you please take a look why below code does not work?

                print(firstStrategy.datas[0])
                print(firstStrategy.datas[1])
                for i in firstStrategy.datas:
                    print(i)
                    print(firstStrategy.datas.index(i))
                <backtrader.feeds.yahoo.YahooFinanceCSVData object at 0x000000000EDED9E8>
                <backtrader.feeds.yahoo.YahooFinanceCSVData object at 0x000000000EE79278>
                <backtrader.feeds.yahoo.YahooFinanceCSVData object at 0x000000000EDED9E8>
                0
                <backtrader.feeds.yahoo.YahooFinanceCSVData object at 0x000000000EE79278>
                Traceback (most recent call last):
                
                  File "<ipython-input-200-e02b00cbff02>", line 5, in <module>
                    print(firstStrategy.datas.index(i))
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\lineroot.py", line 281, in __eq__
                    return self._operation(other, operator.__eq__)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\lineroot.py", line 86, in _operation
                    other, operation, r=r, intify=intify)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\lineroot.py", line 201, in _operation_stage1
                    return self._makeoperation(other, operation, r, self)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\lineroot.py", line 331, in _makeoperation
                    return self.lines[0]._makeoperation(other, operation, r, _ownerskip)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\linebuffer.py", line 378, in _makeoperation
                    _ownerskip=_ownerskip)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\linebuffer.py", line 522, in __call__
                    return super(MetaLineActions, cls).__call__(*args, **kwargs)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\metabase.py", line 89, in __call__
                    _obj, args, kwargs = cls.dopostinit(_obj, *args, **kwargs)
                
                  File "C:\Users\fan\Anaconda3\lib\site-packages\backtrader\linebuffer.py", line 567, in dopostinit
                    _obj._owner.addindicator(_obj)
                
                AttributeError: 'NoneType' object has no attribute 'addindicator'
                
                1 Reply Last reply Reply Quote 0
                • B
                  backtrader administrators last edited by

                  The first thing is: what's your context?

                  Isolated code as the one you show gives no indication as to what you would try to achieve.

                  @anfei said in Indicator on multi data feed:

                  print(firstStrategy.datas[0])
                  print(firstStrategy.datas[1])
                  for i in firstStrategy.datas:
                      print(i)
                      print(firstStrategy.datas.index(i))
                  

                  That's obviously not being executed inside a strategy, which means you have something in mind.

                  In any case this:

                      print(firstStrategy.datas.index(i))
                  

                  seems to have no real purpose at all, since you are already iterating over the list of data feeds.

                  1 Reply Last reply Reply Quote 0
                  • A
                    anfei last edited by

                    @ab_trader

                    Can be an option to hold all indicators in dictionaries and use data names as keys.

                    thanks for the tip, will have a try

                    1 Reply Last reply Reply Quote 0
                    • S
                      Sagittarius19 last edited by Sagittarius19

                      @anfei I did something similar in my post. Putting it in a dictionary was an easy way to access it. You can see in the last comment here : link text

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