def testListedAndCompoundedOperator(self):
        maClose = MovingAverage(20, 'close')
        maOpen = MovingAverage(10, 'open')
        maRf = MovingAverage(10, 'rf')
        listHolder = MovingAverage(20, 'close') ^ MovingAverage(10, 'open') ^ MovingAverage(10, 'rf')
        mc = MovingAlphaBeta(20, listHolder)

        maCloseContainer = deque(maxlen=20)
        maOpenContainer = deque(maxlen=20)

        for i, (open, close, rf) in enumerate(zip(self.sampleOpen, self.sampleClose, self.sampleRf)):
            data = {'close': close, 'open': open, 'rf': rf}
            maClose.push(data)
            maOpen.push(data)
            maRf.push(data)
            mc.push(data)
            maCloseContainer.append(maClose.result() - maRf.result())
            maOpenContainer.append(maOpen.result() - maRf.result())

            if i >= 2:
                expected = linregress(maOpenContainer, maCloseContainer)
                calculated = mc.result()

                # check alpha
                self.assertAlmostEqual(expected[1], calculated[0], 10, "at index {0:d}\n"
                                                                       "expected alpha:   {1:f}\n"
                                                                       "calculated alpha: {2:f}".format(i, expected[1],
                                                                                                        calculated[0]))

                # check beta
                self.assertAlmostEqual(expected[0], calculated[1], 10, "at index {0:d}\n"
                                                                       "expected beta:   {1:f}\n"
                                                                       "calculated beta: {2:f}".format(i, expected[0],
                                                                                                       calculated[1]))
Ejemplo n.º 2
0
    def testMovingAlphaBeta(self):
        dirName = os.path.dirname(os.path.abspath(__file__))
        filePath = os.path.join(dirName, 'data/alphabeta.csv')

        window = 120

        with open(filePath, 'r') as fileHandler:
            reader = csv.reader(fileHandler)
            mv = MovingAlphaBeta(window)
            for i, row in enumerate(reader):
                if i == 0:
                    continue
                mv.push(dict(pRet=float(row[0]), mRet=float(row[1]), riskFree=float(row[2])))
                if i == 1:
                    self.assertTrue(np.all(np.isnan(mv.result())))
                if i >= window:
                    calculatedAlpha, calculatedBeta = mv.result()
                    expectedBeta = float(row[8])
                    self.assertAlmostEqual(calculatedBeta, expectedBeta, 8, "at index {0:d}\n"
                                                                            "Beta expected:   {1:f}\n"
                                                                            "Beta calculated: {2:f}".format(i,
                                                                                                            expectedBeta,
                                                                                                            calculatedBeta))
                    expectedAlpha = float(row[9])
                    self.assertAlmostEqual(calculatedAlpha, expectedAlpha, 8, "at index {0:d}\n"
                                                                              "Alpha expected:   {1:f}\n"
                                                                              "Alpha calculated: {2:f}".format(i,
                                                                                                               expectedAlpha,
                                                                                                               calculatedAlpha))
    def testListedAndCompoundedOperator(self):
        maClose = MovingAverage(20, 'close')
        maOpen = MovingAverage(10, 'open')
        maRf = MovingAverage(10, 'rf')
        listHolder = MovingAverage(20, 'close') ^ MovingAverage(
            10, 'open') ^ MovingAverage(10, 'rf')
        mc = MovingAlphaBeta(20, listHolder)

        maCloseContainer = deque(maxlen=20)
        maOpenContainer = deque(maxlen=20)

        for i, (open, close, rf) in enumerate(
                zip(self.sampleOpen, self.sampleClose, self.sampleRf)):
            data = {'close': close, 'open': open, 'rf': rf}
            maClose.push(data)
            maOpen.push(data)
            maRf.push(data)
            mc.push(data)
            maCloseContainer.append(maClose.result() - maRf.result())
            maOpenContainer.append(maOpen.result() - maRf.result())

            if i >= 2:
                expected = linregress(maOpenContainer, maCloseContainer)
                calculated = mc.result()

                # check alpha
                self.assertAlmostEqual(
                    expected[1], calculated[0], 10, "at index {0:d}\n"
                    "expected alpha:   {1:f}\n"
                    "calculated alpha: {2:f}".format(i, expected[1],
                                                     calculated[0]))

                # check beta
                self.assertAlmostEqual(
                    expected[0], calculated[1], 10, "at index {0:d}\n"
                    "expected beta:   {1:f}\n"
                    "calculated beta: {2:f}".format(i, expected[0],
                                                    calculated[1]))