def testShiftedSecurityValueHolder(self):
        mm = SecurityMovingAverage(2, 'close')
        shifted1 = mm.shift(1)

        data1 = {'aapl': {'close': 1.0},
                 'ibm': {'close': 2.0},
                 'goog': {'close': 3.0}}
        shifted1.push(data1)
        calculated = shifted1.value
        for name in calculated.index():
            self.assertTrue(np.isnan(calculated[name]))

        data2 = {'aapl': {'close': 2.0},
                 'ibm': {'close': 3.0},
                 'goog': {'close': 4.0}}

        shifted1.push(data2)
        expected = SeriesValues({'aapl': 1.0,
                                 'ibm': 2.0,
                                 'goog': 3.0})
        calculated = shifted1.value
        for name in expected.index():
            self.assertAlmostEqual(expected[name], calculated[name])

        data3 = ({'aapl': {'close': 3.0},
                  'ibm': {'close': 4.0},
                  'goog': {'close': 5.0}})

        shifted1.push(data3)
        expected = SeriesValues({'aapl': 1.5,
                                 'ibm': 2.5,
                                 'goog': 3.5})
        calculated = shifted1.value
        for name in expected.index():
            self.assertAlmostEqual(expected[name], calculated[name])
    def testSecurityValuesAdd(self):

        data1 = np.array([3, 2, 2., 1., 4., 5.])
        data2 = -np.array([3, 2, 2., 1., 4., 5.])
        index = [1, 2, 3, 4, 5, 6]

        test1 = SeriesValues(data1, index)
        test2 = SeriesValues(data2, index)

        calculated = test1 + test2
        expected = SeriesValues(data1 + data2, index)

        np.testing.assert_array_equal(calculated.values, expected.values)
        self.assertEqual(calculated.name_mapping, expected.name_mapping)

        calculated = test1 + 2.0
        expected = SeriesValues(data1 + 2.0, index)

        np.testing.assert_array_equal(calculated.values, expected.values)
        self.assertEqual(calculated.name_mapping, expected.name_mapping)

        calculated = 2.0 + test2
        expected = SeriesValues(2.0 + data2, index)

        np.testing.assert_array_equal(calculated.values, expected.values)
        self.assertEqual(calculated.name_mapping, expected.name_mapping)
    def testSecurityValuesUnit(self):
        data = np.array([3, -2, np.nan, np.nan, 4, 5])
        index = [1, 2, 3, 4, 5, 6]
        test = SeriesValues(data, index)
        test = test.unit()

        expected = SeriesValues(data / np.nansum(np.abs(data)), dict(zip(index, range(len(index)))))
        for name in test.index():
            if np.isnan(test[name]):
                self.assertTrue(np.isnan(expected[name]))
            else:
                self.assertEqual(test[name], expected[name])
    def testSecurityValuesRankWithGroup(self):
        data = np.random.randn(3000)
        groups = np.random.randint(0, 30, 3000)
        index = list(range(3000))

        data = SeriesValues(data, index)
        groups = SeriesValues(groups, index)

        test = data.rank(groups)

        pd_series = pd.Series(data.values)
        expected = pd_series.groupby(groups.values).rank()
        np.testing.assert_array_almost_equal(test.values, expected.values)
    def testSecurityValuesRank(self):

        data = np.array([3, 2, np.nan, np.nan, 4, 5])
        index = [1, 2, 3, 4, 5, 6]

        data = SeriesValues(data, index)
        test = data.rank()

        expected = SeriesValues(np.array([2, 1, np.nan, np.nan, 3, 4]), dict(zip(index, range(len(index)))))
        for name in test.index():
            if np.isnan(test[name]):
                self.assertTrue(np.isnan(expected[name]))
            else:
                self.assertEqual(test[name], expected[name])
    def testSecurityValuesXor(self):
        data1 = np.array([3, 2, 2., 1., 4., 5.])
        data2 = -np.array([3, 2, 2., 1., 4., 5.])
        index = [1, 2, 3, 4, 5, 6]

        test1 = SeriesValues(data1, index)
        test2 = SeriesValues(data2, index)

        calculated = test1 ^ test2

        expected = SeriesValues(np.array([data1, data2]).T, index=index)
        np.testing.assert_array_equal(calculated.values, expected.values)
        self.assertEqual(calculated.name_mapping, expected.name_mapping)

        for name in index:
            np.testing.assert_array_almost_equal(calculated[name], expected[name])
    def testSecurityConstArrayValueHolder(self):
        constArray = SecurityConstArrayValueHolder({'a': 1.0, 'b': 2.0})

        testValue = constArray.value
        expectedValue = SeriesValues(np.array([1.0, 2.0]), index=['a', 'b'])

        for name in expectedValue.index():
            self.assertAlmostEqual(testValue[name], expectedValue[name])

        testValue = constArray.value_by_name('a')
        self.assertAlmostEqual(1.0, testValue)

        testValue = constArray.value_by_names(['b', 'a', 'c'])
        expectedValue = SeriesValues(np.array([2.0, 1.0, np.nan]), index=['b', 'a', 'c'])

        np.testing.assert_array_almost_equal(testValue.values, expectedValue.values)
        self.assertEqual(testValue.name_mapping, expectedValue.name_mapping)
    def testSecurityValuesDeepCopy(self):
        data = np.array([3, 2, 2., 1., 4., 5.])
        index = [1, 2, 3, 4, 5, 6]

        test = SeriesValues(data, index)
        copied = copy.deepcopy(test)

        np.testing.assert_array_equal(test.values, copied.values)
        self.assertEqual(test.name_mapping, copied.name_mapping)
    def testSecurityValuesInit(self):
        data = np.array([1, 2, 3])
        index = ['c', 'b', 'a']

        test = SeriesValues(data, dict(zip(index, range(len(index)))))
        expected = dict(zip(index, data))

        for name in test.index():
            self.assertEqual(test[name], expected[name])
    def testDeltaSecurityValueHolder(self):
        mm = SecurityMovingAverage(2, 'close')
        delta1 = SecurityDeltaValueHolder(mm, 2)

        data1 = {'aapl': {'close': 1.0},
                 'ibm': {'close': 2.0},
                 'goog': {'close': 3.0}}
        delta1.push(data1)
        calculated = delta1.value
        for name in calculated.index():
            self.assertTrue(np.isnan(calculated[name]))

        data2 = {'aapl': {'close': 2.0},
                 'ibm': {'close': 3.0},
                 'goog': {'close': 4.0}}

        delta1.push(data2)

        data3 = {'aapl': {'close': 3.0},
                 'ibm': {'close': 4.0},
                 'goog': {'close': 5.0}}

        delta1.push(data3)

        expected = SeriesValues({'aapl': 1.5,
                                 'ibm': 1.5,
                                 'goog': 1.5})
        calculated = delta1.value
        for name in expected.index():
            self.assertAlmostEqual(expected[name], calculated[name])

        data4 = ({'aapl': {'close': 4.0},
                  'ibm': {'close': 5.0},
                  'goog': {'close': 6.0}})

        delta1.push(data4)
        expected = SeriesValues({'aapl': 2.0,
                                 'ibm': 2.0,
                                 'goog': 2.0})
        calculated = delta1.value
        for name in expected.index():
            self.assertAlmostEqual(expected[name], calculated[name])
Exemplo n.º 11
0
    def testSecurityValuesRes(self):
        data1 = np.array([3, 2, 2., 1., 4., 5.])
        data2 = -np.array([3, 2, 2., 1., 4., 5.])
        index = [1, 2, 3, 4, 5, 6]

        test1 = SeriesValues(data1, index)
        test2 = SeriesValues(data2, index)

        calculated = test1.res(test2)
        expected = SeriesValues(np.zeros(len(data1)), index)

        np.testing.assert_array_almost_equal(calculated.values, expected.values)
        self.assertEqual(calculated.name_mapping, expected.name_mapping)

        data1 = np.random.randn(100)
        data2 = np.random.randn(100)
        index = list(range(1, 101))

        test1 = SeriesValues(data1, index)
        test2 = SeriesValues(data2, index)

        calculated = test1.res(test2)
        expected = SeriesValues(data1 - np.dot(data2, data1) / np.dot(data2, data2) * data2, index)

        np.testing.assert_array_almost_equal(calculated.values, expected.values)
        self.assertEqual(calculated.name_mapping, expected.name_mapping)
Exemplo n.º 12
0
    def testSecurityValuesPickle(self):
        data = np.array([3, 2, np.nan, np.nan, 4, 5])
        index = [1, 2, 3, 4, 5, 6]

        test = SeriesValues(data, index)

        f = tempfile.NamedTemporaryFile('w+b', delete=False)
        pickle.dump(test, f)
        f.close()

        with open(f.name, 'rb') as f2:
            pickled = pickle.load(f2)
            np.testing.assert_array_equal(test.values, pickled.values)
            self.assertEqual(test.name_mapping, pickled.name_mapping)

        os.unlink(f.name)
    def __init__(self):

        self.closes = CLOSE()
        self.mul = LAST('multiplier')
        self.preMul = LAST('multiplier').shift(1)
        self.preCloses = CLOSE().shift(1)
        self.openes = OPEN()
        self.lowes = LOW()
        self.highes = HIGH()

        # 计算指数的备用指标
        self.closeDLastClose = self.closes / self.preCloses - 1.
        self.closeDOpen = self.closes / self.openes - 1.
        self.closeDLow = self.closes / self.lowes - 1.
        self.highDClose = 1. - self.highes / self.closes

        self.ih_amount = 28
        self.if_amount = 42
        self.ic_amount = 12

        # 定义指数的权重
        names = ['000016.zicn', '000300.zicn', '000905.zicn']
        self.indexWeights = SeriesValues(np.array([1., -2., 1.]),
                                         index=dict(zip(names, range(len(names)))))
    def testSecuritiesValuesArithmetic(self):
        benchmarkValues = {'AAPL': 1.0, 'IBM': 2.0, 'GOOG': 3.0}
        values = SeriesValues(benchmarkValues)

        self.assertEqual(len(benchmarkValues), len(values))

        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key], values[key], 12)

        negValues = -values
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key], -negValues[key], 12)

        benchmarkValues2 = {'AAPL': 3.0, 'IBM': 2.0, 'GOOG': 1.0}
        values2 = SeriesValues(benchmarkValues2)
        addedValue = values + values2
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] + benchmarkValues2[key], addedValue[key], 12)

        subbedValues = values - values2
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] - benchmarkValues2[key], subbedValues[key], 12)

        multiValues = values * values2
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] * benchmarkValues2[key], multiValues[key], 12)

        divValues = values / values2
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] / benchmarkValues2[key], divValues[key], 12)

        # check operated with scalar
        addedValue = values + 2.0
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] + 2.0, addedValue[key], 12)

        subbedValue = values - 2.0
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] - 2.0, subbedValue[key], 12)

        multiValues = values * 2.0
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] * 2.0, multiValues[key], 12)

        divValues = values / 2.0
        for key in benchmarkValues:
            self.assertAlmostEqual(benchmarkValues[key] / 2.0, divValues[key], 12)

        # check right associate operators
        addedValue = 2.0 + values
        for key in benchmarkValues:
            self.assertAlmostEqual(2.0 + benchmarkValues[key], addedValue[key], 12)

        subbedValue = 2.0 - values
        for key in benchmarkValues:
            self.assertAlmostEqual(2.0 - benchmarkValues[key], subbedValue[key], 12)

        multiValues = 2.0 * values
        for key in benchmarkValues:
            self.assertAlmostEqual(2.0 * benchmarkValues[key], multiValues[key], 12)

        divValues = 2.0 / values
        for key in benchmarkValues:
            self.assertAlmostEqual(2.0 / benchmarkValues[key], divValues[key], 12)
    def testSecuritiesValuesComparison(self):

        benchmarkValues = SeriesValues({'AAPL': 1.0, 'IBM': 2.0, 'GOOG': 3.0})
        calculated = benchmarkValues > 1.5
        expected = SeriesValues({'AAPL': False, 'IBM': True, 'GOOG': True})
        self.checker(expected, calculated)

        calculated = benchmarkValues < 1.5
        expected = SeriesValues({'AAPL': True, 'IBM': False, 'GOOG': False})
        self.checker(expected, calculated)

        calculated = benchmarkValues >= 1.0
        expected = SeriesValues({'AAPL': True, 'IBM': True, 'GOOG': True})
        self.checker(expected, calculated)

        calculated = benchmarkValues <= 2.0
        expected = SeriesValues({'AAPL': True, 'IBM': True, 'GOOG': False})
        self.checker(expected, calculated)

        benchmarkValues = SeriesValues({'AAPL': False, 'IBM': True, 'GOOG': False})
        calculated = benchmarkValues & True
        expected = SeriesValues({'AAPL': False, 'IBM': True, 'GOOG': False})
        self.checker(expected, calculated)

        calculated = True & benchmarkValues
        expected = SeriesValues({'AAPL': False, 'IBM': True, 'GOOG': False})
        self.checker(expected, calculated)

        calculated = benchmarkValues | True
        expected = SeriesValues({'AAPL': True, 'IBM': True, 'GOOG': True})
        self.checker(expected, calculated)

        calculated = True | benchmarkValues
        expected = SeriesValues({'AAPL': True, 'IBM': True, 'GOOG': True})
        self.checker(expected, calculated)

        benchmarkValues = SeriesValues({'AAPL': 1.0, 'IBM': 2.0, 'GOOG': 3.0})
        calculated = benchmarkValues == 2.0
        expected = SeriesValues({'AAPL': False, 'IBM': True, 'GOOG': False})
        self.checker(expected, calculated)

        calculated = benchmarkValues != 2.0
        expected = SeriesValues({'AAPL': True, 'IBM': False, 'GOOG': True})
        self.checker(expected, calculated)