def testSecurityMovingLogReturn(self):
        window = 10
        ma1 = SecurityMovingLogReturn(window, ['close'])
        self.newDataSet = copy.deepcopy(self.dataSet)
        self.newDataSet['aapl']['close'] = np.exp(self.newDataSet['aapl']['close'])
        self.newDataSet['aapl']['open'] = np.exp(self.newDataSet['aapl']['open'])
        self.newDataSet['ibm']['close'] = np.exp(self.newDataSet['ibm']['close'])
        self.newDataSet['ibm']['open'] = np.exp(self.newDataSet['ibm']['open'])

        for i in range(len(self.aapl['close'])):
            data = {'aapl': {'close': self.newDataSet['aapl']['close'][i], 'open': self.newDataSet['aapl']['open'][i]}}
            data['ibm'] = {'close': self.newDataSet['ibm']['close'][i], 'open': self.newDataSet['ibm']['open'][i]}
            ma1.push(data)
            if i < 10:
                start = 0
            else:
                start = i - window

            value = ma1.value
            for name in value.index:
                sampled = self.newDataSet[name]['close'][start:(i + 1)]
                if i >= 10:
                    expected = math.log(sampled[-1] / sampled[0])
                    calculated = value[name]
                    self.assertAlmostEqual(expected, calculated, 12, 'at index {0}\n'
                                                                     'expected:   {1:.12f}\n'
                                                                     'calculated: {2:.12f}'.format(i, expected,
                                                                                                   calculated))

        with self.assertRaises(ValueError):
            _ = SecurityMovingLogReturn(window, ['close', 'open'])
Exemplo n.º 2
0
    def testSecurityMovingLogReturn(self):
        window = 10
        ma1 = SecurityMovingLogReturn(window, ['close'])
        self.newDataSet = copy.deepcopy(self.dataSet)
        self.newDataSet['aapl']['close'] = np.exp(
            self.newDataSet['aapl']['close'])
        self.newDataSet['aapl']['open'] = np.exp(
            self.newDataSet['aapl']['open'])
        self.newDataSet['ibm']['close'] = np.exp(
            self.newDataSet['ibm']['close'])
        self.newDataSet['ibm']['open'] = np.exp(self.newDataSet['ibm']['open'])

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

            value = ma1.value
            for name in value.index():
                sampled = self.newDataSet[name]['close'][start:(i + 1)]
                if i >= 10:
                    expected = math.log(sampled[-1] / sampled[0])
                    calculated = value[name]
                    self.assertAlmostEqual(
                        expected, calculated, 12, 'at index {0}\n'
                        'expected:   {1:.12f}\n'
                        'calculated: {2:.12f}'.format(i, expected, calculated))
Exemplo n.º 3
0
    def testSecurityMovingLogReturnPickle(self):
        ma = SecurityMovingLogReturn(10, ['x'])

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

        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['ibm']['x'] = v + 1.
            ma.push(data)
            pickled.push(data)

            if i >= 10:
                for name in ma.value.index():
                    self.assertAlmostEqual(ma.value[name], pickled.value[name])
    def testSecurityMovingLogReturn(self):
        window = 10
        ma1 = SecurityMovingLogReturn(window, ['close'], ['aapl', 'ibm'])
        self.newDataSet = copy.deepcopy(self.dataSet)
        self.newDataSet['aapl']['close'] = np.exp(
            self.newDataSet['aapl']['close'])
        self.newDataSet['aapl']['open'] = np.exp(
            self.newDataSet['aapl']['open'])
        self.newDataSet['ibm']['close'] = np.exp(
            self.newDataSet['ibm']['close'])
        self.newDataSet['ibm']['open'] = np.exp(self.newDataSet['ibm']['open'])

        for i in range(len(self.aapl['close'])):
            data = {
                'aapl': {
                    'close': self.newDataSet['aapl']['close'][i],
                    'open': self.newDataSet['aapl']['open'][i]
                }
            }
            data['ibm'] = {
                'close': self.newDataSet['ibm']['close'][i],
                'open': self.newDataSet['ibm']['open'][i]
            }
            ma1.push(data)
            if i < 10:
                start = 0
            else:
                start = i - window

            value = ma1.value
            for name in value.index:
                sampled = self.newDataSet[name]['close'][start:(i + 1)]
                if i >= 10:
                    expected = math.log(sampled[-1] / sampled[0])
                    calculated = value[name]
                    self.assertAlmostEqual(
                        expected, calculated, 12, 'at index {0}\n'
                        'expected:   {1:.12f}\n'
                        'calculated: {2:.12f}'.format(i, expected, calculated))

        with self.assertRaises(ValueError):
            _ = SecurityMovingLogReturn(window, ['close', 'open'],
                                        ['aapl', 'ibm'])
Exemplo n.º 5
0
    def testSecurityMovingLogReturnDeepcopy(self):
        ma = SecurityMovingLogReturn(10, ['x'])

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

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

        copied = copy.deepcopy(ma)

        for i, v in enumerate(np.random.rand(20)):
            data['aapl']['x'] = v
            data['ibm']['x'] = v + 1.
            ma.push(data)
            copied.push(data)
            if i >= 10:
                for name in ma.value.index():
                    self.assertAlmostEqual(ma.value[name], copied.value[name])