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/

    Why does self.datas[3] == self.datas[6] evaluates to True?

    General Code/Help
    2
    2
    54
    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.
    • Eduardo De La Garza
      Eduardo De La Garza last edited by

      When I run the following function all items end up in the buys list. I started looking into the code and realized that self.datas[3] == self.datas[6] evaluates to true even though they have different eq and has. Why is that? How can I do what Im trying to do? Thanks.

      def rebalance_portfolio(self):
              inds = self.inds
              rankings = self.stocks
              rankings = rankings[:self.p.num_stocks]
              buys = [d for d in self.stocks if d in rankings]
              sells = [d for d in self.stocks if d not in rankings]
      
      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @Eduardo De La Garza last edited by

        AFAIU, the data feeds ( and other classes inherited from LineRoot ) override all the operators (including eq ) in order to support the definition of complex lines during the strategy's __init__ method. Explained in the docs: here and here

        So just writing ( during the __init__ method only ):

        a = self.datas[2] == self.datas[3]
        

        will result not in boolean value, but return a LineOperation object.

        However, after the data feeds start` method is called the behavior of the above operators changes. Now instead of returning the LineOperation, the operator is applied to the current value of the first line object in the data feed. So:

        a = self.datas[2] == self.datas[3]
        

        will actually be equal to:

        a = self.datas[2].lines[0][0] == self.datas[3].lines[0][0]
        

        It may not sound intuitive at first - but this is how it seems to be working (at least in my debugger :-) ). Please correct me if I'm wrong in case you are observing something else.

        Back to your second question - I would suggest to store the index of the data in the rankings array instead of storing the data feed itself. Probably this way it will be easier to compare them.

        1 Reply Last reply Reply Quote 1
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors