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

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

  • 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]

  • 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 feedsstart` 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.

Log in to reply