Exemplo n.º 1
0
    def testValueHolderCompounding(self):
        window = 10
        ma1 = SecurityMovingAverage(window, 'close')
        compounded1 = SecurityMovingMax(2, ma1)
        compounded2 = SecurityMovingAverage(2, ma1)

        self.assertEqual(compounded1.window, window + 2)

        container = [np.nan, np.nan]
        for i in range(len(self.aapl['close'])):
            data = {
                'aapl': {
                    'close': self.aapl['close'][i],
                    'open': self.aapl['open'][i]
                }
            }
            ma1.push(data)
            compounded1.push(data)
            compounded2.push(data)

            container[i % 2] = ma1.value['aapl']

            if i >= 1:
                self.assertAlmostEqual(max(container),
                                       compounded1.value['aapl'], 12)
                self.assertAlmostEqual(np.mean((container)),
                                       compounded2.value['aapl'], 12)
    def testSecurityMovingMax(self):
        window = 10
        ma1 = SecurityMovingMax(window, ['close'], ['aapl', 'ibm'])

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

            value = ma1.value
            for name in value:
                expected = np.max(self.dataSet[name]['close'][start:(i + 1)])
                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):
            _ = SecurityMovingMax(window, ['close', 'open'], ['aapl', 'ibm'])
    def testSecurityMovingMax(self):
        window = 10
        ma1 = SecurityMovingMax(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]))
            ma1.push(data)
            if i < window:
                start = 0
            else:
                start = i + 1 - window

            value = ma1.value
            for name in value.index():
                expected = np.max(self.dataSet[name]['close'][start:(i + 1)])
                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):
            _ = SecurityMovingMax(window, ['close', 'open'])
 def testDependencyCalculationOnCompoundedValueHolder(self):
     h = SecurityMovingMax(5, SecurityMovingAverage(10, 'close') + SecurityMovingAverage(20, 'open'))
     h.push({'aapl': {'close': 5}})
     expected = {'aapl': ['close', 'open']}
     calculated = h.dependency
     for name in expected:
         self.assertEqual(set(calculated[name]), set(expected[name]))
 def testDependencyCalculationOnCompoundedValueHolder(self):
     h = SecurityMovingMax(
         5,
         SecurityMovingAverage(10, 'close') +
         SecurityMovingAverage(20, 'open'))
     h.push({'aapl': {'close': 5}})
     expected = {'aapl': ['close', 'open']}
     calculated = h.dependency
     for name in expected:
         self.assertEqual(set(calculated[name]), set(expected[name]))
Exemplo n.º 6
0
    def testSecurityValueHolderIsFull(self):
        test = SecurityMovingMax(2, x='close')

        data = {'aapl': {'close': 1.0}, 'ibm': {'close': 2.0}}
        test.push(data)
        self.assertEqual(test.isFull, False)

        data = {'aapl': {'close': 1.0}, 'ibm': {'close': 13.0}}
        test.push(data)
        self.assertEqual(test.isFull, True)
    def testValueHolderCompounding(self):
        window = 10
        ma1 = SecurityMovingAverage(window, 'close')
        compounded1 = SecurityMovingMax(2, ma1)
        compounded2 = SecurityMovingAverage(2, ma1)

        self.assertEqual(compounded1.window, window + 1)

        container = [np.nan, np.nan]
        for i in range(len(self.aapl['close'])):
            data = {'aapl': {'close': self.aapl['close'][i], 'open': self.aapl['open'][i]}}
            ma1.push(data)
            compounded1.push(data)
            compounded2.push(data)

            container[i % 2] = ma1.value['aapl']

            if i >= 1:
                self.assertAlmostEqual(max(container), compounded1.value['aapl'], 12)
                self.assertAlmostEqual(np.mean((container)), compounded2.value['aapl'], 12)
    def testSecurityMovingMax(self):
        window = 10
        ma1 = SecurityMovingMax(window, ['close'])

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

            value = ma1.value
            for name in value.index:
                expected = np.max(self.dataSet[name]['close'][start:(i + 1)])
                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):
            _ = SecurityMovingMax(window, ['close', 'open'])