Exemplo n.º 1
0
    def testSecurityMovingRank(self):
        window = 10
        mq = SecurityMovingRank(window, ['close'])

        for i in range(len(self.aapl['close'])):
            data = dict(aapl=dict(close=self.aapl['close'][i],
                                  open=self.aapl['open'][i]),
                        ibm=dict(close=self.ibm['close'][i],
                                 open=self.ibm['open'][i]))
            mq.push(data)
            if i < window:
                start = 0
            else:
                start = i + 1 - window

            if i < 1:
                continue

            value = mq.value
            for name in value.index():
                con = self.dataSet[name]['close'][start:(i + 1)]
                sorted_con = sorted(con)
                expected = sorted_con.index(self.dataSet[name]['close'][i])
                calculated = value[name]
                self.assertAlmostEqual(
                    expected, calculated, 12, 'at index {0}\n'
                    'expected:   {1:.12f}\n'
                    'calculated: {2:.12f}'.format(i, expected, calculated))
    def testSecurityMovingRank(self):
        window = 100
        mr = SecurityMovingRank(window, 'x')
        for i in range(len(self.aapl['close'])):
            data = {
                'aapl': {
                    'y': self.aapl['close'][i],
                    'x': self.aapl['open'][i]
                },
                'ibm': {
                    'y': self.ibm['close'][i],
                    'x': self.ibm['open'][i]
                }
            }
            mr.push(data)

            if i >= window - 1:
                for name in mr.value.index():
                    series_x = getattr(self,
                                       name)['open'][i - window + 1:i + 1]
                    expected_res = np.argsort(series_x.argsort())[-1]
                    self.assertAlmostEqual(mr.value[name], expected_res, msg= \
                        "at index {0} and symbol {1}\n"
                        "expected:   {2}\n"
                        "calculated: {3}".format(i, name, expected_res, mr.value[name]))
    def testSecurityMovingRankPickle(self):
        ma = SecurityMovingRank(10, 'x')

        data = dict(aapl=dict(x=1., y=2), ibm=dict(x=2., y=3))
        data2 = dict(aapl=dict(x=2., y=3), ibm=dict(x=3., y=4))

        ma.push(data)
        ma.push(data2)

        with tempfile.NamedTemporaryFile('w+b', delete=False) as f:
            pickle.dump(ma, f)

        with open(f.name, 'rb') as f2:
            pickled = pickle.load(f2)
        os.unlink(f.name)

        for i, v in enumerate(np.random.rand(20)):
            data['aapl']['x'] = v
            data['aapl']['y'] = v + 0.1
            data['ibm']['x'] = v + 0.1
            data['ibm']['y'] = v + 0.2
            ma.push(data)
            pickled.push(data)

            if i >= 10:
                for name in ma.value.index():
                    self.assertAlmostEqual(ma.value[name], pickled.value[name])
    def testSecurityMovingRankDeepcopy(self):
        ma = SecurityMovingRank(10, 'x')

        data = dict(aapl=dict(x=1., y=2), ibm=dict(x=2., y=3))
        data2 = dict(aapl=dict(x=2., y=3), ibm=dict(x=3., y=4))

        ma.push(data)
        ma.push(data2)

        copied = copy.deepcopy(ma)

        for i, v in enumerate(np.random.rand(20)):
            data['aapl']['x'] = v
            data['aapl']['y'] = v + 0.1
            data['ibm']['x'] = v + 0.1
            data['ibm']['y'] = v + 0.2
            ma.push(data)
            copied.push(data)
            if i >= 10:
                for name in ma.value.index():
                    self.assertAlmostEqual(ma.value[name], copied.value[name])
Exemplo n.º 5
0
def MRANK(window, x='x'):
    return SecurityMovingRank(window, x)
Exemplo n.º 6
0
def RANK(window, dependency='x'):
    return SecurityMovingRank(window, dependency)