def testCompoundedSecurityValueHolder(self):
        ma = SecurityMovingAverage(2, 'close')
        compounded = ma >> SecurityMovingMax(3)

        container = {'aapl': deque(maxlen=3), 'ibm': deque(maxlen=3)}
        expected = {'aapl': 0.0, 'ibm': 0.0}
        for i in range(len(self.datas['aapl']['close'])):
            data = {
                'aapl': {
                    Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i]
                },
                'ibm': {
                    Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i]
                }
            }
            ma.push(data)
            maRes = ma.value
            for name in maRes.index:
                container[name].append(maRes[name])
                expected[name] = max(container[name])

            compounded.push(data)
            calculated = compounded.value
            for name in calculated.index:
                self.assertAlmostEqual(
                    expected[name], calculated[name], 12,
                    "for {0} at index {1}\n"
                    "expected:   {2}\n"
                    "calculated: {3}".format(name, i, expected[name],
                                             calculated[name]))
    def testBasicFunctions(self):
        window = 10
        pNames = 'close'
        symbolList = ['aapl', 'ibm']
        testValueHolder = SecurityMovingAverage(window, pNames)

        testValueHolder.push({'aapl': {'close': 1.0}, 'ibm': {'close': 2.0}})

        self.assertEqual(set(testValueHolder.symbolList), set(symbolList))
        self.assertEqual(testValueHolder.window, window)

        # test binary operated value holder
        window2 = 5
        pNames2 = 'open'
        test2 = SecurityMovingMax(window2, pNames2)
        binaryValueHolder = testValueHolder + test2

        self.assertEqual(binaryValueHolder.window, max(window, window2))

        # test compounded operated value holder
        test3 = SecurityMovingMax(window2, testValueHolder)
        self.assertEqual(test3.window, window + window2)

        # test compounded twice
        test4 = SecurityMovingMax(window2, test3)
        self.assertEqual(test4.window, window + 2 * window2)
    def testSecurityMovingAverage(self):
        window = 10
        ma1 = SecurityMovingAverage(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.mean(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):
            _ = SecurityMovingAverage(window, ['close', 'open'])
    def testDividedSecurityValueHolders(self):
        window1 = 10
        window2 = 5
        dependency1 = Factors.CLOSE
        dependency2 = Factors.OPEN
        ma = SecurityMovingAverage(window1, dependency1)
        mm = SecurityMovingSum(window2, dependency2)
        combined = ma / mm

        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]
                }
            }
            ma.push(data)
            mm.push(data)
            combined.push(data)

            expected = ma.value / mm.value
            calculated = combined.value
            for name in expected.index:
                self.assertAlmostEqual(expected[name], calculated[name], 12)
Пример #5
0
    def testItemizedValueHolder(self):
        window = 10
        pNames = 'close'
        test = SecurityMovingAverage(window, pNames)
        test.push({
            'aapl': {
                'close': 10.0
            },
            'ibm': {
                'close': 15.0
            },
            'goog': {
                'close': 17.0
            }
        })
        test.push({
            'aapl': {
                'close': 12.0
            },
            'ibm': {
                'close': 10.0
            },
            'goog': {
                'close': 13.0
            }
        })

        expected = {'ibm': 12.5, 'goog': 15.0}
        for s in expected:
            self.assertAlmostEqual(test[s], expected[s])
Пример #6
0
    def testSecurityMovingAverageWithErrorValues(self):
        window = 5
        to_average = SecurityLatestValueHolder(
            'open') / SecurityLatestValueHolder('close')
        ma = SecurityMovingAverage(window, to_average)

        container = {'aapl': deque(maxlen=window), 'ibm': deque(maxlen=window)}

        for i in range(len(self.aapl['close'])):
            data = dict(aapl=dict(
                close=1.0 if self.aapl['close'][i] >= 1.0 else 0.0,
                open=self.aapl['open'][i]),
                        ibm=dict(close=self.ibm['close'][i],
                                 open=self.ibm['open'][i]))
            ma.push(data)

            for name in data:
                res = data[name]['open'] / data[name]['close'] if data[name][
                    'close'] != 0. else np.nan
                if res != np.inf and res != -np.inf and not math.isnan(res):
                    container[name].append(res)

            value = ma.value

            for name in value.index():
                expected = np.mean(container[name])
                calculated = value[name]
                self.assertAlmostEqual(
                    expected, calculated, 12, 'at index {0}\n'
                    'expected:   {1:.12f}\n'
                    'calculated: {2:.12f}'.format(i, expected, calculated))
Пример #7
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 testRDividedSecurityValueHoldersWithScalar(self):
        window = 10
        dependency = 'close'
        ma = SecurityMovingAverage(window, dependency)
        combined = ma / 2.0
        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]}}
            ma.push(data)
            combined.push(data)

            expected = ma.value / 2.0
            calculated = combined.value
            for name in expected.index():
                self.assertAlmostEqual(expected[name], calculated[name], 12)
    def testRDividedSecurityValueHoldersWithScalar(self):
        window = 10
        dependency = ['close']
        ma = SecurityMovingAverage(window, dependency, ['aapl', 'ibm'])
        combined = ma / 2.0
        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]}}
            ma.push(data)
            combined.push(data)

            expected = ma.value / 2.0
            calculated = combined.value
            for name in expected:
                self.assertAlmostEqual(expected[name], calculated[name], 12)
    def testBasicFunctions(self):
        window = 10
        pNames = ['close']
        symbolList = ['aapl', 'ibm']
        testValueHolder = SecurityMovingAverage(window, pNames)

        testValueHolder.push({'aapl': {'close': 1.0}, 'ibm': {'close': 2.0}})

        dependency = {
            name: pNames for name in symbolList
            }

        self.assertEqual(set(testValueHolder.symbolList), set(symbolList))
        self.assertEqual(testValueHolder.dependency, dependency)
        self.assertEqual(testValueHolder.valueSize, 1)
        self.assertEqual(testValueHolder.window, window)

        # test binary operated value holder
        window2 = 5
        pNames2 = ['open']
        test2 = SecurityMovingMax(window2, pNames2)
        binaryValueHolder = testValueHolder + test2
        dependency2 = {
            name: pNames + pNames2 for name in symbolList
            }

        self.assertEqual(set(binaryValueHolder.symbolList), set(symbolList))
        for name in dependency2:
            self.assertEqual(set(binaryValueHolder.dependency[name]), set(dependency2[name]))
        self.assertEqual(binaryValueHolder.valueSize, 1)
        self.assertEqual(binaryValueHolder.window, max(window, window2))

        # test compounded operated value holder
        test3 = SecurityMovingMax(window2, testValueHolder)
        self.assertEqual(set(test3.symbolList), set(symbolList))
        self.assertEqual(test3.dependency, dependency)
        self.assertEqual(test3.valueSize, 1)
        self.assertEqual(test3.window, window + window2 - 1)

        # test compounded twice
        test4 = SecurityMovingMax(window2, test3)
        self.assertEqual(set(test4.symbolList), set(symbolList))
        self.assertEqual(test4.dependency, dependency)
        self.assertEqual(test4.valueSize, 1)
        self.assertEqual(test4.window, window + 2 * window2 - 2)
    def testBasicFunctions(self):
        window = 10
        pNames = ['close']
        symbolList = ['aapl', 'ibm']
        testValueHolder = SecurityMovingAverage(window, pNames)

        testValueHolder.push({'aapl': {'close': 1.0}, 'ibm': {'close': 2.0}})

        dependency = {name: pNames for name in symbolList}

        self.assertEqual(set(testValueHolder.symbolList), set(symbolList))
        self.assertEqual(testValueHolder.dependency, dependency)
        self.assertEqual(testValueHolder.valueSize, 1)
        self.assertEqual(testValueHolder.window, window)

        # test binary operated value holder
        window2 = 5
        pNames2 = ['open']
        test2 = SecurityMovingMax(window2, pNames2)
        binaryValueHolder = testValueHolder + test2
        dependency2 = {name: pNames + pNames2 for name in symbolList}

        self.assertEqual(set(binaryValueHolder.symbolList), set(symbolList))
        for name in dependency2:
            self.assertEqual(set(binaryValueHolder.dependency[name]),
                             set(dependency2[name]))
        self.assertEqual(binaryValueHolder.valueSize, 1)
        self.assertEqual(binaryValueHolder.window, max(window, window2))

        # test compounded operated value holder
        test3 = SecurityMovingMax(window2, testValueHolder)
        self.assertEqual(set(test3.symbolList), set(symbolList))
        self.assertEqual(test3.dependency, dependency)
        self.assertEqual(test3.valueSize, 1)
        self.assertEqual(test3.window, window + window2 - 1)

        # test compounded twice
        test4 = SecurityMovingMax(window2, test3)
        self.assertEqual(set(test4.symbolList), set(symbolList))
        self.assertEqual(test4.dependency, dependency)
        self.assertEqual(test4.valueSize, 1)
        self.assertEqual(test4.window, window + 2 * window2 - 2)
    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 testItemizedValueHolder(self):
        window = 10
        pNames = 'close'
        symbolList = ['AAPL', 'IBM', 'GOOG']
        test = SecurityMovingAverage(window, pNames, symbolList)
        test.push({'aapl': {'close': 10.0}, 'ibm': {'close': 15.0}, 'goog': {'close': 17.0}})
        test.push({'aapl': {'close': 12.0}, 'ibm': {'close': 10.0}, 'goog': {'close': 13.0}})

        # single named value holder
        test1 = test['IBM']
        self.assertAlmostEqual(test1, 12.5, 15)

        # multi-valued named value holder
        test2 = test['IBM', 'GOOG']
        expected = SecuritiesValues({'ibm': 12.5, 'goog':15.0})
        self.assertAlmostEqual(test2, expected)

        # wrong type of item
        with self.assertRaises(TypeError):
            _ = test[1]
    def testItemizedValueHolder(self):
        window = 10
        pNames = 'close'
        test = SecurityMovingAverage(window, pNames)
        test.push({'aapl': {'close': 10.0}, 'ibm': {'close': 15.0}, 'goog': {'close': 17.0}})
        test.push({'aapl': {'close': 12.0}, 'ibm': {'close': 10.0}, 'goog': {'close': 13.0}})

        # single named value holder
        test1 = test['ibm']
        self.assertAlmostEqual(test1, 12.5, 15)

        # multi-valued named value holder
        test2 = test['ibm', 'goog']
        expected = SecuritiesValues({'ibm': 12.5, 'goog':15.0})
        for s in test2.index:
            self.assertAlmostEqual(test2[s], expected[s])

        # wrong type of item
        with self.assertRaises(TypeError):
            _ = test[1]
    def testItemizedValueHolder(self):
        window = 10
        pNames = 'close'
        test = SecurityMovingAverage(window, pNames)
        test.push({
            'aapl': {
                'close': 10.0
            },
            'ibm': {
                'close': 15.0
            },
            'goog': {
                'close': 17.0
            }
        })
        test.push({
            'aapl': {
                'close': 12.0
            },
            'ibm': {
                'close': 10.0
            },
            'goog': {
                'close': 13.0
            }
        })

        # single named value holder
        test1 = test['ibm']
        self.assertAlmostEqual(test1, 12.5, 15)

        # multi-valued named value holder
        test2 = test['ibm', 'goog']
        expected = SecuritiesValues({'ibm': 12.5, 'goog': 15.0})
        for s in test2.index:
            self.assertAlmostEqual(test2[s], expected[s])

        # wrong type of item
        with self.assertRaises(TypeError):
            _ = test[1]
    def testDividedSecurityValueHolders(self):
        window1 = 10
        window2 = 5
        dependency1 = Factors.CLOSE
        dependency2 = Factors.OPEN
        ma = SecurityMovingAverage(window1, dependency1, ['aapl', 'ibm'])
        mm = SecurityMovingSum(window2, dependency2, ['aapl', 'ibm'])
        combined = ma / mm

        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]}}
            ma.push(data)
            mm.push(data)
            combined.push(data)

            expected = ma.value / mm.value
            calculated = combined.value
            for name in expected:
                self.assertAlmostEqual(expected[name], calculated[name], 12)
    def testGeSecurityValueHolder(self):
        filter = SecurityMovingAverage(1, 'close') >= 10.0
        ma = SecurityMovingAverage(10, 'close')[filter]

        data = {
            'aapl': {
                'close': 15.
            },
            'ibm': {
                'close': 10.
            },
            'goog': {
                'close': 7.
            }
        }

        ma.push(data)
        expected = {'aapl': 15., 'ibm': 10.}
        calculated = ma.value
        for name in expected:
            self.assertAlmostEqual(expected[name], calculated[name], 15)

        data = {
            'aapl': {
                'close': 10.
            },
            'ibm': {
                'close': 11.
            },
            'goog': {
                'close': 8.
            }
        }

        ma.push(data)
        expected = {'aapl': 12.5, 'ibm': 10.5}
        calculated = ma.value
        for name in expected:
            self.assertAlmostEqual(expected[name], calculated[name], 15)
    def testCompoundedSecurityValueHolder(self):
        ma = SecurityMovingAverage(2, 'close', ['aapl', 'ibm'])
        compounded = ma >> SecurityMovingMax(3)

        container = {'aapl': deque(maxlen=3), 'ibm': deque(maxlen=3)}
        expected = {'aapl': 0.0, 'ibm': 0.0}
        for i in range(len(self.datas['aapl']['close'])):
            data = {'aapl': {Factors.CLOSE: self.datas['aapl'][Factors.CLOSE][i]},
                    'ibm': {Factors.CLOSE: self.datas['ibm'][Factors.CLOSE][i]}}
            ma.push(data)
            maRes = ma.value
            for name in maRes:
                container[name].append(maRes[name])
                expected[name] = max(container[name])

            compounded.push(data)
            calculated = compounded.value
            for name in calculated:
                self.assertAlmostEqual(expected[name], calculated[name], 12, "for {0} at index {1}\n"
                                                                             "expected:   {2}\n"
                                                                             "calculated: {3}"
                                       .format(name, i, expected[name], calculated[name]))
    def testGeSecurityValueHolder(self):
        filter = SecurityMovingAverage(1, 'close') >= 10.0
        ma = SecurityMovingAverage(10, 'close')[filter]

        data = {'aapl': {'close': 15.},
                'ibm': {'close': 10.},
                'goog': {'close': 7.}}

        ma.push(data)
        expected = {'aapl': 15., 'ibm': 10.}
        calculated = ma.value
        for name in expected:
            self.assertAlmostEqual(expected[name], calculated[name], 15)

        data = {'aapl': {'close': 10.},
                'ibm': {'close': 11.},
                'goog': {'close': 8.}}

        ma.push(data)
        expected = {'aapl': 12.5, 'ibm': 10.5}
        calculated = ma.value
        for name in expected:
            self.assertAlmostEqual(expected[name], calculated[name], 15)
    def testSecurityMovingAverage(self):
        window = 10
        ma1 = SecurityMovingAverage(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.mean(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):
            _ = SecurityMovingAverage(window, ['close', 'open'])