def testFilteredSecurityValueHolder(self): benchmark = SecurityLatestValueHolder(dependency='close') > 0 filtered = FilteredSecurityValueHolder(benchmark, benchmark) 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] } } benchmark.push(data) filtered.push(data) try: _ = filtered['aapl'] self.assertTrue(benchmark['aapl']) except KeyError: self.assertFalse(benchmark['aapl']) try: _ = filtered['ibm'] self.assertTrue(benchmark['ibm']) except KeyError: self.assertFalse(benchmark['ibm'])
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 testFilteredSecurityValueHolder(self): benchmark = SecurityLatestValueHolder(dependency='close') > 0 filtered = FilteredSecurityValueHolder(benchmark, benchmark) 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] } } benchmark.push(data) filtered.push(data) if np.isnan(filtered['aapl']): self.assertTrue(filtered['aapl']) if np.isnan(filtered['ibm']): self.assertTrue(filtered['ibm'])
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)