def testCSRankedSecurityValueHolder(self): benchmark = SecurityLatestValueHolder(dependency='close') rankHolder = CSRankedSecurityValueHolder(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) rankHolder.push(data) benchmarkValues = benchmark.value np.testing.assert_array_almost_equal(benchmarkValues.rank().values, rankHolder.value.values)
def testCSRankedSecurityValueHolderWithGroups(self): benchmark = SecurityLatestValueHolder(x='close') groups = SecurityLatestValueHolder(x='ind') rankHolder = CSRankedSecurityValueHolder(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) rankHolder.push(data) benchmarkValues = benchmark.value groups = {'aapl': 1., 'ibm': 1., 'goog': 2., 'baba': 2.} expected_rank = pd.Series(benchmarkValues.to_dict()).groupby(groups).rank().values np.testing.assert_array_almost_equal(expected_rank, rankHolder.value.values)
def CSRank(x, groups=None): return CSRankedSecurityValueHolder(x, groups)
def CSRank(dependency): return CSRankedSecurityValueHolder(dependency)