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 = SecuritiesValues({'aapl': 1.0, 'ibm': 2.0, 'goog': 3.0}) calculated = shifted1.value for name in expected.index: self.assertAlmostEqual(expected[name], calculated[name]) data3 = SecuritiesValues({'aapl': {'close': 3.0}, 'ibm': {'close': 4.0}, 'goog': {'close': 5.0}}) shifted1.push(data3) expected = SecuritiesValues({'aapl': 1.5, 'ibm': 2.5, 'goog': 3.5}) calculated = shifted1.value for name in expected.index: self.assertAlmostEqual(expected[name], calculated[name])
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 testTransformWithDifferentGroup(self): test_df = pd.DataFrame({'code': [1, 2, 3, 4, 5, 6, 7], 'b': [4, 5, 6, 7, 6, 5, 4], 'c': [9, 8, 7, 6, 5, 4, 3]}, index=[1, 1, 1, 1, 2, 2, 2], dtype=float) expression = SecurityMovingAverage(2, 'b') calculated = expression.transform(test_df, name='new_factor', category_field='code') expected = [4., 5., 6., 7., 6., 5., 4.] np.testing.assert_array_almost_equal(calculated['new_factor'], expected)
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 testTransformWithoutCategoryAndDuplicatedIndex(self): sample_data = pd.DataFrame( data={'code': [1, 2, 1, 2, 1, 2], 'open': [2.0, 1.0, 1.5, 3.0, 2.4, 3.5], 'close': [1.7, 1.6, 0.9, 3.8, 1.6, 2.1]}, index=[1, 1, 2, 2, 3, 3], dtype=float ) expression = SecurityMovingAverage(2, 'close') calculated = expression.transform(sample_data, name='new_factor') expected = sample_data.rolling(window=2).mean() np.testing.assert_array_almost_equal(calculated['new_factor'].values[1:], expected['close'].values[1:])
def testFilterSecurityValueHolderWorkWithStr(self): filter = SecurityLatestValueHolder('code') == 'ibm' ma = FilteredSecurityValueHolder(SecurityMovingAverage(10, 'close'), filter) data = { 'aapl': { 'code': 'aapl', 'close': 15. }, 'ibm': { 'code': 'ibm', 'close': 10. }, 'goog': { 'code': 'goog', 'close': 7. } } ma.push(data) expected = {'ibm': 10.0} calculated = ma.value for name in calculated.index(): self.assertAlmostEqual(expected[name], calculated[name], 15)
def MA(window, x='x', closed="right"): if isinstance(window, int): return SecurityMovingAverage(window, x) elif isinstance(window, str): return SecurityTimeMovingAverage(window, x, closed) else: raise ValueError("wrong window format <{0}>".format(window))
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 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' 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 testGeSecurityValueHolder(self): filter = SecurityMovingAverage(1, 'close') >= 10.0 ma = FilteredSecurityValueHolder(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 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])
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 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 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)
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'])
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))
def testShiftedSecurityValueHolder(self): mm = SecurityMovingAverage(2, 'close', ['aapl', 'ibm', 'goog']) shifted1 = mm.shift(1) data1 = {'aapl': {'close': 1.0}, 'ibm': {'close': 2.0}, 'goog': {'close': 3.0}} shifted1.push(data1) expected = {'aapl': np.nan, 'ibm': np.nan, 'goog': np.nan} calculated = shifted1.value for name in expected: self.assertIs(expected[name], calculated[name]) data2 = {'aapl': {'close': 2.0}, 'ibm': {'close': 3.0}, 'goog': {'close': 4.0}} shifted1.push(data2) expected = {'aapl': 1.0, 'ibm': 2.0, 'goog': 3.0} calculated = shifted1.value for name in expected: self.assertAlmostEqual(expected[name], calculated[name]) data3 = {'aapl': {'close': 3.0}, 'ibm': {'close': 4.0}, 'goog': {'close': 5.0}} shifted1.push(data3) expected = {'aapl': 1.5, 'ibm': 2.5, 'goog': 3.5} calculated = shifted1.value for name in expected: self.assertAlmostEqual(expected[name], calculated[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])
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' 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 testGtSecurityValueHolder(self): filter = SecurityLatestValueHolder('close') > 10.0 ma = SecurityMovingAverage(10, 'close')[filter] data = { 'aapl': { 'close': 15. }, 'ibm': { 'close': 8. }, 'goog': { 'close': 7. } } ma.push(data) expected = {'aapl': 15.} calculated = ma.value for name in expected: self.assertAlmostEqual(expected[name], calculated[name], 15) data = { 'aapl': { 'close': 9. }, 'ibm': { 'close': 11. }, 'goog': { 'close': 8. } } ma.push(data) expected = {'ibm': 9.5} calculated = ma.value for name in expected: self.assertAlmostEqual(expected[name], calculated[name], 15)
def testShiftedSecurityValueHolderWithLengthZero(self): mm = SecurityMovingAverage(2, 'close', ['aapl', 'ibm', 'goog']) with self.assertRaises(ValueError): _ = mm.shift(0)
def MA(window, dependency='x', symbolList=None): return SecurityMovingAverage(window, dependency, symbolList)
def testCombinedSecurityValueHolderWithoutData(self): ma = SecurityMovingAverage(10, 'close') calculated = ma.value for name in calculated.index(): self.assertTrue(np.isnan(calculated[name]))
def MA(window, x='x'): return SecurityMovingAverage(window, x)
def MA(window, dependency='x'): return SecurityMovingAverage(window, dependency)
def testShiftedSecurityValueHolderWithLengthZero(self): mm = SecurityMovingAverage(2, 'close') with self.assertRaises(ValueError): _ = mm.shift(0)