Пример #1
0
    def testCSPercentileSecurityValueHolderWithGroups(self):
        benchmark = SecurityLatestValueHolder(x='close')
        groups = SecurityLatestValueHolder(x='ind')
        perHolder = CSPercentileSecurityValueHolder(benchmark, groups)

        for i in range(len(self.datas['aapl']['close'])):
            data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['aapl'][Factors.OPEN][i],
                             'ind': 1.},
                    'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                            Factors.OPEN: self.datas['ibm'][Factors.OPEN][i],
                            'ind': 1.},
                    'goog': {Factors.CLOSE: self.datas['goog'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['goog'][Factors.OPEN][i],
                             'ind': 2.},
                    'baba': {Factors.CLOSE: self.datas['baba'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['baba'][Factors.OPEN][i],
                             'ind': 2.}}
            benchmark.push(data)
            perHolder.push(data)
            benchmarkValues = benchmark.value
            groups = {'aapl': 1., 'ibm': 1., 'goog': 2., 'baba': 2.}
            expected_rank = pd.Series(benchmarkValues.to_dict()).groupby(groups) \
                .transform(lambda x: x.rank().values / len(x))
            np.testing.assert_array_almost_equal(expected_rank, perHolder.value.values)
Пример #2
0
    def testCSPercentileSecurityValueHolder(self):
        benchmark = SecurityLatestValueHolder(x='close')
        perHolder = CSPercentileSecurityValueHolder(benchmark)

        for i in range(len(self.datas['aapl']['close'])):
            data = {
                'aapl': {
                    Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]
                },
                'ibm': {
                    Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]
                },
                'goog': {
                    Factors.CLOSE: self.datas['goog'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['goog'][Factors.OPEN][i]
                },
                'baba': {
                    Factors.CLOSE: self.datas['baba'][Factors.CLOSE][i],
                    Factors.OPEN: self.datas['baba'][Factors.OPEN][i]
                }
            }
            benchmark.push(data)
            perHolder.push(data)
            benchmarkValues = benchmark.value
            np.testing.assert_array_almost_equal(
                benchmarkValues.rank().values / (len(data) - 1),
                perHolder.value.values)
    def testCSPercentileSecurityValueHolder(self):
        percent = 50
        benchmark = SecurityLatestValueHolder(dependency='close')
        perHolder = CSPercentileSecurityValueHolder(percent, benchmark)

        for i in range(len(self.datas['aapl']['close'])):
            data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i],
                             Factors.OPEN: self.datas['aapl'][Factors.OPEN][i]},
                    'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i],
                            Factors.OPEN: self.datas['ibm'][Factors.OPEN][i]}}
            benchmark.push(data)
            perHolder.push(data)
            benchmarkValues = benchmark.value
            np.testing.assert_array_almost_equal(np.percentile(benchmarkValues.values, 50), perHolder.value.values)
Пример #4
0
def CSQuantiles(x, groups=None):
    return CSPercentileSecurityValueHolder(x, groups)
Пример #5
0
def CSPercentile(percent, dependency):
    return CSPercentileSecurityValueHolder(percent, dependency)