Beispiel #1
0
class MovingAlphaBeta(StatefulValueHolder):
    def __init__(self, window, dependency=('pRet', 'mRet', 'riskFree')):
        self._returnSize = 2
        super(MovingAlphaBeta, self).__init__(window, dependency)
        self._pReturnMean = MovingAverage(window, dependency='x')
        self._mReturnMean = MovingAverage(window, dependency='y')
        self._pReturnVar = MovingVariance(window, dependency='x')
        self._mReturnVar = MovingVariance(window, dependency='y')
        self._correlationHolder = MovingCorrelation(window,
                                                    dependency=['x', 'y'])

    def push(self, data):
        value = super(MovingAlphaBeta, self).push(data)
        if value is None:
            return
        pReturn = value[0]
        mReturn = value[1]
        rf = value[2]
        data = {'x': pReturn - rf, 'y': mReturn - rf}
        self._pReturnMean.push(data)
        self._mReturnMean.push(data)
        self._pReturnVar.push(data)
        self._mReturnVar.push(data)
        self._correlationHolder.push(data)

    def result(self):
        corr = self._correlationHolder.result()
        pStd = math.sqrt(self._pReturnVar.result())
        mStd = math.sqrt(self._mReturnVar.result())
        beta = corr * pStd / mStd
        alpha = self._pReturnMean.result() - beta * self._mReturnMean.result()
        return alpha, beta
Beispiel #2
0
class MovingAlphaBeta(StatefulValueHolder):
    def __init__(self, window, dependency=('pRet', 'mRet', 'riskFree')):
        self._returnSize = 2
        super(MovingAlphaBeta, self).__init__(window, dependency)
        self._pReturnMean = MovingAverage(window, dependency='x')
        self._mReturnMean = MovingAverage(window, dependency='y')
        self._pReturnVar = MovingVariance(window, dependency='x')
        self._mReturnVar = MovingVariance(window, dependency='y')
        self._correlationHolder = MovingCorrelation(window, dependency=['x', 'y'])

    def push(self, data):
        value = super(MovingAlphaBeta, self).push(data)
        if value is None:
            return
        pReturn = value[0]
        mReturn = value[1]
        rf = value[2]
        data = {'x': pReturn - rf, 'y': mReturn - rf}
        self._pReturnMean.push(data)
        self._mReturnMean.push(data)
        self._pReturnVar.push(data)
        self._mReturnVar.push(data)
        self._correlationHolder.push(data)

    def result(self):
        corr = self._correlationHolder.result()
        pStd = math.sqrt(self._pReturnVar.result())
        mStd = math.sqrt(self._mReturnVar.result())
        beta = corr * pStd / mStd
        alpha = self._pReturnMean.result() - beta * self._mReturnMean.result()
        return alpha, beta
    def testDivOperator(self):
        mc5 = MovingCorrelation(5, ['open', 'close'])
        minum = Minimum('open')
        divRes = Minimum('open') / MovingCorrelation(5, ['open', 'close'])
        concated = (Minimum('open') ^ MovingCorrelation(5, ['open', 'close'])) / MovingCorrelation(5, ['open', 'close'])
        concated2 = MovingCorrelation(5, ['open', 'close']) / (
        Minimum('open') ^ MovingCorrelation(5, ['open', 'close']))

        for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)):
            data = {'close': close, 'open': open}
            mc5.push(data)
            minum.push(data)
            divRes.push(data)
            concated.push(data)
            concated2.push(data)

            if i >= 1:
                expected = minum.result() / mc5.result()
                calculated = divRes.result()
                self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                                 "expected:   {1:f}\n"
                                                                 "calculated: {2:f}".format(i, expected, calculated))

                expected = (minum.result() / mc5.result(), mc5.result() / mc5.result())
                calculated = concated.result()
                self.assertAlmostEqual(calculated[0], expected[0], 12, "at index {0:d}\n"
                                                                       "expected:   {1:f}\n"
                                                                       "calculated: {2:f}".format(i, expected[0],
                                                                                                  calculated[0]))
                self.assertAlmostEqual(calculated[1], expected[1], 12, "at index {0:d}\n"
                                                                       "expected:   {1:f}\n"
                                                                       "calculated: {2:f}".format(i, expected[1],
                                                                                                  calculated[1]))

                expected = (mc5.result() / minum.result(), mc5.result() / mc5.result())
                calculated = concated2.result()
                self.assertAlmostEqual(calculated[0], expected[0], 12, "at index {0:d}\n"
                                                                       "expected:   {1:f}\n"
                                                                       "calculated: {2:f}".format(i, expected[0],
                                                                                                  calculated[0]))
                self.assertAlmostEqual(calculated[1], expected[1], 12, "at index {0:d}\n"
                                                                       "expected:   {1:f}\n"
                                                                       "calculated: {2:f}".format(i, expected[1],
                                                                                                  calculated[1]))
    def testDivOperator(self):
        mc5 = MovingCorrelation(5, ['open', 'close'])
        minum = Minimum('open')
        divRes = Minimum('open') / MovingCorrelation(5, ['open', 'close'])
        concated = (Minimum('open') ^ MovingCorrelation(
            5, ['open', 'close'])) / MovingCorrelation(5, ['open', 'close'])
        concated2 = MovingCorrelation(5, ['open', 'close']) / (
            Minimum('open') ^ MovingCorrelation(5, ['open', 'close']))

        for i, (open,
                close) in enumerate(zip(self.sampleOpen, self.sampleClose)):
            data = {'close': close, 'open': open}
            mc5.push(data)
            minum.push(data)
            divRes.push(data)
            concated.push(data)
            concated2.push(data)

            if i >= 1:
                expected = minum.result() / mc5.result()
                calculated = divRes.result()
                self.assertAlmostEqual(
                    calculated, expected, 12, "at index {0:d}\n"
                    "expected:   {1:f}\n"
                    "calculated: {2:f}".format(i, expected, calculated))

                expected = (minum.result() / mc5.result(),
                            mc5.result() / mc5.result())
                calculated = concated.result()
                self.assertAlmostEqual(
                    calculated[0], expected[0], 12, "at index {0:d}\n"
                    "expected:   {1:f}\n"
                    "calculated: {2:f}".format(i, expected[0], calculated[0]))
                self.assertAlmostEqual(
                    calculated[1], expected[1], 12, "at index {0:d}\n"
                    "expected:   {1:f}\n"
                    "calculated: {2:f}".format(i, expected[1], calculated[1]))

                expected = (mc5.result() / minum.result(),
                            mc5.result() / mc5.result())
                calculated = concated2.result()
                self.assertAlmostEqual(
                    calculated[0], expected[0], 12, "at index {0:d}\n"
                    "expected:   {1:f}\n"
                    "calculated: {2:f}".format(i, expected[0], calculated[0]))
                self.assertAlmostEqual(
                    calculated[1], expected[1], 12, "at index {0:d}\n"
                    "expected:   {1:f}\n"
                    "calculated: {2:f}".format(i, expected[1], calculated[1]))
Beispiel #5
0
    def testDivOperator(self):
        mc5 = MovingCorrelation(5, 'open', 'close')
        minum = Min('open')
        divRes = Min('open') / MovingCorrelation(5, 'open', 'close')

        for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)):
            data = {'close': close, 'open': open}
            mc5.push(data)
            minum.push(data)
            divRes.push(data)

            if i >= 1:
                expected = minum.result() / mc5.result()
                calculated = divRes.result()
                self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                                 "expected:   {1:f}\n"
                                                                 "calculated: {2:f}".format(i, expected, calculated))
Beispiel #6
0
class MovingAlphaBeta(StatefulValueHolder):
    def __init__(self, window, dependency=('pRet', 'mRet', 'riskFree')):
        super(MovingAlphaBeta, self).__init__(window, dependency)
        self._returnSize = 2
        self._pReturnMean = MovingAverage(window, dependency='x')
        self._mReturnMean = MovingAverage(window, dependency='y')
        self._pReturnVar = MovingVariance(window, dependency='x')
        self._mReturnVar = MovingVariance(window, dependency='y')
        self._correlationHolder = MovingCorrelation(window, dependency=['x', 'y'])

    def push(self, data):
        value = super(MovingAlphaBeta, self).push(data)
        if np.any(np.isnan(value)):
            return np.nan
        pReturn = value[0]
        mReturn = value[1]
        rf = value[2]
        data = {'x': pReturn - rf, 'y': mReturn - rf}
        self._pReturnMean.push(data)
        self._mReturnMean.push(data)
        self._pReturnVar.push(data)
        self._mReturnVar.push(data)
        self._correlationHolder.push(data)

    def result(self):
        corr = self._correlationHolder.result()
        tmp = self._pReturnVar.result()
        if not isClose(tmp, 0.):
            pStd = math.sqrt(tmp)
        else:
            pStd = 0.
        tmp = self._mReturnVar.result()
        if not isClose(tmp, 0.):
            mStd = math.sqrt(tmp)
        else:
            mStd = 0.

        if not isClose(tmp, 0.):
            beta = corr * pStd / mStd
        else:
            beta = 0.
        alpha = self._pReturnMean.result() - beta * self._mReturnMean.result()
        return alpha, beta