Esempio n. 1
0
 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'])
Esempio n. 2
0
class MovingSharp(StatefulValueHolder):
    def __init__(self, window, dependency=('ret', 'riskFree')):
        super(MovingSharp, self).__init__(window, dependency)
        self._mean = MovingAverage(window, dependency='x')
        self._var = MovingVariance(window, dependency='x', isPopulation=False)

    def push(self, data):
        value = super(MovingSharp, self).push(data)
        ret = value[0]
        benchmark = value[1]
        data = {'x': ret - benchmark}
        self._mean.push(data)
        self._var.push(data)

    def result(self):
        return self._mean.result() / math.sqrt(self._var.result())
Esempio n. 3
0
class MovingSharp(StatefulValueHolder):
    def __init__(self, window, dependency=('ret', 'riskFree')):
        super(MovingSharp, self).__init__(window, dependency)
        self._mean = MovingAverage(window, dependency='x')
        self._var = MovingVariance(window, dependency='x', isPopulation=False)

    def push(self, data):
        value = super(MovingSharp, self).push(data)
        ret = value[0]
        benchmark = value[1]
        data = {'x': ret - benchmark}
        self._mean.push(data)
        self._var.push(data)

    def result(self):
        return self._mean.result() / math.sqrt(self._var.result())
Esempio n. 4
0
    def testMultiplyOperator(self):
        mv5 = MovingVariance(5, 'close')
        average = Average('open')
        mulRes = MovingVariance(5, 'close') * Average('open')

        for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)):
            data = {'close': close, 'open': open}
            mv5.push(data)
            average.push(data)
            mulRes.push(data)

            if i >= 1:
                expected = mv5.result() * average.result()
                calculated = mulRes.result()
                self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                                 "expected:   {1:f}\n"
                                                                 "calculated: {2:f}".format(i, expected, calculated))
Esempio n. 5
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
Esempio n. 6
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 testMultiplyOperator(self):
        mv5 = MovingVariance(5, 'close')
        average = Average('open')
        mulRes = MovingVariance(5, 'close') * Average('open')
        concated = (Average('open') ^ mv5) * Average('open')
        concated2 = Average('open') * (Average('open') ^ mv5)

        for i, (open, close) in enumerate(zip(self.sampleOpen, self.sampleClose)):
            data = {'close': close, 'open': open}
            mv5.push(data)
            average.push(data)
            mulRes.push(data)
            concated.push(data)
            concated2.push(data)

            if i >= 1:
                expected = mv5.result() * average.result()
                calculated = mulRes.result()
                self.assertAlmostEqual(calculated, expected, 12, "at index {0:d}\n"
                                                                 "expected:   {1:f}\n"
                                                                 "calculated: {2:f}".format(i, expected, calculated))

                expected = (average.result() * average.result(), mv5.result() * average.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]))

                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]))
Esempio n. 8
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
Esempio n. 9
0
 def __init__(self, window, dependency=('ret', 'riskFree')):
     super(MovingSharp, self).__init__(window, dependency)
     self._mean = MovingAverage(window, dependency='x')
     self._var = MovingVariance(window, dependency='x', isPopulation=False)
Esempio n. 10
0
 def __init__(self, window, dependency=('ret', 'riskFree')):
     super(MovingSharp, self).__init__(window, dependency)
     self._mean = MovingAverage(window, dependency='x')
     self._var = MovingVariance(window, dependency='x', isPopulation=False)