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'])
示例#2
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 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 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 testTransformWithoutCategory(self):
        test_df = pd.DataFrame({'b': [4, 5, 6, 7, 6, 5, 4],
                                'c': [9, 8, 7, 6, 5, 4, 3]},
                               index=[1, 2, 3, 4, 5, 6, 7],
                               dtype=float)

        expression = SecurityMovingMax(20, 'b') + SecurityMovingMin(20, 'c')
        calculated = expression.transform(test_df, name='new_factor')
        expected = [13., 13., 13., 13., 12., 11., 10.]
        np.testing.assert_array_almost_equal(calculated['new_factor'], expected)
 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 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}})

        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)
示例#11
0
    def testSecurityValueHolderIsFull(self):
        test = SecurityMovingMax(2, dependency='close')

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

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

        data = {'ibm': {'close': 13.0}}
        test.push(data)
        self.assertEqual(test.isFull, True)
    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'])
示例#13
0
def MMAX(window, x='x'):
    return SecurityMovingMax(window, x)
示例#14
0
def MMAX(window, dependency='x'):
    return SecurityMovingMax(window, dependency)
示例#15
0
def MAX(window, dependency='x', symbolList=None):
    return SecurityMovingMax(window, dependency, symbolList)