backtrader2/backtrader

CmpEx function seems to behave incorrectly

Opened this issue · 0 comments

Community discussion:
https://community.backtrader.com/topic/3042/find-bugs-when-using-the-demarkpivotpoint-indicator

Unexpected behavior:

Looking at the code of CmpEx.next() method is seems the logic in this method is different from the CmpEx.once() method:

    def next(self):
        self[0] = cmp(self.a[0], self.b[0])

    def once(self, start, end):
        # cache python dictionary lookups
        dst = self.array
        srca = self.a.array
        srcb = self.b.array
        r1 = self.r1.array
        r2 = self.r2.array
        r3 = self.r3.array

        for i in range(start, end):
            ai = srca[i]
            bi = srcb[i]

            if ai < bi:
                dst[i] = r1[i]
            elif ai > bi:
                dst[i] = r3[i]
            else:
                dst[i] = r2[i]

it results in incorrect function calculation and plotting output (see the community discussion)

Proposed fix:

   def next(self):
        if self.a[0] < self.b[0]:
            self[0] = self.r1[0]
        elif self.a[0] > self.b[0]:
            self[0] = self.r3[0]
        else:
            self[0] = self.r2[0]